Skip to content

Commit

Permalink
Refactor licenses page summary
Browse files Browse the repository at this point in the history
  • Loading branch information
candela97 committed Mar 30, 2024
1 parent b867b9c commit f8aae3e
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 78 deletions.
4 changes: 2 additions & 2 deletions src/js/Content/Features/Store/Licenses/CLicenses.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Context, ContextType} from "../../../modulesContent";
import FLincensesSummary from "./FLincensesSummary";
import {FLicensesSummary} from "./FLicensesSummary.svelte";

export class CLicenses extends Context {

constructor() {

super(ContextType.LICENSES, [
FLincensesSummary,
FLicensesSummary,
]);
}
}
121 changes: 121 additions & 0 deletions src/js/Content/Features/Store/Licenses/FLicensesSummary.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<script lang="ts" context="module">
// @ts-ignore
import self_ from "./FLicensesSummary.svelte";
import {Feature} from "../../../modulesContent";
import type {CLicenses} from "./CLicenses";
export class FLicensesSummary extends Feature<CLicenses> {
override apply(): void {
let target = document.querySelector(".youraccount_page");
if (!target) {
throw new Error("Node not found");
}
(new self_({
target,
anchor: target.firstElementChild,
}));
}
}
</script>

<script lang="js">
import {Localization} from "../../../../modulesCore";
const list = {};
const types = {};
for (const item of document.querySelectorAll(".account_table tr")) {
const dateStr = item.querySelector(".license_date_col")?.textContent.trim();
const type = item.querySelector(".license_acquisition_col")?.textContent.trim();
if (!dateStr || !type) { continue; }
const year = /\d{4}/.exec(dateStr)?.[0] ?? "";
(list[year] ??= {})[type] = (list[year][type] ?? 0) + 1;
types[type] = (types[type] ?? 0) + 1;
}
const listEntries = Object.entries(list).reverse();
const totals = listEntries.map(
([, row]) => Object.entries(row)
.map(([, count]) => count)
.reduce((a, b) => a + b, 0)
);
const totalGlobal = totals.reduce((a, b) => a + b, 0);
const typesEntries = Object.entries(types);
const tableHeader = typesEntries
.map(([name]) => `<th>${name}</th>`)
.join("");
const tableFooter = typesEntries
.map(([, value]) => `<td>${value || "0"}</td>`)
.join("");
const rows = listEntries.map(
([year, row], i) =>
`<tr>
<td>${year}</td>
${typesEntries
.map(([name]) => `<td>${row[name] || "0"}</td>`)
.join("")
}
<td>${totals[i] || "0"}</td>
</tr>`
)
.join("");
let show = false;
function onClick() {
show = !show;
}
</script>
<span class="h3">{Localization.str.wl.label}</span>
<button on:click={onClick}>{show ? Localization.str.hide : Localization.str.show}</button>
{#if show}
<div class="block_content">
<table class="account_table">
<thead>
<tr>
<th>{Localization.str.year}</th>
{@html tableHeader}
<th>{Localization.str.all}</th>
</tr>
</thead>
<tbody>
{@html rows}
</tbody>
<tfoot>
<tr>
<td>{Localization.str.all}</td>
{@html tableFooter}
<td>{totalGlobal}</td>
</tr>
</tfoot>
</table>
</div>
{/if}
<style>
.h3 {
color: #ffffff;
font-size: 22px;
font-family: "Motiva Sans", Sans-serif;
font-weight: normal;
}
button {
vertical-align: text-bottom;
margin-left: 5px;
cursor: pointer;
border: solid #8ecafc 1px;
padding: 0px 10px;
}
:global(.block) {
margin-top: 20px;
}
</style>
76 changes: 0 additions & 76 deletions src/js/Content/Features/Store/Licenses/FLincensesSummary.js

This file was deleted.

0 comments on commit f8aae3e

Please sign in to comment.