Implement DTOs, add managed state for schedule, export to txt

This commit is contained in:
2026-01-11 22:28:10 +02:00
parent 53f8695572
commit 92a9c6d704
11 changed files with 254 additions and 70 deletions

View File

@@ -1,6 +1,5 @@
// state.svelte.ts
import { CalendarDate, getDayOfWeek } from "@internationalized/date";
import { trace } from "@tauri-apps/plugin-log";
export interface Resident {
id: string;
@@ -9,7 +8,7 @@ export interface Resident {
manualShifts: CalendarDate[];
maxShifts: number | undefined;
allowedTypes: string[];
hasReducedLoad: boolean;
reducedLoad: boolean;
}
export interface ForbiddenPair {
@@ -30,6 +29,8 @@ export class RotaState {
daysArray = $derived(Array.from({ length: this.projectMonthDays }, (_, i) => i + 1));
emptySlots = $derived(Array.from({ length: getDayOfWeek(this.projectMonth, "en-GB") }));
solution = $state<MonthlySchedule>({} as MonthlySchedule);
addResident() {
this.residents.push({
id: crypto.randomUUID(),
@@ -37,13 +38,17 @@ export class RotaState {
negativeShifts: [],
manualShifts: [],
maxShifts: undefined,
allowedTypes: ["OpenAsFirst", "OpenAsSecond", "Closed"],
hasReducedLoad: false
allowedTypes: ["Closed", "OpenFirst", "OpenSecond"],
reducedLoad: false
});
}
removeResident(id: string) {
this.residents = this.residents.filter((p) => p.id !== id);
this.residents = this.residents.filter((r) => r.id !== id);
}
findResident(id: string) {
return this.residents.find((r) => r.id === id);
}
addForbiddenPair() {
@@ -57,10 +62,61 @@ export class RotaState {
removeForbiddenPair(index: number) {
this.forbiddenPairs.splice(index, 1);
}
toDTO(): UserConfigDTO {
return {
month: this.selectedMonth,
year: this.selectedYear,
holidays: this.holidays.map((d) => d.day),
residents: this.residents.map((r) => ({
id: r.id,
name: r.name,
negativeShifts: r.negativeShifts.map((d) => d.day),
manualShifts: r.manualShifts.map((s) => ({
day: s.day,
position: "First"
})),
maxShifts: r.maxShifts ?? null,
allowedTypes: r.allowedTypes as ShiftType[],
reducedLoad: r.reducedLoad
})),
toxic_pairs: this.forbiddenPairs.map((pair) => [pair.id1, pair.id2])
};
}
}
export const rota = new RotaState();
export type MonthlyScheduleDTO = {
schedule: Record<number, Record<ShiftPosition, string>>;
};
export type UserConfigDTO = {
month: number;
year: number;
holidays: Array<number>;
residents: Array<ResidentDTO>;
toxic_pairs: Array<[string, string]>;
};
export type ResidentDTO = {
id: string;
name: string;
negativeShifts: Array<number>;
manualShifts: Array<{ day: number; position: ShiftPosition }>;
maxShifts: number | null;
allowedTypes: Array<ShiftType>;
reducedLoad: boolean;
};
export type ShiftType = "Closed" | "OpenFirst" | "OpenSecond";
export type ShiftPosition = "First" | "Second";
export type SlotKey = `${number}-${"First" | "Second"}`;
export type MonthlySchedule = { [key: SlotKey]: string };
export const steps = [
{
id: 1,