Add new scan screen

This commit is contained in:
iamdoubz committed 2026-11-05 16:08:39 -06:00
1 parent ac4c45bb36
commit c0ca256282
1 file changed
+68
+68
View File
@@ -0,0 +1,68 @@
<script lang="ts">
import { app, go } from '../lib/state.svelte';
import { thisMonth } from '../lib/dates';
const sources = $derived(app.household!.sources);
let sourceId = $state<number | ''>('');
let month = $state(thisMonth());
let files = $state<FileList | null>(null);
let busy = $state(false);
let error = $state('');
$effect(() => {
if (sourceId === '' && sources.length === 1) sourceId = sources[0].id;
});
async function submit(e: SubmitEvent) {
e.preventDefault();
if (!files?.length || sourceId === '') return;
busy = true;
error = '';
const form = new FormData();
form.set('source_id', String(sourceId));
form.set('month', month);
for (const f of files) form.append('files', f);
try {
const res = await fetch('/api/scans', { method: 'POST', body: form });
const body = await res.json().catch(() => null);
if (!res.ok) throw new Error(typeof body?.detail === 'string' ? body.detail : `${res.status} ${res.statusText}`);
go(`#/scan/${body.id}`);
} catch (err: any) {
error = err.message;
} finally {
busy = false;
}
}
</script>
<h1>New scan</h1>
{#if sources.length === 0}
<p class="card">First add a source (the school calendar, a menu, a team schedule) in <a href="#/settings">Settings</a>.</p>
{:else}
<form class="card stack" onsubmit={submit}>
<div>
<label for="source">What is it?</label>
<select id="source" bind:value={sourceId} required>
<option value="" disabled>Choose a source</option>
{#each sources as s (s.id)}
<option value={s.id}>{s.name}</option>
{/each}
</select>
</div>
<div>
<label for="month">Month it covers (the first month, for multi-month pages)</label>
<input id="month" type="month" bind:value={month} required />
</div>
<div>
<label for="files">Photo, screenshot or PDF</label>
<!-- No `capture` attribute: phones then offer both the camera and the photo library. -->
<input id="files" type="file" accept="image/*,application/pdf" multiple bind:files required />
<p class="muted small">Two-sided sheet? Add both photos. The photo stays on your server.</p>
</div>
{#if error}<p class="error">{error}</p>{/if}
<button class="primary" type="submit" disabled={busy || !files?.length || sourceId === ''}>
{busy ? 'Uploading…' : 'Read it'}
</button>
</form>
{/if}