Add review screen
This commit is contained in:
1 parent
a1cc191fde
commit
ab8fe0d108
1 file changed
+296
@@ -0,0 +1,296 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { api, KINDS, type CalEvent, type EventValues as Values, type Kind, type MenuItem, type Scan } from '../lib/api';
|
||||||
|
import { app, toast } from '../lib/state.svelte';
|
||||||
|
import { dayLabel, monthsBetween, whenLabel } from '../lib/dates';
|
||||||
|
import EventEditor from './EventEditor.svelte';
|
||||||
|
import MonthGrid from './MonthGrid.svelte';
|
||||||
|
|
||||||
|
let { scan = $bindable() }: { scan: Scan } = $props();
|
||||||
|
|
||||||
|
let active = $state<number | null>(null); // event highlighted on the photo
|
||||||
|
let editing = $state<number | 'new' | null>(null);
|
||||||
|
let newDate = $state('');
|
||||||
|
let page = $state(1);
|
||||||
|
let zoom = $state(false);
|
||||||
|
let view = $state<'list' | 'grid'>('list');
|
||||||
|
let selecting = $state(false);
|
||||||
|
let selected = $state<number[]>([]);
|
||||||
|
let bulkKind = $state<Kind>('event');
|
||||||
|
let open = $state({ unchanged: false, excluded: false });
|
||||||
|
let busy = $state(false);
|
||||||
|
let photo = $state<HTMLDivElement>();
|
||||||
|
|
||||||
|
const live = $derived(scan.events.filter((e) => e.status !== 'deleted'));
|
||||||
|
const needs = $derived(live.filter((e) => e.include && e.flags.length));
|
||||||
|
const updated = $derived(live.filter((e) => e.include && !e.flags.length && e.match === 'updated'));
|
||||||
|
const fresh = $derived(live.filter((e) => e.include && !e.flags.length && (e.match === 'new' || !e.match)));
|
||||||
|
const unchanged = $derived(live.filter((e) => e.include && !e.flags.length && e.match === 'unchanged'));
|
||||||
|
const excluded = $derived(live.filter((e) => !e.include));
|
||||||
|
const activeBox = $derived(scan.events.find((e) => e.id === active)?.cell_bbox ?? null);
|
||||||
|
const months = $derived(
|
||||||
|
monthsBetween(scan.date_start ?? `${scan.hint_month}-01`, scan.date_end ?? `${scan.hint_month}-01`),
|
||||||
|
);
|
||||||
|
const children = $derived(new Map(app.household!.children.map((c) => [c.id, c])));
|
||||||
|
const colorFor = (e: CalEvent) => children.get(e.child_ids[0])?.color ?? 'var(--accent)';
|
||||||
|
const flaggedMenu = $derived(scan.menu_items.filter((m) => m.flags.length).length);
|
||||||
|
|
||||||
|
async function refresh() {
|
||||||
|
scan = await api<Scan>(`/scans/${scan.id}`);
|
||||||
|
}
|
||||||
|
async function run(work: () => Promise<unknown>) {
|
||||||
|
try {
|
||||||
|
await work();
|
||||||
|
await refresh();
|
||||||
|
} catch (e: any) {
|
||||||
|
toast(e.message);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function focus(e: CalEvent) {
|
||||||
|
active = e.id;
|
||||||
|
if (e.page) page = e.page;
|
||||||
|
// Scroll the photo panel to the highlighted spot.
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
const img = photo?.querySelector('img');
|
||||||
|
if (!photo || !img || !e.cell_bbox) return;
|
||||||
|
photo.scrollTo({ top: e.cell_bbox[1] * img.clientHeight - 24, left: e.cell_bbox[0] * img.clientWidth - 24, behavior: 'smooth' });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const save = (id: number) => (v: Values) => run(() => api(`/events/${id}`, { method: 'PATCH', json: v })).then(() => (editing = null));
|
||||||
|
const remove = (id: number) => () => run(() => api(`/events/${id}`, { method: 'DELETE' })).then(() => (editing = null));
|
||||||
|
const add = (v: Values) => run(() => api('/events', { method: 'POST', json: { ...v, scan_id: scan.id } })).then(() => (editing = null));
|
||||||
|
const include = (e: CalEvent, value: boolean) => run(() => api(`/events/${e.id}`, { method: 'PATCH', json: { include: value } }));
|
||||||
|
|
||||||
|
async function undo() {
|
||||||
|
try {
|
||||||
|
scan = await api<Scan>(`/scans/${scan.id}/undo`, { method: 'POST' });
|
||||||
|
} catch (e: any) {
|
||||||
|
toast(e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function bulk(body: object, what: string) {
|
||||||
|
if (!selected.length) return;
|
||||||
|
await run(() => api('/events/bulk', { method: 'POST', json: { ids: selected, ...body } }));
|
||||||
|
toast(`${what}: ${selected.length} events`);
|
||||||
|
selected = [];
|
||||||
|
selecting = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function addOn(date: string) {
|
||||||
|
newDate = date;
|
||||||
|
editing = 'new';
|
||||||
|
view = 'list';
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveMenu(m: MenuItem, text: string, noLunch: boolean) {
|
||||||
|
const items = text.split('\n').map((s) => s.trim()).filter(Boolean);
|
||||||
|
await run(() => api(`/menu_items/${m.id}`, { method: 'PATCH', json: { items, no_lunch: noLunch } }));
|
||||||
|
toast(`Saved ${dayLabel(m.date)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function publish() {
|
||||||
|
busy = true;
|
||||||
|
try {
|
||||||
|
scan = await api<Scan>(`/scans/${scan.id}/publish`, { method: 'POST' });
|
||||||
|
toast('Published');
|
||||||
|
} catch (e: any) {
|
||||||
|
toast(e.message);
|
||||||
|
} finally {
|
||||||
|
busy = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function changes(e: CalEvent): string[] {
|
||||||
|
if (!e.replaces) return [];
|
||||||
|
const names: Record<string, string> = { date: 'date', end_date: 'last day', all_day: 'all day', start_time: 'start',
|
||||||
|
end_time: 'end', title: 'title', notes: 'notes', location: 'location', kind: 'kind' };
|
||||||
|
return Object.keys(names).filter((k) => (e as any)[k] !== (e.replaces as any)[k]).map((k) => names[k]);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#snippet eventRow(e: CalEvent)}
|
||||||
|
<li class="event card" class:active={active === e.id}>
|
||||||
|
<div class="line">
|
||||||
|
{#if selecting}
|
||||||
|
<input type="checkbox" bind:group={selected} value={e.id} aria-label={`Select ${e.title}`} />
|
||||||
|
{/if}
|
||||||
|
<button class="item" onclick={() => focus(e)} aria-pressed={active === 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>
|
||||||
|
{#each e.flags as flag}
|
||||||
|
<span class="badge warn"><span aria-hidden="true">⚠</span> {flag}</span>
|
||||||
|
{/each}
|
||||||
|
{#if e.match === 'updated'}
|
||||||
|
<span class="badge info">Changes: {changes(e).join(', ') || 'children'}</span>
|
||||||
|
{/if}
|
||||||
|
</button>
|
||||||
|
{#if e.include}
|
||||||
|
<button class="small" onclick={() => (editing = editing === e.id ? null : e.id)} aria-expanded={editing === e.id}>Edit</button>
|
||||||
|
{:else}
|
||||||
|
<button class="small" onclick={() => include(e, true)}>Include</button>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
{#if editing === e.id}
|
||||||
|
<EventEditor event={e} onsave={save(e.id)} oncancel={() => (editing = null)} ondelete={remove(e.id)} />
|
||||||
|
{/if}
|
||||||
|
</li>
|
||||||
|
{/snippet}
|
||||||
|
|
||||||
|
{#snippet group(title: string, list: CalEvent[], note: string)}
|
||||||
|
{#if list.length}
|
||||||
|
<h2>{title} <span class="muted">({list.length})</span></h2>
|
||||||
|
{#if note}<p class="muted small">{note}</p>{/if}
|
||||||
|
<ul class="events">
|
||||||
|
{#each list as e (e.id)}{@render eventRow(e)}{/each}
|
||||||
|
</ul>
|
||||||
|
{/if}
|
||||||
|
{/snippet}
|
||||||
|
|
||||||
|
<div class="layout">
|
||||||
|
<div class="photo-col">
|
||||||
|
<div class="photo" bind:this={photo} class:zoom>
|
||||||
|
{#if scan.image_keys.length}
|
||||||
|
<div class="frame">
|
||||||
|
<img src={`/api/scans/${scan.id}/pages/${page}`} alt={`Page ${page} of the scanned ${scan.source_name}`} />
|
||||||
|
{#if activeBox && scan.events.find((e) => e.id === active)?.page === page}
|
||||||
|
<div class="box" style:left="{activeBox[0] * 100}%" style:top="{activeBox[1] * 100}%"
|
||||||
|
style:width="{activeBox[2] * 100}%" style:height="{activeBox[3] * 100}%"></div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<p class="muted">The photo has been deleted.</p>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
{#if scan.image_keys.length > 1}
|
||||||
|
{#each scan.image_keys as _, i}
|
||||||
|
<button class="small" aria-pressed={page === i + 1} onclick={() => (page = i + 1)}>Page {i + 1}</button>
|
||||||
|
{/each}
|
||||||
|
{/if}
|
||||||
|
<button class="small" aria-pressed={zoom} onclick={() => (zoom = !zoom)}>{zoom ? 'Fit photo' : 'Zoom photo'}</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="list-col">
|
||||||
|
<div class="toolbar row">
|
||||||
|
<button class="small" onclick={undo} disabled={!scan.can_undo}>Undo</button>
|
||||||
|
<button class="small" aria-pressed={selecting} onclick={() => { selecting = !selecting; selected = []; }}>
|
||||||
|
{selecting ? 'Done selecting' : 'Select'}
|
||||||
|
</button>
|
||||||
|
<button class="small" onclick={() => addOn(`${scan.hint_month}-01`)}>Add event</button>
|
||||||
|
<button class="small" onclick={() => (view = view === 'list' ? 'grid' : 'list')}>{view === 'list' ? 'Month view' : 'List view'}</button>
|
||||||
|
<span class="grow"></span>
|
||||||
|
<button class="primary" onclick={publish} disabled={busy}>Publish</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{#if selecting}
|
||||||
|
<div class="card bulk stack">
|
||||||
|
<p class="small">{selected.length} selected</p>
|
||||||
|
<div class="row">
|
||||||
|
<button class="small" onclick={() => bulk({ delete: true }, 'Deleted')}>Delete</button>
|
||||||
|
<button class="small" onclick={() => bulk({ changes: { include: false } }, 'Unchecked')}>Uncheck</button>
|
||||||
|
<select bind:value={bulkKind} aria-label="Kind to set" style="width:auto">
|
||||||
|
{#each Object.entries(KINDS).filter(([k]) => k !== 'pack_lunch') as [value, label]}
|
||||||
|
<option {value}>{label}</option>
|
||||||
|
{/each}
|
||||||
|
</select>
|
||||||
|
<button class="small" onclick={() => bulk({ changes: { kind: bulkKind } }, 'Kind changed')}>Set kind</button>
|
||||||
|
<button class="small" onclick={() => bulk({ changes: { all_day: true } }, 'Made all-day')}>Make all-day</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
{#if editing === 'new'}
|
||||||
|
<div class="card">
|
||||||
|
<h3 style="margin-top:0">New event</h3>
|
||||||
|
<EventEditor
|
||||||
|
defaults={{ date: newDate, child_ids: app.household!.sources.find((s) => s.id === scan.source_id)?.child_ids ?? [] }}
|
||||||
|
onsave={add}
|
||||||
|
oncancel={() => (editing = null)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
{#if view === 'grid'}
|
||||||
|
<p class="muted small">Tap a day to add an event.</p>
|
||||||
|
{#each months as month}
|
||||||
|
<MonthGrid {month} events={live.filter((e) => e.include)} {colorFor} onday={addOn} />
|
||||||
|
{/each}
|
||||||
|
{:else}
|
||||||
|
{#if !live.length && !scan.menu_items.length}
|
||||||
|
<p class="empty">Nothing was found on this page. Add events by hand, or discard the scan.</p>
|
||||||
|
{/if}
|
||||||
|
{@render group('Needs a look', needs, 'Each one says why. Tap to see where it came from on the photo.')}
|
||||||
|
{@render group('Updated', updated, 'Already on your calendar; publishing applies these changes.')}
|
||||||
|
{@render group('New', fresh, '')}
|
||||||
|
|
||||||
|
{#if unchanged.length}
|
||||||
|
<button class="quiet disclosure" aria-expanded={open.unchanged} onclick={() => (open.unchanged = !open.unchanged)}>
|
||||||
|
{open.unchanged ? '▾' : '▸'} Unchanged ({unchanged.length})
|
||||||
|
</button>
|
||||||
|
{#if open.unchanged}<ul class="events">{#each unchanged as e (e.id)}{@render eventRow(e)}{/each}</ul>{/if}
|
||||||
|
{/if}
|
||||||
|
{#if excluded.length}
|
||||||
|
<button class="quiet disclosure" aria-expanded={open.excluded} onclick={() => (open.excluded = !open.excluded)}>
|
||||||
|
{open.excluded ? '▾' : '▸'} Not for this child, or unchecked ({excluded.length})
|
||||||
|
</button>
|
||||||
|
{#if open.excluded}<ul class="events">{#each excluded as e (e.id)}{@render eventRow(e)}{/each}</ul>{/if}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
{#if scan.menu_items.length}
|
||||||
|
<h2>Menu <span class="muted">({scan.menu_items.length} days{flaggedMenu ? `, ${flaggedMenu} need a look` : ''})</span></h2>
|
||||||
|
<p class="muted small">One food per line. Your kids pick lunch days on the Lunch screen after you publish.</p>
|
||||||
|
<ul class="events">
|
||||||
|
{#each scan.menu_items as m (m.id)}
|
||||||
|
{@const text = m.items.join('\n')}
|
||||||
|
<li class="card menu-day">
|
||||||
|
<form onsubmit={(ev) => { ev.preventDefault(); const fd = new FormData(ev.currentTarget as HTMLFormElement); saveMenu(m, String(fd.get('items') ?? ''), fd.get('no_lunch') === 'on'); }}>
|
||||||
|
<div class="row spread">
|
||||||
|
<strong>{dayLabel(m.date)}</strong>
|
||||||
|
{#each m.flags as flag}<span class="badge warn"><span aria-hidden="true">⚠</span> {flag}</span>{/each}
|
||||||
|
</div>
|
||||||
|
<textarea name="items" aria-label={`Menu for ${dayLabel(m.date)}`} rows={Math.max(2, m.items.length)}>{text}</textarea>
|
||||||
|
<div class="row spread">
|
||||||
|
<label class="check"><input type="checkbox" name="no_lunch" checked={m.no_lunch} /> No lunch this day</label>
|
||||||
|
<button class="small" type="submit">Save</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.layout { display: grid; gap: 16px; }
|
||||||
|
@media (min-width: 900px) {
|
||||||
|
.layout { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
|
||||||
|
.photo-col { position: sticky; top: 16px; }
|
||||||
|
.photo { max-height: calc(100vh - 140px); }
|
||||||
|
}
|
||||||
|
.photo-col { position: sticky; top: 0; z-index: 2; background: var(--bg); padding-bottom: 8px; }
|
||||||
|
.photo { max-height: 38vh; overflow: auto; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); }
|
||||||
|
.frame { position: relative; }
|
||||||
|
.frame img { display: block; width: 100%; height: auto; }
|
||||||
|
.zoom .frame { width: 250%; }
|
||||||
|
.box { position: absolute; border: 3px solid var(--accent); background: var(--highlight); border-radius: 4px; pointer-events: none; }
|
||||||
|
.toolbar { position: sticky; top: calc(38vh + 52px); z-index: 1; background: var(--bg); padding: 8px 0; }
|
||||||
|
@media (min-width: 900px) { .toolbar { top: 0; } }
|
||||||
|
.events { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
|
||||||
|
.event { padding: 8px 12px; }
|
||||||
|
.event.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
|
||||||
|
.line { display: flex; align-items: center; gap: 8px; }
|
||||||
|
.item {
|
||||||
|
flex: 1; display: flex; flex-direction: column; align-items: flex-start; gap: 4px; text-align: left;
|
||||||
|
border: 0; background: none; padding: 4px 0; min-width: 0;
|
||||||
|
}
|
||||||
|
.title { font-weight: 600; display: flex; align-items: center; gap: 8px; }
|
||||||
|
.disclosure { margin-top: 16px; padding: 0; font-weight: 600; }
|
||||||
|
.bulk { margin-bottom: 8px; }
|
||||||
|
.menu-day textarea { margin: 8px 0; }
|
||||||
|
</style>
|
||||||
Reference in new issue
Block a user