Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 381285c

Browse files
committed
fix: filter calendar times based on priority
1 parent fb153de commit 381285c

3 files changed

Lines changed: 39 additions & 19 deletions

File tree

apps/admin-ui/src/spa/recurring-reservations/application-rounds/[id]/allocation/AllocationCalendar.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ import {
2525
encodeTimeSlot,
2626
type RelatedSlot,
2727
isInsideCell,
28+
convertPriorityFilter,
2829
} from "./modules/applicationRoundAllocation";
2930
import {
3031
useFocusAllocatedSlot,
3132
useFocusApplicationEvent,
3233
useSlotSelection,
3334
} from "./hooks";
35+
import { useSearchParams } from "react-router-dom";
3436

3537
type Props = {
3638
applicationSections: ApplicationSectionNode[] | null;
@@ -314,24 +316,39 @@ export function AllocationCalendar({
314316
)
315317
);
316318

319+
const [searchParams] = useSearchParams();
320+
const priorityFilter = searchParams.getAll("priority");
321+
const priorityFilterSanitized = convertPriorityFilter(priorityFilter);
322+
317323
const [focused] = useFocusApplicationEvent();
318-
const focusedApplicationEvent = applicationSections?.find(
319-
(ae) => ae.pk === focused
320-
);
324+
const aes = applicationSections?.map((ae) => {
325+
// if priority filter is set, we need to filter what is shown in calendar based on that
326+
// these are included in the backend request because we want to show them elsewhere, but not in the calendar
327+
if (priorityFilter.length > 0) {
328+
return {
329+
...ae,
330+
suitableTimeRanges: ae.suitableTimeRanges?.filter((tr) =>
331+
priorityFilterSanitized.find((p) => p === tr.priority)
332+
),
333+
};
334+
}
335+
return ae;
336+
});
337+
338+
const focusedApplicationEvent = aes?.find((ae) => ae.pk === focused);
339+
321340
const [focusedAllocated] = useFocusAllocatedSlot();
322341

323342
const data = WEEKDAYS.map((day) => {
324343
const isNotHandled = (ae: ApplicationSectionNode) =>
325344
ae.status !== ApplicationSectionStatusChoice.Handled;
326345

327346
// Only show allocated that match the unit and day
328-
const timeslots = filterNonNullable(applicationSections)
347+
const timeslots = filterNonNullable(aes)
329348
.filter(isNotHandled)
330349
.filter((ae) => ae.suitableTimeRanges?.some((tr) => isDay(tr, day)));
331350

332-
const resUnits = applicationSections?.flatMap(
333-
(ae) => ae.reservationUnitOptions
334-
);
351+
const resUnits = aes?.flatMap((ae) => ae.reservationUnitOptions);
335352

336353
const allocated = filterNonNullable(resUnits)
337354
.filter((a) => a.allocatedTimeSlots?.some((ts) => isDay(ts, day)))

apps/admin-ui/src/spa/recurring-reservations/application-rounds/[id]/allocation/index.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
type UnitNode,
1414
ApplicantTypeChoice,
1515
ApplicationRoundStatusChoice,
16-
Priority,
1716
type QueryApplicationRoundArgs,
1817
type QueryAffectingAllocatedTimeSlotsArgs,
1918
type ApplicationRoundNode,
@@ -44,6 +43,7 @@ import {
4443
} from "./queries";
4544
import { AllocationPageContent } from "./ApplicationEvents";
4645
import { ComboboxFilter, SearchFilter } from "@/component/QueryParamFilters";
46+
import { convertPriorityFilter } from "./modules/applicationRoundAllocation";
4747

4848
const MAX_RES_UNIT_NAME_LENGTH = 35;
4949

@@ -259,17 +259,7 @@ function ApplicationRoundAllocation({
259259

260260
// NOTE sanitize all other query filters similar to this
261261
// backend returns an error on invalid filter values, but user can cause them by manipulating the url
262-
const priorityFilterSanitized = priorityFilter
263-
?.map((x) => Number(x))
264-
.reduce<Array<Priority.Secondary | Priority.Primary>>((acc, x) => {
265-
if (x === 200) {
266-
return [...acc, Priority.Secondary];
267-
} else if (x === 300) {
268-
return [...acc, Priority.Primary];
269-
}
270-
return acc;
271-
}, []);
272-
262+
const priorityFilterSanitized = convertPriorityFilter(priorityFilter);
273263
const priorityFilterQuery =
274264
priorityFilterSanitized.length > 0 ? priorityFilterSanitized : null;
275265
const ageGroupFilterQuery = ageGroupFilter

apps/admin-ui/src/spa/recurring-reservations/application-rounds/[id]/allocation/modules/applicationRoundAllocation.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,16 @@ export function isInsideCell(
315315
const endMinutes = (end === 0 ? 24 : end) * 60;
316316
return cellTime >= beginMinutes && cellTime < endMinutes;
317317
}
318+
319+
export function convertPriorityFilter(values: string[]): Priority[] {
320+
return values
321+
?.map((x) => Number(x))
322+
.reduce<Array<Priority>>((acc, x) => {
323+
if (x === 200) {
324+
return [...acc, Priority.Secondary];
325+
} else if (x === 300) {
326+
return [...acc, Priority.Primary];
327+
}
328+
return acc;
329+
}, []);
330+
}

0 commit comments

Comments
 (0)