diff options
author | Daniel Schadt <kingdread@gmx.de> | 2024-04-08 19:51:15 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2024-04-08 19:51:15 +0200 |
commit | 34e2bcf51e08806db300683a4b024399bf221c7f (patch) | |
tree | b04c8866ff02660067cc1fbd260cafb3e4b5bcf3 /asset-sources/fietsboek.ts | |
parent | 982842cf5f7d1b3b7e641f6a89869e418906fc75 (diff) | |
parent | 8ff073f7c2d91b70d664877622fbdd95013c3781 (diff) | |
download | fietsboek-34e2bcf51e08806db300683a4b024399bf221c7f.tar.gz fietsboek-34e2bcf51e08806db300683a4b024399bf221c7f.tar.bz2 fietsboek-34e2bcf51e08806db300683a4b024399bf221c7f.zip |
Merge branch 'profile-graph'
Diffstat (limited to 'asset-sources/fietsboek.ts')
-rw-r--r-- | asset-sources/fietsboek.ts | 71 |
1 files changed, 69 insertions, 2 deletions
diff --git a/asset-sources/fietsboek.ts b/asset-sources/fietsboek.ts index d9d2c15..66caa4f 100644 --- a/asset-sources/fietsboek.ts +++ b/asset-sources/fietsboek.ts @@ -10,6 +10,10 @@ interface JsonFriend { id: number, } +interface YearSummary { + [index: string]: {[index: number]: number}, +} + interface Window { fietsboekImageIndex: number, fietsboekCurrentImage: HTMLDivElement | null, @@ -36,6 +40,19 @@ function getCookie(name: string): string | undefined { ?.split("=")[1]; } + +/** + * Builds a URL with the correct application URL as base. + * + * Do not add the leading slash, otherwise the resolution will be wrong! + * + * @param path - Path to append to the base. + * @return The correct URL in regards to the application URL. + */ +function makeUrl(name: string): URL { + return new URL(name, BASE_URL); +} + /** * Installs a listener to the given DOM objects. * @@ -390,7 +407,7 @@ addHandler(".summary-toggler", "click", toggleSummary); */ addHandler("#archiveDownloadButton", "click", () => { const checked = document.querySelectorAll(".archive-checkbox:checked"); - const url = new URL("/track/archive", window.location.href); + const url = makeUrl("track/archive"); checked.forEach((c) => { url.searchParams.append("track_id[]", (c as HTMLInputElement).value); }); @@ -453,7 +470,7 @@ function toggleTrackFavourite(event: MouseEvent) { if (trackId === null) { return; } - const url = new URL("/me/toggle-favourite", window.location.href); + const url = makeUrl("me/toggle-favourite"); const formData = new URLSearchParams(); formData.append("track-id", trackId); formData.append("csrf_token", getCookie("csrf_token") ?? ""); @@ -472,6 +489,56 @@ function toggleTrackFavourite(event: MouseEvent) { addHandler(".favourite-star", "click", toggleTrackFavourite); +/** + * Returns an array of localized month names (Jan-Dec). + */ +function getLocalizedMonthNames(): string[] { + const monthNames: string[] = []; + const date = new Date(2000, 0); + for (let i = 0; i < 12; i++) { + monthNames.push(date.toLocaleString(LOCALE, {month: 'long'})); + date.setMonth(i + 1); + } + return monthNames; +} + +/** + * Load and plot the user's summary. + */ +function loadProfileStats() { + const monthNames = getLocalizedMonthNames(); + + const url = makeUrl("me/summary.json"); + fetch(url) + .then(response => response.json()) + .then((response: YearSummary) => { + const datasets = []; + for (const [year, months] of Object.entries(response)) { + const data = []; + for (let i = 1; i <= 12; ++i) { + data.push((i in months) ? (months[i] / 1000) : 0); + } + datasets.push({ + data: data, + label: year, + }); + } + + new Chart( + "graph-month-summary", + { + type: "bar", + data: { + datasets: datasets, + labels: monthNames, + }, + }, + ); + }); +} + +/* Used via in-page scripts, so make eslint happy */ +loadProfileStats; document.addEventListener('DOMContentLoaded', function() { window.fietsboekImageIndex = 0; |