Add event editor
This commit is contained in:
1 parent
c0bb12cee2
commit
e270c5a39f
1 file changed
+132
@@ -0,0 +1,132 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { KINDS, type CalEvent, type EventValues as Values } from '../lib/api';
|
||||||
|
import { untrack } from 'svelte';
|
||||||
|
import { app } from '../lib/state.svelte';
|
||||||
|
|
||||||
|
let {
|
||||||
|
event = null,
|
||||||
|
defaults = {},
|
||||||
|
onsave,
|
||||||
|
oncancel,
|
||||||
|
ondelete = null,
|
||||||
|
}: {
|
||||||
|
event?: CalEvent | null;
|
||||||
|
defaults?: Partial<Values>;
|
||||||
|
onsave: (v: Values) => void | Promise<void>;
|
||||||
|
oncancel: () => void;
|
||||||
|
ondelete?: (() => void) | null;
|
||||||
|
} = $props();
|
||||||
|
|
||||||
|
// The form edits a copy taken when it opens; later changes to the props don't reset it.
|
||||||
|
const initial = untrack(() => ({ ...defaults, ...(event ?? {}) }));
|
||||||
|
let f = $state<Values>({
|
||||||
|
title: initial.title ?? '',
|
||||||
|
date: initial.date ?? '',
|
||||||
|
end_date: initial.end_date ?? null,
|
||||||
|
all_day: initial.all_day ?? true,
|
||||||
|
start_time: initial.start_time ?? null,
|
||||||
|
end_time: initial.end_time ?? null,
|
||||||
|
kind: initial.kind ?? 'event',
|
||||||
|
notes: initial.notes ?? null,
|
||||||
|
location: initial.location ?? null,
|
||||||
|
child_ids: [...(initial.child_ids ?? [])],
|
||||||
|
});
|
||||||
|
let saving = $state(false);
|
||||||
|
let confirmDelete = $state(false);
|
||||||
|
const kinds = $derived(Object.entries(KINDS).filter(([k]) => k !== 'pack_lunch' || f.kind === 'pack_lunch'));
|
||||||
|
|
||||||
|
async function submit(e: SubmitEvent) {
|
||||||
|
e.preventDefault();
|
||||||
|
saving = true;
|
||||||
|
try {
|
||||||
|
await onsave({
|
||||||
|
...f,
|
||||||
|
title: f.title.trim(),
|
||||||
|
end_date: f.end_date || null,
|
||||||
|
start_time: f.all_day ? null : f.start_time || null,
|
||||||
|
end_time: f.all_day ? null : f.end_time || null,
|
||||||
|
notes: f.notes?.trim() || null,
|
||||||
|
location: f.location?.trim() || null,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
saving = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<form class="editor" onsubmit={submit}>
|
||||||
|
{#if event?.source_text}
|
||||||
|
<p class="muted small">On the page: “{event.source_text}”</p>
|
||||||
|
{/if}
|
||||||
|
<div class="field">
|
||||||
|
<label for="title-{event?.id ?? 'new'}">Title</label>
|
||||||
|
<input id="title-{event?.id ?? 'new'}" bind:value={f.title} required maxlength="80" />
|
||||||
|
</div>
|
||||||
|
<div class="fields">
|
||||||
|
<div class="field">
|
||||||
|
<label for="date-{event?.id ?? 'new'}">Date</label>
|
||||||
|
<input id="date-{event?.id ?? 'new'}" type="date" bind:value={f.date} required />
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label for="end-{event?.id ?? 'new'}">Last day (multi-day)</label>
|
||||||
|
<input id="end-{event?.id ?? 'new'}" type="date" bind:value={f.end_date} min={f.date} />
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label for="kind-{event?.id ?? 'new'}">Kind</label>
|
||||||
|
<select id="kind-{event?.id ?? 'new'}" bind:value={f.kind}>
|
||||||
|
{#each kinds as [value, label]}
|
||||||
|
<option {value}>{label}</option>
|
||||||
|
{/each}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<label class="check"><input type="checkbox" bind:checked={f.all_day} /> All day</label>
|
||||||
|
{#if !f.all_day}
|
||||||
|
<div class="fields">
|
||||||
|
<div class="field">
|
||||||
|
<label for="start-{event?.id ?? 'new'}">Starts</label>
|
||||||
|
<input id="start-{event?.id ?? 'new'}" type="time" bind:value={f.start_time} required />
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label for="stop-{event?.id ?? 'new'}">Ends (optional)</label>
|
||||||
|
<input id="stop-{event?.id ?? 'new'}" type="time" bind:value={f.end_time} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
<fieldset class="field">
|
||||||
|
<legend class="muted small">For</legend>
|
||||||
|
<div class="row">
|
||||||
|
{#each app.household!.children as c (c.id)}
|
||||||
|
<label class="check"><input type="checkbox" bind:group={f.child_ids} value={c.id} /><span class="dot" style:background={c.color}></span>{c.display_name}</label>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
<div class="fields">
|
||||||
|
<div class="field">
|
||||||
|
<label for="loc-{event?.id ?? 'new'}">Location</label>
|
||||||
|
<input id="loc-{event?.id ?? 'new'}" bind:value={f.location} maxlength="200" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label for="notes-{event?.id ?? 'new'}">Notes</label>
|
||||||
|
<textarea id="notes-{event?.id ?? 'new'}" bind:value={f.notes} maxlength="2000"></textarea>
|
||||||
|
</div>
|
||||||
|
<div class="row spread">
|
||||||
|
<div class="row">
|
||||||
|
<button class="primary" type="submit" disabled={saving}>Save</button>
|
||||||
|
<button type="button" onclick={oncancel}>Cancel</button>
|
||||||
|
</div>
|
||||||
|
{#if ondelete}
|
||||||
|
{#if confirmDelete}
|
||||||
|
<button type="button" class="danger" onclick={ondelete}>Delete event</button>
|
||||||
|
{:else}
|
||||||
|
<button type="button" class="quiet" onclick={() => (confirmDelete = true)}>Delete…</button>
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.editor { padding: 12px 0 4px; border-top: 1px dashed var(--border); margin-top: 8px; }
|
||||||
|
fieldset { border: 0; padding: 0; margin: 0 0 12px; }
|
||||||
|
</style>
|
||||||
Reference in new issue
Block a user