Skip to content

Commit 98b4616

Browse files
committed
Implemented locales + previous weeks in time graph
1 parent df22e0c commit 98b4616

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

counter/static/webpack/graph-index.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface ActivityChartConfig {
1212
canvas: HTMLCanvasElement;
1313
startDate: Date;
1414
counterId: number;
15+
locale: string;
1516
}
1617

1718
interface OpeningTime {
@@ -23,11 +24,9 @@ interface EventInput {
2324
start: Date;
2425
end: Date;
2526
backgroundColor: string;
27+
title?: string;
2628
}
2729

28-
// TODO: Semaines passées
29-
// TODO: Manage locales
30-
3130
exportToHtml("loadChart", loadChart);
3231

3332
async function loadChart(options: ActivityChartConfig) {
@@ -44,25 +43,44 @@ async function loadChart(options: ActivityChartConfig) {
4443
const calendar = new Calendar(options.canvas, {
4544
plugins: [timeGridPlugin],
4645
initialView: "timeGridWeek",
47-
locale: "fr",
46+
locale: options.locale,
4847
slotLabelFormat: { hour: "2-digit", minute: "2-digit", hour12: false },
4948
dayHeaderFormat: { weekday: "long" },
5049
firstDay: 1,
5150
views: { timeGrid: { allDaySlot: false } },
5251
scrollTime: "09:00:00",
53-
headerToolbar: { left: "", center: "", right: "" },
52+
headerToolbar: { left: "prev today", center: "title", right: "" },
5453
events: events,
5554
nowIndicator: true,
5655
height: 600,
5756
});
5857
calendar.render();
58+
59+
calendar.on("datesSet", async (info) => {
60+
if (options.startDate <= info.start) {
61+
return;
62+
}
63+
const newPerms = await paginated(permanencyFetchPermanancies, {
64+
query: {
65+
counter: [options.counterId],
66+
// biome-ignore lint/style/useNamingConvention: backend API uses snake_case
67+
end_after: info.startStr,
68+
// biome-ignore lint/style/useNamingConvention: backend API uses snake_case
69+
start_before: info.endStr,
70+
},
71+
} as PermanencyFetchPermananciesData);
72+
options.startDate = info.start;
73+
calendar.addEventSource(getEvents(newPerms, false));
74+
permanancies.push(...newPerms);
75+
calendar.render();
76+
});
5977
}
6078

6179
function roundToQuarter(date: Date, ceil: boolean) {
6280
const result = date;
6381
const minutes = date.getMinutes();
6482
// removes minutes exceeding the lower quarter and adds 15 minutes if rounded to ceiling
65-
result.setMinutes(minutes + +ceil * 15 - (minutes % 15), 0, 0);
83+
result.setMinutes(minutes - (minutes % 15) + +ceil * 15, 0, 0);
6684
return result;
6785
}
6886

@@ -99,12 +117,15 @@ function getOpeningTimes(rawPermanancies: PermanencySchema[]) {
99117
return openingTimes;
100118
}
101119

102-
function getEvents(permanancies: PermanencySchema[]) {
120+
function getEvents(permanancies: PermanencySchema[], currentWeek = true): EventInput[] {
103121
const openingTimes = getOpeningTimes(permanancies);
104122
const events: EventInput[] = [];
105123
for (const openingTime of openingTimes) {
106-
const lastMonday = getLastMonday();
107-
const shift = openingTime.end < lastMonday;
124+
let shift = false;
125+
if (currentWeek) {
126+
const lastMonday = getLastMonday();
127+
shift = openingTime.end < lastMonday;
128+
}
108129
// if permanancies took place last week (=before monday),
109130
// -> display them in lightblue as part of the current week
110131
events.push({
@@ -117,8 +138,7 @@ function getEvents(permanancies: PermanencySchema[]) {
117138
}
118139

119140
// Function to get last Monday at 00:00
120-
function getLastMonday(): Date {
121-
const now = new Date();
141+
function getLastMonday(now = new Date()): Date {
122142
const dayOfWeek = now.getDay();
123143
const lastMonday = new Date(now);
124144
lastMonday.setDate(now.getDate() - ((dayOfWeek + 6) % 7)); // Adjust for Monday as day 1

counter/templates/counter/activity.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{% trans %}There is currently no barman connected.{% endtrans %}
2727
{% endif %}
2828
</ul>
29-
<h4>{% trans %}Last Week Activity {% endtrans %}</h4>
29+
<h4>{% trans %}Last weeks opening times{% endtrans %}</h4>
3030
<div id="activityGraph" width="400" height="200"></div>
3131
<br/>
3232
<br/>
@@ -51,8 +51,10 @@
5151
window.addEventListener("DOMContentLoaded", () => {
5252
loadChart({
5353
canvas: document.getElementById("activityGraph"),
54+
// sets the start day to 7 days ago
5455
startDate: new Date(new Date().setDate(new Date().getDate() - 7)),
5556
counterId: {{ counter.id }},
57+
locale: {{ get_current_language()|tojson }}
5658
});
5759
});
5860
</script>

locale/fr/LC_MESSAGES/django.po

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6076,3 +6076,7 @@ msgstr "Vous ne pouvez plus écrire de commentaires, la date est passée."
60766076
#, python-format
60776077
msgid "Maximum characters: %(max_length)s"
60786078
msgstr "Nombre de caractères max: %(max_length)s"
6079+
6080+
#: counter/activity.jinja
6081+
msgid "Last weeks opening times"
6082+
msgstr "Heures d'ouverture des dernières semaines"

sith/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
"ProductType": "counter.models.ProductType",
166166
"timezone": "django.utils.timezone",
167167
"get_sith": "com.views.sith",
168+
"get_current_language": "django.views.i18n.get_language",
168169
},
169170
"bytecode_cache": {
170171
"name": "default",

0 commit comments

Comments
 (0)