Add lunch screen
This commit is contained in:
1 parent
b7fd804172
commit
03207f7bfe
1 file changed
+112
@@ -0,0 +1,112 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { api, type LunchDay } from '../lib/api';
|
||||||
|
import { app, toast } from '../lib/state.svelte';
|
||||||
|
import { dayLabel, monthLabel, shiftMonth, thisMonth } from '../lib/dates';
|
||||||
|
|
||||||
|
const kids = $derived(app.household!.children);
|
||||||
|
let childId = $state<number | null>(null);
|
||||||
|
let month = $state(thisMonth());
|
||||||
|
let days = $state<LunchDay[] | null>(null);
|
||||||
|
let error = $state('');
|
||||||
|
let pending = $state<string | null>(null);
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
if (childId === null && kids.length) childId = kids[0].id;
|
||||||
|
});
|
||||||
|
const child = $derived(kids.find((c) => c.id === childId));
|
||||||
|
const hasMenu = $derived(app.household!.sources.some((s) => s.layout_type === 'menu' && s.child_ids.includes(childId ?? -1)));
|
||||||
|
const picked = $derived(days?.filter((d) => d.picked).length ?? 0);
|
||||||
|
|
||||||
|
async function load() {
|
||||||
|
if (childId === null) return;
|
||||||
|
error = '';
|
||||||
|
try {
|
||||||
|
days = await api<LunchDay[]>(`/lunch?child_id=${childId}&month=${month}`);
|
||||||
|
} catch (e: any) {
|
||||||
|
error = e.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$effect(() => {
|
||||||
|
void childId;
|
||||||
|
void month;
|
||||||
|
load();
|
||||||
|
});
|
||||||
|
|
||||||
|
async function toggle(d: LunchDay) {
|
||||||
|
pending = d.date;
|
||||||
|
try {
|
||||||
|
days = await api<LunchDay[]>('/lunch', { method: 'POST', json: { child_id: childId, date: d.date, picked: !d.picked } });
|
||||||
|
} catch (e: any) {
|
||||||
|
toast(e.message);
|
||||||
|
} finally {
|
||||||
|
pending = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<h1>Lunch</h1>
|
||||||
|
|
||||||
|
{#if !kids.length}
|
||||||
|
<p class="card">Add your children in <a href="#/settings">Settings</a> first.</p>
|
||||||
|
{:else}
|
||||||
|
<div class="row kids" role="tablist" aria-label="Child">
|
||||||
|
{#each kids as c (c.id)}
|
||||||
|
<button role="tab" aria-selected={c.id === childId} onclick={() => (childId = c.id)} style:--c={c.color}>
|
||||||
|
<span class="dot" style:background={c.color}></span>{c.display_name}
|
||||||
|
</button>
|
||||||
|
{/each}
|
||||||
|
</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>
|
||||||
|
|
||||||
|
{#if !hasMenu}
|
||||||
|
<p class="card">No lunch menu is set up for {child?.display_name}. Add a source with the “Lunch menu” layout in <a href="#/settings">Settings</a>, then scan the menu.</p>
|
||||||
|
{:else if error}
|
||||||
|
<p class="error">{error}</p>
|
||||||
|
{:else if days === null}
|
||||||
|
<p class="muted">Loading…</p>
|
||||||
|
{:else if !days.length}
|
||||||
|
<p class="empty">No published menu for {monthLabel(month)} yet. Scan it from the Scans tab.</p>
|
||||||
|
{:else}
|
||||||
|
<p class="muted">{child?.display_name}: tap the days to pack lunch. {picked} picked. Each pick goes on the calendar as “Pack lunch”.</p>
|
||||||
|
<ul class="days">
|
||||||
|
{#each days as d (d.date)}
|
||||||
|
<li class="card day" class:greyed={d.greyed} class:picked={d.picked}>
|
||||||
|
<div class="row spread">
|
||||||
|
<strong>{dayLabel(d.date)}</strong>
|
||||||
|
{#if d.warning}<span class="badge warn"><span aria-hidden="true">⚠</span> {d.warning}</span>{/if}
|
||||||
|
</div>
|
||||||
|
{#if d.greyed}
|
||||||
|
<p class="muted">{d.closed ?? 'No lunch this day'}</p>
|
||||||
|
{:else}
|
||||||
|
{#each d.menus as m}
|
||||||
|
{#if d.menus.length > 1}<p class="muted small">{m.source_name}</p>{/if}
|
||||||
|
<p class="food">{m.items.join(' · ')}</p>
|
||||||
|
{/each}
|
||||||
|
{/if}
|
||||||
|
{#if !d.greyed || d.picked}
|
||||||
|
<button class:primary={d.picked} aria-pressed={d.picked} disabled={pending === d.date} onclick={() => toggle(d)}>
|
||||||
|
{d.picked ? '✓ Packing lunch' : 'Pack lunch this day'}
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.kids button[aria-selected='true'] { border-color: var(--c); box-shadow: inset 0 0 0 2px var(--c); font-weight: 600; }
|
||||||
|
.month { margin: 12px 0; }
|
||||||
|
.month button { min-width: 44px; font-size: 1.25rem; }
|
||||||
|
.days { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
|
||||||
|
.day { display: flex; flex-direction: column; gap: 6px; }
|
||||||
|
.day button { margin-top: auto; }
|
||||||
|
.greyed { background: var(--grey-day); }
|
||||||
|
.picked { border-color: var(--accent); }
|
||||||
|
.food { margin: 0; }
|
||||||
|
</style>
|
||||||
Reference in new issue
Block a user