refactor and corrections for PR

This commit is contained in:
NaNoMelo 2024-10-22 23:17:24 +02:00
parent 02c9cca682
commit 4719769e46
4 changed files with 40 additions and 35 deletions

View File

@ -51,5 +51,10 @@ class PermanencyController(ControllerBase):
exclude_none=True, exclude_none=True,
) )
@paginate(PageNumberPaginationExtra, page_size=100) @paginate(PageNumberPaginationExtra, page_size=100)
def fetch_permanancies(self, filters: Query[PermanencyFilterSchema]): def fetch_permanencies(self, filters: Query[PermanencyFilterSchema]):
return filters.filter(Permanency.objects.all()).distinct().order_by("-start") return (
filters.filter(Permanency.objects.all())
.distinct()
.order_by("-start")
.select_related("counter")
)

View File

@ -23,7 +23,7 @@
} }
} }
#activityGraph { #activityTimeGrid {
th { th {
border: none; border: none;
} }

View File

@ -3,12 +3,12 @@ import { exportToHtml } from "#core:utils/globals";
import { Calendar } from "@fullcalendar/core"; import { Calendar } from "@fullcalendar/core";
import timeGridPlugin from "@fullcalendar/timegrid"; import timeGridPlugin from "@fullcalendar/timegrid";
import { import {
type PermanencyFetchPermananciesData, type PermanencyFetchPermanenciesData,
type PermanencySchema, type PermanencySchema,
permanencyFetchPermanancies, permanencyFetchPermanencies,
} from "#openapi"; } from "#openapi";
interface ActivityChartConfig { interface ActivityTimeGridConfig {
canvas: HTMLCanvasElement; canvas: HTMLCanvasElement;
startDate: Date; startDate: Date;
counterId: number; counterId: number;
@ -27,18 +27,18 @@ interface EventInput {
title?: string; title?: string;
} }
exportToHtml("loadChart", loadChart); exportToHtml("loadActivityTimeGrid", loadActivityTimeGrid);
async function loadChart(options: ActivityChartConfig) { async function loadActivityTimeGrid(options: ActivityTimeGridConfig) {
const permanancies = await paginated(permanencyFetchPermanancies, { const permanencies = await paginated(permanencyFetchPermanencies, {
query: { query: {
counter: [options.counterId], counter: [options.counterId],
// biome-ignore lint/style/useNamingConvention: backend API uses snake_case // biome-ignore lint/style/useNamingConvention: backend API uses snake_case
took_place_after: options.startDate.toISOString(), took_place_after: options.startDate.toISOString(),
}, },
} as PermanencyFetchPermananciesData); } as PermanencyFetchPermanenciesData);
const events = getEvents(permanancies); const events = getEvents(permanencies);
const calendar = new Calendar(options.canvas, { const calendar = new Calendar(options.canvas, {
plugins: [timeGridPlugin], plugins: [timeGridPlugin],
@ -59,7 +59,7 @@ async function loadChart(options: ActivityChartConfig) {
if (options.startDate <= info.start) { if (options.startDate <= info.start) {
return; return;
} }
const newPerms = await paginated(permanencyFetchPermanancies, { const newPerms = await paginated(permanencyFetchPermanencies, {
query: { query: {
counter: [options.counterId], counter: [options.counterId],
// biome-ignore lint/style/useNamingConvention: backend API uses snake_case // biome-ignore lint/style/useNamingConvention: backend API uses snake_case
@ -67,10 +67,10 @@ async function loadChart(options: ActivityChartConfig) {
// biome-ignore lint/style/useNamingConvention: backend API uses snake_case // biome-ignore lint/style/useNamingConvention: backend API uses snake_case
start_before: info.endStr, start_before: info.endStr,
}, },
} as PermanencyFetchPermananciesData); } as PermanencyFetchPermanenciesData);
options.startDate = info.start; options.startDate = info.start;
calendar.addEventSource(getEvents(newPerms, false)); calendar.addEventSource(getEvents(newPerms, false));
permanancies.push(...newPerms); permanencies.push(...newPerms);
calendar.render(); calendar.render();
}); });
} }
@ -83,41 +83,41 @@ function roundToQuarter(date: Date, ceil: boolean) {
return result; return result;
} }
function convertPermanancyToOpeningTime(permanancy: PermanencySchema): OpeningTime { function convertPermanencyToOpeningTime(permanency: PermanencySchema): OpeningTime {
return { return {
start: roundToQuarter(new Date(permanancy.start), false), start: roundToQuarter(new Date(permanency.start), false),
end: roundToQuarter(new Date(permanancy.end ?? Date.now()), true), end: roundToQuarter(new Date(permanency.end ?? Date.now()), true),
}; };
} }
function getOpeningTimes(rawPermanancies: PermanencySchema[]) { function getOpeningTimes(rawPermanencies: PermanencySchema[]) {
const permanancies = rawPermanancies const permanencies = rawPermanencies
.map(convertPermanancyToOpeningTime) .map(convertPermanencyToOpeningTime)
.sort((a, b) => a.start.getTime() - b.start.getTime()); .sort((a, b) => a.start.getTime() - b.start.getTime());
const openingTimes: OpeningTime[] = []; const openingTimes: OpeningTime[] = [];
for (const permanancy of permanancies) { for (const permanency of permanencies) {
// if there are no opening times, add the first one // if there are no opening times, add the first one
if (openingTimes.length === 0) { if (openingTimes.length === 0) {
openingTimes.push(permanancy); openingTimes.push(permanency);
} else { } else {
const lastPermanancy = openingTimes[openingTimes.length - 1]; const lastPermanency = openingTimes[openingTimes.length - 1];
// if the new permanancy starts before the 15 minutes following the end of the last one, merge them // if the new permanency starts before the 15 minutes following the end of the last one, merge them
if (permanancy.start <= lastPermanancy.end) { if (permanency.start <= lastPermanency.end) {
lastPermanancy.end = new Date( lastPermanency.end = new Date(
Math.max(lastPermanancy.end.getTime(), permanancy.end.getTime()), Math.max(lastPermanency.end.getTime(), permanency.end.getTime()),
); );
} else { } else {
openingTimes.push(permanancy); openingTimes.push(permanency);
} }
} }
} }
return openingTimes; return openingTimes;
} }
function getEvents(permanancies: PermanencySchema[], currentWeek = true): EventInput[] { function getEvents(permanencies: PermanencySchema[], currentWeek = true): EventInput[] {
const openingTimes = getOpeningTimes(permanancies); const openingTimes = getOpeningTimes(permanencies);
const events: EventInput[] = []; const events: EventInput[] = [];
for (const openingTime of openingTimes) { for (const openingTime of openingTimes) {
let shift = false; let shift = false;
@ -125,7 +125,7 @@ function getEvents(permanancies: PermanencySchema[], currentWeek = true): EventI
const lastMonday = getLastMonday(); const lastMonday = getLastMonday();
shift = openingTime.end < lastMonday; shift = openingTime.end < lastMonday;
} }
// if permanancies took place last week (=before monday), // if permanencies took place last week (=before monday),
// -> display them in lightblue as part of the current week // -> display them in lightblue as part of the current week
events.push({ events.push({
start: shift ? shiftDateByDays(openingTime.start, 7) : openingTime.start, start: shift ? shiftDateByDays(openingTime.start, 7) : openingTime.start,

View File

@ -6,7 +6,7 @@
{% endblock %} {% endblock %}
{% block additional_js %} {% block additional_js %}
<script defer src="{{ static('webpack/graph-index.ts') }}"></script> <script defer src="{{ static('webpack/counter/permanencies/time-grid-index.ts') }}"></script>
{% endblock %} {% endblock %}
{%- block additional_css -%} {%- block additional_css -%}
@ -27,7 +27,7 @@
{% endif %} {% endif %}
</ul> </ul>
<h4>{% trans %}Last weeks opening times{% endtrans %}</h4> <h4>{% trans %}Last weeks opening times{% endtrans %}</h4>
<div id="activityGraph"></div> <div id="activityTimeGrid"></div>
<br/> <br/>
<br/> <br/>
{% endif %} {% endif %}
@ -49,8 +49,8 @@
{{super()}} {{super()}}
<script> <script>
window.addEventListener("DOMContentLoaded", () => { window.addEventListener("DOMContentLoaded", () => {
loadChart({ loadActivityTimeGrid({
canvas: document.getElementById("activityGraph"), canvas: document.getElementById("activityTimeGrid"),
// sets the start day to 7 days ago // sets the start day to 7 days ago
startDate: new Date(new Date().setDate(new Date().getDate() - 7)), startDate: new Date(new Date().setDate(new Date().getDate() - 7)),
counterId: {{ counter.id }}, counterId: {{ counter.id }},