Skip to content

Commit eb67e2d

Browse files
authored
Merge pull request #248 from kpalang/feature/assign-availability-rules
feat: Assign event types to availability rules
2 parents 8ed3078 + fe75122 commit eb67e2d

4 files changed

Lines changed: 73 additions & 13 deletions

File tree

‎frontend/src/lib/i18n/en.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@
241241
"addHours": "+ Add hours",
242242
"addBlock": "Add block",
243243
"saving": "saving…",
244+
"eventTypes": {
245+
"appliesTo": "Applies to",
246+
"allEventTypes": "All event types"
247+
},
244248
"blockError": {
245249
"endAfterStart": "End must be after start"
246250
},

‎frontend/src/lib/i18n/fr.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@
241241
"addHours": "+ Ajouter des horaires",
242242
"addBlock": "Ajouter un bloc",
243243
"saving": "enregistrement…",
244+
"eventTypes": {
245+
"appliesTo": "S'applique à",
246+
"allEventTypes": "Tous les types d'événements"
247+
},
244248
"blockError": {
245249
"endAfterStart": "La fin doit être après le début"
246250
},

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

Lines changed: 49 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,21 @@
274275
}
275276
}
276277
278+
let eventTypes = $state<{id: string; name: string}[]>([])
279+
async function loadEventTypes() {
280+
try {
281+
const res = await api.get<{ items: EventType[] }>('/v1/event-types');
282+
eventTypes = (res.items ?? []).map((et) => ({
283+
id: et.id,
284+
name: et.name,
285+
}))
286+
} catch (e: any) {
287+
288+
}
289+
}
290+
277291
onMount(() => {
292+
loadEventTypes()
278293
loadRules();
279294
loadOverrides();
280295
});
@@ -288,6 +303,12 @@
288303
// Time-of-day options for the start/end selects, formatted per the user's prefs.
289304
let timeOptions = $derived(TIME_SLOTS.map((t) => ({ value: t, label: fmtTime(t) })));
290305
function timeLabel(t: string) { return fmtTime(t); }
306+
function eventName(id: string | null) {
307+
if (id === null) {
308+
return {id: null, name: "None"}
309+
}
310+
return eventTypes.find((et => et.id == id))
311+
}
291312
</script>
292313

293314
<ConfirmDialog
@@ -380,6 +401,30 @@
380401
{:else if block.error}
381402
<span class="text-xs text-destructive">{block.error}</span>
382403
{/if}
404+
405+
<span class="text-muted-foreground">|</span>
406+
<div class="text-sm font-normal text-muted-foreground">
407+
{$t('availability.eventTypes.appliesTo')}:
408+
</div>
409+
<Select.Root
410+
type="single"
411+
bind:value={block.event_type_id}
412+
onValueChange={() => updateBlock(block)}
413+
disabled={block.saving || !block.id}
414+
>
415+
<Select.Trigger class="w-fit">
416+
{#if block.event_type_id === "null"}
417+
{$t('availability.eventTypes.allEventTypes')}
418+
{:else}
419+
{eventName(block.event_type_id)?.name ?? block.event_type_id}
420+
{/if}
421+
</Select.Trigger>
422+
<Select.Content>
423+
<Select.Item value="null" label="All">{$t('availability.eventTypes.allEventTypes')}</Select.Item>
424+
{#each eventTypes as e}<Select.Item value={e.id} label={e.name}>{e.name}</Select.Item>{/each}
425+
</Select.Content>
426+
</Select.Root>
427+
383428
<Tooltip.Root>
384429
<Tooltip.Trigger
385430
class={buttonVariants({ variant: 'ghost', size: 'icon' })}

‎internal/handler/availability.go‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ func (h *Handler) UpdateAvailabilityRule(w http.ResponseWriter, r *http.Request)
123123
r.Body = http.MaxBytesReader(w, r.Body, 32<<10)
124124

125125
var req struct {
126-
DayOfWeek *int `json:"day_of_week"`
127-
StartTime *string `json:"start_time"`
128-
EndTime *string `json:"end_time"`
126+
EventTypeId json.RawMessage `json:"event_type_id"` // `nil` = leave alone, `null` = clear
127+
DayOfWeek *int `json:"day_of_week"`
128+
StartTime *string `json:"start_time"`
129+
EndTime *string `json:"end_time"`
129130
}
130131
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
131132
h.writeError(w, http.StatusBadRequest, "invalid JSON")
@@ -137,11 +138,10 @@ func (h *Handler) UpdateAvailabilityRule(w http.ResponseWriter, r *http.Request)
137138
}
138139

139140
var current availRuleJSON
140-
var etID sql.NullString
141141
err := h.db.QueryRowContext(r.Context(),
142142
`SELECT id, event_type_id, day_of_week, start_time, end_time
143143
FROM availability_rules WHERE id = ? AND user_id = ?`, id, user.ID).
144-
Scan(&current.ID, &etID, &current.DayOfWeek, &current.StartTime, &current.EndTime)
144+
Scan(&current.ID, &current.EventTypeID, &current.DayOfWeek, &current.StartTime, &current.EndTime)
145145
if err == sql.ErrNoRows {
146146
h.writeError(w, http.StatusNotFound, "rule not found")
147147
return
@@ -151,8 +151,15 @@ func (h *Handler) UpdateAvailabilityRule(w http.ResponseWriter, r *http.Request)
151151
h.writeError(w, http.StatusInternalServerError, "internal error")
152152
return
153153
}
154-
if etID.Valid {
155-
current.EventTypeID = &etID.String
154+
155+
if req.EventTypeId != nil {
156+
var etId *string
157+
if err := json.Unmarshal(req.EventTypeId, &etId); err != nil {
158+
h.writeError(w, http.StatusBadRequest, "event_type_id must be a string")
159+
return
160+
}
161+
162+
current.EventTypeID = etId
156163
}
157164

158165
if req.DayOfWeek != nil {
@@ -174,8 +181,8 @@ func (h *Handler) UpdateAvailabilityRule(w http.ResponseWriter, r *http.Request)
174181
}
175182

176183
_, err = h.db.ExecContext(r.Context(),
177-
`UPDATE availability_rules SET day_of_week=?, start_time=?, end_time=? WHERE id=? AND user_id=?`,
178-
current.DayOfWeek, current.StartTime, current.EndTime, id, user.ID)
184+
`UPDATE availability_rules SET event_type_id=?, day_of_week=?, start_time=?, end_time=? WHERE id=? AND user_id=?`,
185+
current.EventTypeID, current.DayOfWeek, current.StartTime, current.EndTime, id, user.ID)
179186
if err != nil {
180187
if db.IsUniqueViolation(err) {
181188
h.writeError(w, http.StatusConflict, "a rule for this day and time already exists")

0 commit comments

Comments
 (0)