From 9206fed4cef7e2b5d33b8c4c2ebeb61941a55ca2 Mon Sep 17 00:00:00 2001 From: NaNoMelo Date: Tue, 22 Oct 2024 13:17:50 +0200 Subject: [PATCH] Implemented locales + previous weeks in time graph --- counter/static/webpack/graph-index.ts | 42 +++++++++++++++++------- counter/templates/counter/activity.jinja | 6 ++-- locale/fr/LC_MESSAGES/django.po | 4 +++ sith/settings.py | 1 + 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/counter/static/webpack/graph-index.ts b/counter/static/webpack/graph-index.ts index c68969ec..54a7b1f9 100644 --- a/counter/static/webpack/graph-index.ts +++ b/counter/static/webpack/graph-index.ts @@ -12,6 +12,7 @@ interface ActivityChartConfig { canvas: HTMLCanvasElement; startDate: Date; counterId: number; + locale: string; } interface OpeningTime { @@ -23,11 +24,9 @@ interface EventInput { start: Date; end: Date; backgroundColor: string; + title?: string; } -// TODO: Semaines passées -// TODO: Manage locales - exportToHtml("loadChart", loadChart); async function loadChart(options: ActivityChartConfig) { @@ -44,25 +43,44 @@ async function loadChart(options: ActivityChartConfig) { const calendar = new Calendar(options.canvas, { plugins: [timeGridPlugin], initialView: "timeGridWeek", - locale: "fr", + locale: options.locale, slotLabelFormat: { hour: "2-digit", minute: "2-digit", hour12: false }, dayHeaderFormat: { weekday: "long" }, firstDay: 1, views: { timeGrid: { allDaySlot: false } }, scrollTime: "09:00:00", - headerToolbar: { left: "", center: "", right: "" }, + headerToolbar: { left: "prev today", center: "title", right: "" }, events: events, nowIndicator: true, height: 600, }); calendar.render(); + + calendar.on("datesSet", async (info) => { + if (options.startDate <= info.start) { + return; + } + const newPerms = await paginated(permanencyFetchPermanancies, { + query: { + counter: [options.counterId], + // biome-ignore lint/style/useNamingConvention: backend API uses snake_case + end_after: info.startStr, + // biome-ignore lint/style/useNamingConvention: backend API uses snake_case + start_before: info.endStr, + }, + } as PermanencyFetchPermananciesData); + options.startDate = info.start; + calendar.addEventSource(getEvents(newPerms, false)); + permanancies.push(...newPerms); + calendar.render(); + }); } function roundToQuarter(date: Date, ceil: boolean) { const result = date; const minutes = date.getMinutes(); // removes minutes exceeding the lower quarter and adds 15 minutes if rounded to ceiling - result.setMinutes(minutes + +ceil * 15 - (minutes % 15), 0, 0); + result.setMinutes(minutes - (minutes % 15) + +ceil * 15, 0, 0); return result; } @@ -99,12 +117,15 @@ function getOpeningTimes(rawPermanancies: PermanencySchema[]) { return openingTimes; } -function getEvents(permanancies: PermanencySchema[]) { +function getEvents(permanancies: PermanencySchema[], currentWeek = true): EventInput[] { const openingTimes = getOpeningTimes(permanancies); const events: EventInput[] = []; for (const openingTime of openingTimes) { - const lastMonday = getLastMonday(); - const shift = openingTime.end < lastMonday; + let shift = false; + if (currentWeek) { + const lastMonday = getLastMonday(); + shift = openingTime.end < lastMonday; + } // if permanancies took place last week (=before monday), // -> display them in lightblue as part of the current week events.push({ @@ -117,8 +138,7 @@ function getEvents(permanancies: PermanencySchema[]) { } // Function to get last Monday at 00:00 -function getLastMonday(): Date { - const now = new Date(); +function getLastMonday(now = new Date()): Date { const dayOfWeek = now.getDay(); const lastMonday = new Date(now); lastMonday.setDate(now.getDate() - ((dayOfWeek + 6) % 7)); // Adjust for Monday as day 1 diff --git a/counter/templates/counter/activity.jinja b/counter/templates/counter/activity.jinja index 8df16b12..2732110e 100644 --- a/counter/templates/counter/activity.jinja +++ b/counter/templates/counter/activity.jinja @@ -26,8 +26,8 @@ {% trans %}There is currently no barman connected.{% endtrans %} {% endif %} -

{% trans %}Last Week Activity {% endtrans %}

-
+

{% trans %}Last weeks opening times{% endtrans %}

+


{% endif %} @@ -51,8 +51,10 @@ window.addEventListener("DOMContentLoaded", () => { loadChart({ canvas: document.getElementById("activityGraph"), + // sets the start day to 7 days ago startDate: new Date(new Date().setDate(new Date().getDate() - 7)), counterId: {{ counter.id }}, + locale: {{ get_current_language()|tojson }} }); }); diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po index 4b717fd4..4db5dd6d 100644 --- a/locale/fr/LC_MESSAGES/django.po +++ b/locale/fr/LC_MESSAGES/django.po @@ -6143,3 +6143,7 @@ msgstr "Vous ne pouvez plus écrire de commentaires, la date est passée." #, python-format msgid "Maximum characters: %(max_length)s" msgstr "Nombre de caractères max: %(max_length)s" + +#: counter/activity.jinja +msgid "Last weeks opening times" +msgstr "Heures d'ouverture des dernières semaines" \ No newline at end of file diff --git a/sith/settings.py b/sith/settings.py index 3f6fd731..5a8dc413 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -164,6 +164,7 @@ TEMPLATES = [ "ProductType": "counter.models.ProductType", "timezone": "django.utils.timezone", "get_sith": "com.views.sith", + "get_current_language": "django.views.i18n.get_language", }, "bytecode_cache": { "name": "default",