Skip to content

Commit

Permalink
Implemented locales + previous weeks in time graph
Browse files Browse the repository at this point in the history
  • Loading branch information
NaNoMelo committed Oct 22, 2024
1 parent df22e0c commit 21b4a5d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
42 changes: 31 additions & 11 deletions counter/static/webpack/graph-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface ActivityChartConfig {
canvas: HTMLCanvasElement;
startDate: Date;
counterId: number;
locale: string;
}

interface OpeningTime {
Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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({
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions counter/templates/counter/activity.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
{% trans %}There is currently no barman connected.{% endtrans %}
{% endif %}
</ul>
<h4>{% trans %}Last Week Activity {% endtrans %}</h4>
<div id="activityGraph" width="400" height="200"></div>
<h4>{% trans %}Last weeks opening times{% endtrans %}</h4>
<div id="activityGraph"></div>
<br/>
<br/>
{% endif %}
Expand All @@ -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 }}
});
});
</script>
Expand Down
4 changes: 4 additions & 0 deletions locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -6076,3 +6076,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"
1 change: 1 addition & 0 deletions sith/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
"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",
Expand Down

0 comments on commit 21b4a5d

Please sign in to comment.