Add calendar screen with .ics download
This commit is contained in:
1 parent
ab8fe0d108
commit
b7fd804172
1 file changed
+141
@@ -0,0 +1,141 @@
|
||||
<script lang="ts">
|
||||
import { api, KINDS, type CalEvent, type EventValues } from '../lib/api';
|
||||
import { app, toast } from '../lib/state.svelte';
|
||||
import { dayLabel, monthEnd, monthLabel, shiftMonth, thisMonth, whenLabel } from '../lib/dates';
|
||||
import EventEditor from './EventEditor.svelte';
|
||||
import MonthGrid from './MonthGrid.svelte';
|
||||
|
||||
const calendars = $derived(app.household!.calendars);
|
||||
let calendarId = $state<number | null>(null);
|
||||
let month = $state(thisMonth());
|
||||
let view = $state<'list' | 'grid'>('list');
|
||||
let events = $state<CalEvent[] | null>(null);
|
||||
let editing = $state<number | 'new' | null>(null);
|
||||
let newDate = $state('');
|
||||
let error = $state('');
|
||||
|
||||
$effect(() => {
|
||||
if (calendarId === null && calendars.length) calendarId = calendars[0].id;
|
||||
});
|
||||
const calendar = $derived(calendars.find((c) => c.id === calendarId));
|
||||
const children = $derived(new Map(app.household!.children.map((c) => [c.id, c])));
|
||||
const colorFor = (e: CalEvent) => children.get(e.child_ids[0])?.color ?? calendar?.color ?? 'var(--accent)';
|
||||
const byDay = $derived.by(() => {
|
||||
const days = new Map<string, CalEvent[]>();
|
||||
for (const e of events ?? []) {
|
||||
const key = e.date < `${month}-01` ? `${month}-01` : e.date; // multi-day events that began last month
|
||||
if (!days.has(key)) days.set(key, []);
|
||||
days.get(key)!.push(e);
|
||||
}
|
||||
return [...days.entries()].sort();
|
||||
});
|
||||
|
||||
async function load() {
|
||||
if (!calendarId) return;
|
||||
error = '';
|
||||
try {
|
||||
events = await api<CalEvent[]>(`/events?calendar_id=${calendarId}&start=${month}-01&end=${monthEnd(month)}`);
|
||||
} catch (e: any) {
|
||||
error = e.message;
|
||||
}
|
||||
}
|
||||
$effect(() => {
|
||||
void calendarId;
|
||||
void month;
|
||||
load();
|
||||
});
|
||||
|
||||
async function run(work: () => Promise<unknown>, done: string) {
|
||||
try {
|
||||
await work();
|
||||
editing = null;
|
||||
toast(done);
|
||||
await load();
|
||||
} catch (e: any) {
|
||||
toast(e.message);
|
||||
}
|
||||
}
|
||||
const save = (id: number) => (v: EventValues) => run(() => api(`/events/${id}`, { method: 'PATCH', json: v }), 'Saved');
|
||||
const remove = (id: number) => () => run(() => api(`/events/${id}`, { method: 'DELETE' }), 'Deleted');
|
||||
const add = (v: EventValues) => run(() => api('/events', { method: 'POST', json: v }), 'Added');
|
||||
function addOn(date: string) {
|
||||
newDate = date;
|
||||
editing = 'new';
|
||||
view = 'list';
|
||||
}
|
||||
</script>
|
||||
|
||||
<h1>Calendar</h1>
|
||||
|
||||
{#if !calendars.length}
|
||||
<p class="card">Add a calendar in <a href="#/settings">Settings</a> first.</p>
|
||||
{:else}
|
||||
<div class="row">
|
||||
<select bind:value={calendarId} aria-label="Calendar" class="grow" style="width:auto">
|
||||
{#each calendars as c (c.id)}<option value={c.id}>{c.name}</option>{/each}
|
||||
</select>
|
||||
<button onclick={() => (view = view === 'list' ? 'grid' : 'list')}>{view === 'list' ? 'Month view' : 'List view'}</button>
|
||||
</div>
|
||||
|
||||
<div class="row spread month">
|
||||
<button aria-label="Previous month" onclick={() => (month = shiftMonth(month, -1))}>‹</button>
|
||||
<strong>{monthLabel(month)}</strong>
|
||||
<button aria-label="Next month" onclick={() => (month = shiftMonth(month, 1))}>›</button>
|
||||
</div>
|
||||
|
||||
<section class="card stack">
|
||||
<div class="row">
|
||||
<a class="button primary" href={`/api/calendars/${calendarId}/export.ics`} download>Download new and changed (.ics)</a>
|
||||
<a class="button" href={`/api/calendars/${calendarId}/export.ics?everything=true`} download>Download everything</a>
|
||||
</div>
|
||||
<p class="muted small">
|
||||
On iPhone, open the file and tap “Add All”. Each download holds what changed since the last one, so re-importing
|
||||
doesn't duplicate events in Apple Calendar.
|
||||
{#if calendar?.last_exported_at}Last download: {new Date(calendar.last_exported_at.replace(' ', 'T') + 'Z').toLocaleString()}.{/if}
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<div class="row" style="margin-top:12px">
|
||||
<button onclick={() => addOn(`${month}-01`)}>Add event</button>
|
||||
</div>
|
||||
{#if editing === 'new'}
|
||||
<div class="card">
|
||||
<EventEditor defaults={{ date: newDate, child_ids: calendar?.child_ids ?? [] }} onsave={add} oncancel={() => (editing = null)} />
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if error}
|
||||
<p class="error">{error}</p>
|
||||
{:else if events === null}
|
||||
<p class="muted">Loading…</p>
|
||||
{:else if view === 'grid'}
|
||||
<MonthGrid {month} {events} {colorFor} onday={addOn} />
|
||||
{:else if !events.length}
|
||||
<p class="empty">Nothing this month.</p>
|
||||
{:else}
|
||||
{#each byDay as [day, list] (day)}
|
||||
<h3>{dayLabel(day)}</h3>
|
||||
<ul class="events">
|
||||
{#each list as e (e.id)}
|
||||
<li class="card">
|
||||
<button class="item" onclick={() => (editing = editing === e.id ? null : e.id)} aria-expanded={editing === e.id}>
|
||||
<span class="title"><span class="dot" style:background={colorFor(e)}></span>{e.title}</span>
|
||||
<span class="muted small">{whenLabel(e)} · {KINDS[e.kind]}{e.location ? ` · ${e.location}` : ''}</span>
|
||||
</button>
|
||||
{#if editing === e.id}
|
||||
<EventEditor event={e} onsave={save(e.id)} oncancel={() => (editing = null)} ondelete={remove(e.id)} />
|
||||
{/if}
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/each}
|
||||
{/if}
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.month { margin: 12px 0; }
|
||||
.month button { min-width: 44px; font-size: 1.25rem; }
|
||||
.events { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
|
||||
.item { width: 100%; flex-direction: column; align-items: flex-start; gap: 2px; border: 0; padding: 4px 0; text-align: left; background: none; }
|
||||
.title { font-weight: 600; display: flex; align-items: center; gap: 8px; }
|
||||
</style>
|
||||
Reference in new issue
Block a user