Skip to content

Commit d6c8f60

Browse files
committed
Add UI to select event type for availability rule
1 parent 6722949 commit d6c8f60

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

‎frontend/src/routes/availability/+page.svelte‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
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';
44
import { prefs, fmtDate, weekDays } from '$lib/prefs';
55
import { t, tr, localeTag } from '$lib/i18n';
66
import { Button, buttonVariants } from '$lib/components/ui/button';
@@ -33,7 +33,7 @@
3333
}
3434
3535
// ── 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 }
3737
type DayState = { day_of_week: number; blocks: DayBlock[]; error: string }
3838
3939
let days: DayState[] = $state(
@@ -56,7 +56,7 @@
5656
for (const day of days) { day.blocks = []; day.error = ''; }
5757
for (const r of (res.items ?? [])) {
5858
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: '' });
6060
}
6161
for (const day of days) {
6262
day.blocks.sort((a, b) => a.start_time.localeCompare(b.start_time));
@@ -69,7 +69,7 @@
6969
}
7070
7171
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: '' });
7373
// Operate on the element AS STORED in the reactive array (a proxy), not the literal we
7474
// pushed — Svelte 5 proxies pushed objects, so mutating the local literal wouldn't update
7575
// the UI (the "saving…" stuck-until-refresh bug).
@@ -100,6 +100,7 @@
100100
block.saving = true;
101101
try {
102102
await api.patch(`/v1/availability-rules/${block.id}`, {
103+
event_type_id: block.event_type_id === null ? "null" : block.event_type_id,
103104
start_time: block.start_time,
104105
end_time: block.end_time
105106
});
@@ -274,7 +275,20 @@
274275
}
275276
}
276277
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+
277290
onMount(() => {
291+
loadEventTypes()
278292
loadRules();
279293
loadOverrides();
280294
});
@@ -288,6 +302,12 @@
288302
// Time-of-day options for the start/end selects, formatted per the user's prefs.
289303
let timeOptions = $derived(TIME_SLOTS.map((t) => ({ value: t, label: fmtTime(t) })));
290304
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+
}
291311
</script>
292312

293313
<ConfirmDialog
@@ -380,6 +400,26 @@
380400
{:else if block.error}
381401
<span class="text-xs text-destructive">{block.error}</span>
382402
{/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+
383423
<Tooltip.Root>
384424
<Tooltip.Trigger
385425
class={buttonVariants({ variant: 'ghost', size: 'icon' })}

0 commit comments

Comments
 (0)