|
1 | 1 | <script lang="ts"> |
2 | 2 | import { onMount } from 'svelte'; |
3 | | - import { api, type AvailabilityRule, type AvailabilityOverride } from '$lib/api'; |
| 3 | + import {api, type AvailabilityRule, type AvailabilityOverride, type EventType} from '$lib/api'; |
4 | 4 | import { prefs, fmtDate, weekDays } from '$lib/prefs'; |
5 | 5 | import { t, tr, localeTag } from '$lib/i18n'; |
6 | 6 | import { Button, buttonVariants } from '$lib/components/ui/button'; |
|
33 | 33 | } |
34 | 34 |
|
35 | 35 | // ── Weekly rules — 7-day model ──────────────────────────────────────────────── |
36 | | - type DayBlock = { id: string; start_time: string; end_time: string; saving: boolean; error: string } |
| 36 | + type DayBlock = { id: string; event_type_id: string, start_time: string; end_time: string; saving: boolean; error: string } |
37 | 37 | type DayState = { day_of_week: number; blocks: DayBlock[]; error: string } |
38 | 38 |
|
39 | 39 | let days: DayState[] = $state( |
|
56 | 56 | for (const day of days) { day.blocks = []; day.error = ''; } |
57 | 57 | for (const r of (res.items ?? [])) { |
58 | 58 | const day = days[r.day_of_week]; |
59 | | - if (day) day.blocks.push({ id: r.id, start_time: r.start_time, end_time: r.end_time, saving: false, error: '' }); |
| 59 | + if (day) day.blocks.push({ id: r.id, event_type_id: r.event_type_id ?? 'null', start_time: r.start_time, end_time: r.end_time, saving: false, error: '' }); |
60 | 60 | } |
61 | 61 | for (const day of days) { |
62 | 62 | day.blocks.sort((a, b) => a.start_time.localeCompare(b.start_time)); |
|
69 | 69 | } |
70 | 70 |
|
71 | 71 | async function addBlock(day: DayState) { |
72 | | - day.blocks.push({ id: '', start_time: '09:00', end_time: '17:00', saving: true, error: '' }); |
| 72 | + day.blocks.push({ id: '', event_type_id: 'null', start_time: '09:00', end_time: '17:00', saving: true, error: '' }); |
73 | 73 | // Operate on the element AS STORED in the reactive array (a proxy), not the literal we |
74 | 74 | // pushed — Svelte 5 proxies pushed objects, so mutating the local literal wouldn't update |
75 | 75 | // the UI (the "saving…" stuck-until-refresh bug). |
|
100 | 100 | block.saving = true; |
101 | 101 | try { |
102 | 102 | await api.patch(`/v1/availability-rules/${block.id}`, { |
| 103 | + event_type_id: block.event_type_id === null ? "null" : block.event_type_id, |
103 | 104 | start_time: block.start_time, |
104 | 105 | end_time: block.end_time |
105 | 106 | }); |
|
274 | 275 | } |
275 | 276 | } |
276 | 277 |
|
| 278 | + let eventTypes: {id: string, name: string}[] = [] |
| 279 | + async function loadEventTypes() { |
| 280 | + try { |
| 281 | + const res = await api.get<{ items: EventType[] }>('/v1/event-types'); |
| 282 | + for (const r of (res.items ?? [])) { |
| 283 | + eventTypes.push({id: r.id, name: r.name}) |
| 284 | + } |
| 285 | + } catch (e: any) { |
| 286 | +
|
| 287 | + } |
| 288 | + } |
| 289 | +
|
277 | 290 | onMount(() => { |
| 291 | + loadEventTypes() |
278 | 292 | loadRules(); |
279 | 293 | loadOverrides(); |
280 | 294 | }); |
|
288 | 302 | // Time-of-day options for the start/end selects, formatted per the user's prefs. |
289 | 303 | let timeOptions = $derived(TIME_SLOTS.map((t) => ({ value: t, label: fmtTime(t) }))); |
290 | 304 | function timeLabel(t: string) { return fmtTime(t); } |
| 305 | + function eventName(id: string | null) { |
| 306 | + if (id === null) { |
| 307 | + return {id: null, name: "None"} |
| 308 | + } |
| 309 | + return eventTypes.find((et => et.id == id)) |
| 310 | + } |
291 | 311 | </script> |
292 | 312 |
|
293 | 313 | <ConfirmDialog |
|
380 | 400 | {:else if block.error} |
381 | 401 | <span class="text-xs text-destructive">{block.error}</span> |
382 | 402 | {/if} |
| 403 | + |
| 404 | + <Select.Root |
| 405 | + type="single" |
| 406 | + bind:value={block.event_type_id} |
| 407 | + onValueChange={() => updateBlock(block)} |
| 408 | + disabled={block.saving || !block.id} |
| 409 | + > |
| 410 | + <Select.Trigger class="w-fit"> |
| 411 | + {#if block.event_type_id === "null"} |
| 412 | + All |
| 413 | + {:else} |
| 414 | + {eventName(block.event_type_id)?.name ?? block.event_type_id} |
| 415 | + {/if} |
| 416 | + </Select.Trigger> |
| 417 | + <Select.Content> |
| 418 | + <Select.Item value="null" label="All">All</Select.Item> |
| 419 | + {#each eventTypes as e}<Select.Item value={e.id} label={e.name}>{e.name}</Select.Item>{/each} |
| 420 | + </Select.Content> |
| 421 | + </Select.Root> |
| 422 | + |
383 | 423 | <Tooltip.Root> |
384 | 424 | <Tooltip.Trigger |
385 | 425 | class={buttonVariants({ variant: 'ghost', size: 'icon' })} |
|
0 commit comments