fix(frontend): fix routines list crash and AI save redirect

- Make Routine.steps optional in types.ts — list endpoint does not
  eager-load steps, so the field is absent from the JSON response
- Guard routine.steps?.length ?? 0 in routines list page to prevent
  SSR TypeError when steps is undefined
- Move redirect() outside try/catch in suggest save action; SvelteKit
  redirect throws internally and was being caught, causing a 500 error
  instead of navigating to the new routine

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Piotr Oleszczyk 2026-03-01 00:54:54 +01:00
parent 81b1cacc5c
commit 832676bcfa
3 changed files with 5 additions and 3 deletions

View file

@ -181,7 +181,7 @@ export interface Routine {
notes?: string; notes?: string;
created_at: string; created_at: string;
updated_at: string; updated_at: string;
steps: RoutineStep[]; steps?: RoutineStep[];
} }
export interface GroomingSchedule { export interface GroomingSchedule {

View file

@ -47,7 +47,7 @@
<Badge variant={routine.part_of_day === 'am' ? 'default' : 'secondary'}> <Badge variant={routine.part_of_day === 'am' ? 'default' : 'secondary'}>
{routine.part_of_day.toUpperCase()} {routine.part_of_day.toUpperCase()}
</Badge> </Badge>
<span class="text-sm">{routine.steps.length} steps</span> <span class="text-sm">{routine.steps?.length ?? 0} steps</span>
</div> </div>
{#if routine.notes} {#if routine.notes}
<span class="text-sm text-muted-foreground truncate max-w-xs">{routine.notes}</span> <span class="text-sm text-muted-foreground truncate max-w-xs">{routine.notes}</span>

View file

@ -74,8 +74,10 @@ export const actions: Actions = {
return fail(400, { error: 'Nieprawidłowy format kroków.' }); return fail(400, { error: 'Nieprawidłowy format kroków.' });
} }
let routineId: string;
try { try {
const routine = await createRoutine({ routine_date, part_of_day }); const routine = await createRoutine({ routine_date, part_of_day });
routineId = routine.id;
for (let i = 0; i < steps.length; i++) { for (let i = 0; i < steps.length; i++) {
const s = steps[i]; const s = steps[i];
await addRoutineStep(routine.id, { await addRoutineStep(routine.id, {
@ -87,10 +89,10 @@ export const actions: Actions = {
region: s.region || undefined region: s.region || undefined
}); });
} }
redirect(303, `/routines/${routine.id}`);
} catch (e) { } catch (e) {
return fail(500, { error: (e as Error).message }); return fail(500, { error: (e as Error).message });
} }
redirect(303, `/routines/${routineId}`);
}, },
saveBatch: async ({ request }) => { saveBatch: async ({ request }) => {