Refactor to use ClientSchedule on client

Use the ClientSchedule data structure for deserialising and tracking
edit state on the client instead of trying to directly deal with the
ApiSchedule type which is not build for ease of edits or rendering.
This commit is contained in:
Hornwitser 2025-06-14 19:22:53 +02:00
parent ce9f758f84
commit bb450fd583
15 changed files with 488 additions and 1297 deletions

View file

@ -1,11 +1,5 @@
<template>
<main v-if="schedule.deleted">
<h1>Error</h1>
<p>
Schedule has been deleted.
</p>
</main>
<main v-else>
<main>
<h1>Edit</h1>
<label>
Crew Filter:
@ -37,14 +31,15 @@
:selected="locationFilter === undefined"
>&lt;All locations&gt;</option>
<option
v-for="location in schedule.locations?.filter(l => !l.deleted)"
v-for="location in schedule.locations.values()"
:key="location.id"
:value="location.id"
:disabled="location.deleted"
:selected="locationFilter === location.id"
>{{ location.name }}</option>
</select>
</label>
<ScheduleTable :edit="true" :location="locationFilter" :eventSlotFilter :shiftSlotFilter />
<ScheduleTable :edit="true" :locationId="locationFilter" :eventSlotFilter :shiftSlotFilter />
<h2>Events</h2>
<EventsTable :edit="true" />
<h2>Roles</h2>
@ -60,9 +55,10 @@
:selected="roleFilter === undefined"
>&lt;All roles&gt;</option>
<option
v-for="role in schedule.roles?.filter(r => !r.deleted)"
v-for="role in schedule.roles.values()"
:key="role.id"
:value="role.id"
:disabled="role.deleted"
:selected="roleFilter === role.id"
>{{ role.name }}</option>
</select>
@ -70,12 +66,17 @@
<ShiftScheduleTable :edit="true" :roleId="roleFilter" :eventSlotFilter :shiftSlotFilter />
<h2>Shifts</h2>
<ShiftsTable :edit="true" :roleId="roleFilter" />
<p v-if="schedule.modified">
Changes are not saved yet.
<button
type="button"
@click="saveChanges"
>Save Changes</button>
</p>
</main>
</template>
<script lang="ts" setup>
import type { ApiScheduleEventSlot, ApiScheduleShiftSlot } from '~/shared/types/api';
definePageMeta({
middleware: ["authenticated"],
allowedAccountTypes: ["crew", "admin"],
@ -101,14 +102,14 @@ const eventSlotFilter = computed(() => {
return () => true;
}
const cid = parseInt(crewFilter.value);
return (slot: ApiScheduleEventSlot) => slot.assigned?.some(id => id === cid) || false;
return (slot: ClientScheduleEventSlot) => slot.assigned.has(cid);
});
const shiftSlotFilter = computed(() => {
if (crewFilter.value === undefined || !accountStore.valid) {
return () => true;
}
const cid = parseInt(crewFilter.value);
return (slot: ApiScheduleShiftSlot) => slot.assigned?.some(id => id === cid) || false;
return (slot: ClientScheduleShiftSlot) => slot.assigned.has(cid);
});
const locationFilter = computed({
@ -132,4 +133,16 @@ const roleFilter = computed({
},
}),
});
async function saveChanges() {
try {
await $fetch("/api/schedule", {
method: "PATCH",
body: schedule.value.toApi(true),
});
} catch (err: any) {
console.error(err);
alert(err?.data?.message ?? err.message);
}
}
</script>