Add account based filtering of the schedule
Some checks failed
/ build (push) Has been cancelled
/ deploy (push) Has been cancelled

Implement personal filtering of the schedule based on events marked as
being interested in and filtering based on assigned crew for events.
This commit is contained in:
Hornwitser 2025-03-15 22:47:32 +01:00
parent 89b1d2a547
commit 399a4d2ca5
6 changed files with 197 additions and 46 deletions

View file

@ -1,6 +1,6 @@
<template> <template>
<div> <div>
<Timetable :schedule="schedulePreview" /> <Timetable :schedule="schedulePreview" :eventSlotFilter :shiftSlotFilter />
<table> <table>
<thead> <thead>
<tr> <tr>
@ -220,13 +220,15 @@
<script lang="ts" setup> <script lang="ts" setup>
import { DateTime, Duration } from 'luxon'; import { DateTime, Duration } from 'luxon';
import type { ChangeRecord, Schedule, ScheduleEvent, ScheduleLocation, TimeSlot } from '~/shared/types/schedule'; import type { ChangeRecord, Schedule, ScheduleEvent, ScheduleLocation, ShiftSlot, TimeSlot } from '~/shared/types/schedule';
import { applyChangeArray } from '~/shared/utils/changes'; import { applyChangeArray } from '~/shared/utils/changes';
import { enumerate, pairs, toId } from '~/shared/utils/functions'; import { enumerate, pairs, toId } from '~/shared/utils/functions';
const props = defineProps<{ const props = defineProps<{
edit?: boolean, edit?: boolean,
location?: string, location?: string,
eventSlotFilter?: (slot: TimeSlot) => boolean,
shiftSlotFilter?: (slot: ShiftSlot) => boolean,
}>(); }>();
interface EventSlot { interface EventSlot {
@ -625,6 +627,8 @@ const eventSlots = computed(() => {
const data: (EventSlot | Gap)[] = []; const data: (EventSlot | Gap)[] = [];
for (const event of schedule.value.events) { for (const event of schedule.value.events) {
for (const slot of event.slots) { for (const slot of event.slots) {
if (props.eventSlotFilter && !props.eventSlotFilter(slot))
continue;
for (const location of slot.locations) { for (const location of slot.locations) {
if (props.location !== undefined && location !== props.location) if (props.location !== undefined && location !== props.location)
continue; continue;

View file

@ -1,6 +1,6 @@
<template> <template>
<div> <div>
<Timetable :schedule="schedulePreview" /> <Timetable :schedule="schedulePreview" :eventSlotFilter :shiftSlotFilter />
<table> <table>
<thead> <thead>
<tr> <tr>
@ -218,13 +218,15 @@
<script lang="ts" setup> <script lang="ts" setup>
import { DateTime, Duration } from 'luxon'; import { DateTime, Duration } from 'luxon';
import type { ChangeRecord, Schedule, Shift, Role, ShiftSlot as ShiftTimeSlot} from '~/shared/types/schedule'; import type { ChangeRecord, Schedule, Shift, Role, ShiftSlot as ShiftTimeSlot, TimeSlot} from '~/shared/types/schedule';
import { applyChangeArray } from '~/shared/utils/changes'; import { applyChangeArray } from '~/shared/utils/changes';
import { enumerate, pairs, toId } from '~/shared/utils/functions'; import { enumerate, pairs, toId } from '~/shared/utils/functions';
const props = defineProps<{ const props = defineProps<{
edit?: boolean, edit?: boolean,
role?: string, role?: string,
eventSlotFilter?: (slot: TimeSlot) => boolean,
shiftSlotFilter?: (slot: ShiftTimeSlot) => boolean,
}>(); }>();
interface ShiftSlot { interface ShiftSlot {
@ -608,6 +610,8 @@ const shiftSlots = computed(() => {
if (props.role !== undefined && shift.role !== props.role) if (props.role !== undefined && shift.role !== props.role)
continue; continue;
for (const slot of shift.slots) { for (const slot of shift.slots) {
if (props.shiftSlotFilter && !props.shiftSlotFilter(slot))
continue;
data.push({ data.push({
type: "slot", type: "slot",
id: slot.id, id: slot.id,

View file

@ -56,36 +56,40 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr v-for="location in schedule.locations" :key="location.id"> <template v-for="location in schedule.locations" :key="location.id">
<th>{{ location.name }}</th> <tr v-if="locationRows.has(location.id)">
<td <th>{{ location.name }}</th>
v-for="row, index in locationRows.get(location.id)"
:key="index"
:colSpan="row.span"
:class='{"event": row.slots.size, "crew": row.crew }'
:title="row.title"
>
{{ row.title }}
</td>
</tr>
<template v-if="schedule.roles">
<tr>
<th>Shifts</th>
<td :colSpan="totalColumns"></td>
</tr>
<tr v-for="role in schedule.roles" :key="role.id">
<th>{{ role.name }}</th>
<td <td
v-for="row, index in roleRows.get(role.id)" v-for="row, index in locationRows.get(location.id)"
:key="index" :key="index"
:colSpan="row.span" :colSpan="row.span"
:class='{"shift": row.slots.size }' :class='{"event": row.slots.size, "crew": row.crew }'
:title="row.title" :title="row.title"
> >
{{ row.title }} {{ row.title }}
</td> </td>
</tr> </tr>
</template> </template>
<template v-if="schedule.roles">
<tr>
<th>Shifts</th>
<td :colSpan="totalColumns"></td>
</tr>
<template v-for="role in schedule.roles" :key="role.id">
<tr v-if="roleRows.has(role.id)">
<th>{{ role.name }}</th>
<td
v-for="row, index in roleRows.get(role.id)"
:key="index"
:colSpan="row.span"
:class='{"shift": row.slots.size }'
:title="row.title"
>
{{ row.title }}
</td>
</tr>
</template>
</template>
</tbody> </tbody>
</table> </table>
</figure> </figure>
@ -129,9 +133,9 @@ type Stretch = {
spans: Span[]; spans: Span[];
} }
function* edgesFromEvents(events: Iterable<ScheduleEvent>): Generator<Edge> { function* edgesFromEvents(events: Iterable<ScheduleEvent>, filter = (slot: TimeSlot) => true): Generator<Edge> {
for (const event of events) { for (const event of events) {
for (const slot of event.slots) { for (const slot of event.slots.filter(filter)) {
if (slot.start > slot.end) { if (slot.start > slot.end) {
throw new Error(`Slot ${slot.id} ends before it starts.`); throw new Error(`Slot ${slot.id} ends before it starts.`);
} }
@ -141,9 +145,9 @@ function* edgesFromEvents(events: Iterable<ScheduleEvent>): Generator<Edge> {
} }
} }
function* edgesFromShifts(shifts: Iterable<Shift>): Generator<Edge> { function* edgesFromShifts(shifts: Iterable<Shift>, filter = (slot: ShiftSlot) => true): Generator<Edge> {
for (const shift of shifts) { for (const shift of shifts) {
for (const slot of shift.slots) { for (const slot of shift.slots.filter(filter)) {
if (slot.start > slot.end) { if (slot.start > slot.end) {
throw new Error(`Slot ${slot.id} ends before it starts.`); throw new Error(`Slot ${slot.id} ends before it starts.`);
} }
@ -470,19 +474,27 @@ function tableElementsFromStretches(
columnGroups, columnGroups,
dayHeaders: dayHeaders.filter(day => day.span), dayHeaders: dayHeaders.filter(day => day.span),
hourHeaders: hourHeaders.filter(hour => hour.span), hourHeaders: hourHeaders.filter(hour => hour.span),
locationRows: new Map([...locationRows].map(([id, cells]) => [id, cells.filter(cell => cell.span)])), locationRows: new Map([...locationRows]
roleRows: new Map([...roleRows].map(([id, cells]) => [id, cells.filter(cell => cell.span)])), .filter(([_, cells]) => cells.some(cell => cell.slots.size))
.map(([id, cells]) => [id, cells.filter(cell => cell.span)]))
,
roleRows: new Map([...roleRows]
.filter(([_, cells]) => cells.some(cell => cell.slots.size))
.map(([id, cells]) => [id, cells.filter(cell => cell.span)]))
,
eventBySlotId, eventBySlotId,
}; };
} }
const props = defineProps<{ const props = defineProps<{
schedule: Schedule, schedule: Schedule,
eventSlotFilter?: (slot: TimeSlot) => boolean,
shiftSlotFilter?: (slot: ShiftSlot) => boolean,
}>(); }>();
const schedule = computed(() => props.schedule); const schedule = computed(() => props.schedule);
const junctions = computed(() => junctionsFromEdges([ const junctions = computed(() => junctionsFromEdges([
...edgesFromEvents(schedule.value.events), ...edgesFromEvents(schedule.value.events, props.eventSlotFilter),
...edgesFromShifts(schedule.value.rota ?? []), ...edgesFromShifts(schedule.value.rota ?? [], props.shiftSlotFilter),
])); ]));
const stretches = computed(() => [ const stretches = computed(() => [
...stretchesFromSpans( ...stretchesFromSpans(

View file

@ -1,6 +1,23 @@
<template> <template>
<main> <main>
<h1>Edit</h1> <h1>Edit</h1>
<label>
Crew Filter:
<select
v-model="crewFilter"
>
<option
:value="undefined"
:selected="crewFilter === undefined"
>&lt;All Crew&gt;</option>
<option
v-for="account in accounts?.filter(a => a.type === 'crew' || a.type === 'admin')"
:key="account.id"
:value="String(account.id)"
:selected="crewFilter === String(account.id)"
>{{ account.name }}</option>
</select>
</label>
<h2>Locations</h2> <h2>Locations</h2>
<LocationsTable :edit="isAdmin" /> <LocationsTable :edit="isAdmin" />
<h2>Schedule</h2> <h2>Schedule</h2>
@ -21,7 +38,7 @@
>{{ location.name }}</option> >{{ location.name }}</option>
</select> </select>
</label> </label>
<ScheduleTable :edit="true" :location="locationFilter" /> <ScheduleTable :edit="true" :location="locationFilter" :eventSlotFilter :shiftSlotFilter />
<h2>Events</h2> <h2>Events</h2>
<EventsTable :edit="true" /> <EventsTable :edit="true" />
<h2>Roles</h2> <h2>Roles</h2>
@ -44,14 +61,14 @@
>{{ role.name }}</option> >{{ role.name }}</option>
</select> </select>
</label> </label>
<ShiftScheduleTable :edit="true" :role="roleFilter" /> <ShiftScheduleTable :edit="true" :role="roleFilter" :eventSlotFilter :shiftSlotFilter />
<h2>Shifts</h2> <h2>Shifts</h2>
<ShiftsTable :edit="true" :role="roleFilter" /> <ShiftsTable :edit="true" :role="roleFilter" />
</main> </main>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import type { LocationQueryValue } from 'vue-router'; import type { ShiftSlot, TimeSlot } from '~/shared/types/schedule';
definePageMeta({ definePageMeta({
middleware: ["authenticated"], middleware: ["authenticated"],
@ -59,16 +76,34 @@ definePageMeta({
}); });
const schedule = await useSchedule(); const schedule = await useSchedule();
const { data: accounts } = await useAccounts();
function queryToString(item?: null | LocationQueryValue | LocationQueryValue[]) {
if (item === null)
return "";
if (item instanceof Array)
return queryToString(item[0])
return item;
}
const route = useRoute(); const route = useRoute();
const crewFilter = computed({
get: () => queryToString(route.query.crew),
set: (value: string | undefined) => navigateTo({
path: route.path,
query: {
...route.query,
crew: value,
},
}),
});
const eventSlotFilter = computed(() => {
if (crewFilter.value === undefined || !session.value) {
return () => true;
}
const cid = parseInt(crewFilter.value);
return (slot: TimeSlot) => slot.assigned?.some(id => id === cid) || false;
});
const shiftSlotFilter = computed(() => {
if (crewFilter.value === undefined || !session.value) {
return () => true;
}
const cid = parseInt(crewFilter.value);
return (slot: ShiftSlot) => slot.assigned?.some(id => id === cid) || false;
});
const locationFilter = computed({ const locationFilter = computed({
get: () => queryToString(route.query.location), get: () => queryToString(route.query.location),
set: (value: string | undefined) => navigateTo({ set: (value: string | undefined) => navigateTo({

View file

@ -12,10 +12,38 @@
Check out your <NuxtLink to="/account/settings">Account Setting</NuxtLink> to set up notifications for changes to schedule. Check out your <NuxtLink to="/account/settings">Account Setting</NuxtLink> to set up notifications for changes to schedule.
</p> </p>
<h2>Schedule</h2> <h2>Schedule</h2>
<Timetable :schedule /> <label v-if="session">
Filter:
<select
v-model="filter"
>
<option
:value="undefined"
:selected="filter === undefined"
>&lt;All events&gt;</option>
<option
value="my-schedule"
:selected='filter === "my-schedule"'
>My Schedule</option>
<option
v-if="isCrew"
value="assigned"
:selected='filter === "assigned"'
>Assigned to Me</option>
<optgroup v-if="isCrew && accounts" label="Crew">
<option
v-for="account in accounts.filter(a => a.type === 'crew' || a.type === 'admin')"
:key="account.id"
:value="`crew-${account.id}`"
:selected="filter === `crew-${account.id}`"
>{{ account.name }}</option>
</optgroup>
</select>
</label>
<Timetable :schedule :eventSlotFilter :shiftSlotFilter />
<EventsEdit /> <EventsEdit />
<h2>Events</h2> <h2>Events</h2>
<EventCard v-for="event in schedule.events" :event/> <EventCard v-for="event in schedule.events.filter(e => e.slots.some(eventSlotFilter))" :event/>
<h2>Locations</h2> <h2>Locations</h2>
<ul> <ul>
<li v-for="location in schedule.locations" :key="location.id"> <li v-for="location in schedule.locations" :key="location.id">
@ -27,6 +55,65 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import type { ShiftSlot, TimeSlot } from '~/shared/types/schedule';
const { data: session } = await useAccountSession(); const { data: session } = await useAccountSession();
const { data: accounts } = await useAccounts();
const schedule = await useSchedule(); const schedule = await useSchedule();
const isCrew = computed(() => (
session.value?.account?.type === "crew"
|| session.value?.account?.type === "admin"
));
const route = useRoute();
const filter = computed({
get: () => queryToString(route.query.filter),
set: (value: string | undefined) => navigateTo({
path: route.path,
query: {
...route.query,
filter: value,
},
}),
});
const eventSlotFilter = computed(() => {
if (filter.value === undefined || !session.value) {
return () => true;
}
const aid = session.value?.account?.id;
if (filter.value === "my-schedule") {
const ids = new Set(session.value?.account?.interestedIds);
for (const event of schedule.value.events) {
if (ids.has(event.id)) {
for (const slot of event.slots) {
ids.add(slot.id);
}
}
}
return (slot: TimeSlot) => ids.has(slot.id) || slot.assigned?.some(id => id === aid) || false;
}
if (filter.value === "assigned") {
return (slot: TimeSlot) => slot.assigned?.some(id => id === aid) || false;
}
if (filter.value.startsWith("crew-")) {
const cid = parseInt(filter.value.slice(5));
return (slot: TimeSlot) => slot.assigned?.some(id => id === cid) || false;
}
return () => false;
});
const shiftSlotFilter = computed(() => {
if (filter.value === undefined || !session.value) {
return () => true;
}
if (filter.value === "my-schedule" || filter.value === "assigned") {
const aid = session.value?.account?.id;
return (slot: ShiftSlot) => slot.assigned?.some(id => id === aid) || false;
}
if (filter.value.startsWith("crew-")) {
const cid = parseInt(filter.value.slice(5));
return (slot: ShiftSlot) => slot.assigned?.some(id => id === cid) || false;
}
return () => false;
});
</script> </script>

9
utils/functions.ts Normal file
View file

@ -0,0 +1,9 @@
import type { LocationQueryValue } from 'vue-router';
export function queryToString(item?: null | LocationQueryValue | LocationQueryValue[]) {
if (item === null)
return "";
if (item instanceof Array)
return queryToString(item[0])
return item;
}