Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,10 @@ group for Mark all (MEA-111). An error page from something in between, such as a
no answer. An unexpected error from Meals itself says "Something went wrong. Please try again."
instead. Either way the page doesn't reload, since that needs the server too. Tried it goes back
to what's saved, as the stars and Request still show it, so tapping it again tries again.
Tapping a line's circle again, after an earlier tap on the page got no answer, finds the line
already received if that tap did reach Meals. A check-off the page already sent for that line
counts as done then: the list shows the line received, with no message (MEA-133). Otherwise, as
when someone else checked it off first, the row says "This line was already received".

### 6.12 Requests

Expand Down Expand Up @@ -1411,7 +1415,14 @@ suggestions), quantity, unit, store (filled from the default; tap to change) and
store". Groups can be collapsed, and the device remembers which ones are. Each group
lists its To Order lines, then Ordered, then Received today (struck through). Store
groups have "Mark all ordered" and "Mark all received"; "No store" has neither. After either,
the group says how many lines it changed (6.2). A row's circle checks it off as Received.
the group says how many lines it changed (6.2). A row's circle checks it off as Received. It
fills as soon as it's tapped, and until the check-off is saved, or the row says why it wasn't, the
row's buttons are dimmed and can't be tapped. Undoing a received line empties its circle the same
way, and picking a store under "Where did you get it?" dims the stores until it's done (MEA-133).
Add reads "Adding…" until the line is added or the add bar says why not. While any of the page's
forms is on its way, sending it again, as with a second tap on a slow connection, does nothing:
that goes for Add, the question about an item already on the list, Mark all and a line's sheet as
well as the circles, so nothing is added or changed twice.
Tapping a row opens an edit sheet: quantity, unit, store, note, Mark ordered, Didn't come, Got
fewer, and Delete. An item's notes (brand, size) show under its name, and its image, if it has
one, beside it (Item images, below).
Expand Down
4 changes: 4 additions & 0 deletions src/lib/groceries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// The grocery list's shared rules (design 6.2), for the page and the server alike.

/** Why a line can't be checked off or get Got fewer: it's already received. */
export const ALREADY_RECEIVED = 'This line was already received';
5 changes: 3 additions & 2 deletions src/lib/server/data/needs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { error } from '@sveltejs/kit';
import { and, asc, desc, eq, gt, inArray, or, sql } from 'drizzle-orm';
import { dateIn } from '../../dates.ts';
import { ALREADY_RECEIVED } from '../../groceries.ts';
import { formatQuantity, normalizeName } from '../../text.ts';
import { conversionFactor, sameUnit } from '../../units.ts';
import { db, transaction } from '../db/index.ts';
Expand Down Expand Up @@ -393,7 +394,7 @@ export function markReceived(
): void {
transaction(() => {
const line = getLine(householdId, needId);
if (line.status === 'received') error(400, 'This line was already received');
if (line.status === 'received') error(400, ALREADY_RECEIVED);
const store = storeForStatusChange(householdId, line, storeId);
db()
.update(groceryNeeds)
Expand Down Expand Up @@ -496,7 +497,7 @@ export function markGotFewer(
): void {
transaction(() => {
const line = getLine(householdId, needId);
if (line.status === 'received') error(400, 'This line was already received');
if (line.status === 'received') error(400, ALREADY_RECEIVED);
if (!(receivedQuantity > 0 && receivedQuantity < line.quantity)) {
error(400, `Enter an amount less than ${formatQuantity(line.quantity)}`);
}
Expand Down
105 changes: 86 additions & 19 deletions src/routes/(app)/groceries/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<script lang="ts">
import { untrack } from 'svelte';
import { SvelteSet } from 'svelte/reactivity';
import { enhance, type ActionResult, type SubmitFunction } from '$app/forms';
import { refreshAll } from '$app/navigation';
import ConfirmButton from '#lib/components/ConfirmButton.svelte';
import ItemInput from '#lib/components/ItemInput.svelte';
import ItemThumb from '#lib/components/ItemThumb.svelte';
import Sheet from '#lib/components/Sheet.svelte';
import Tour from '#lib/components/Tour.svelte';
import { ALREADY_RECEIVED } from '#lib/groceries.ts';
import { keepPage, problemIn } from '#lib/submit.ts';
import { foldCase, formatQuantity, normalizeName } from '#lib/text.ts';

Expand All @@ -20,13 +22,36 @@
return line.unit ? `${formatQuantity(line.quantity)} ${line.unit}` : formatQuantity(line.quantity);
}

// Changes on their way, by what sent them: 'add' for the add bar and its question, a store
// group's key for its Mark all, 'sheet' for a line's sheet, a line's id for checking it off,
// from its circle or "Where did you get it?", and `undo-<id>` for undoing that. Sending one
// again while it's on its way, as with a second tap on a slow connection, does nothing
// (design 7, Grocery list; MEA-133).
const sending = new SvelteSet<string | number>();

/** Sends a form as `submit` does, unless `key` already has a change on its way. */
function oneAtATime(key: string | number, submit: SubmitFunction): SubmitFunction {
return async (input) => {
if (sending.has(key)) return input.cancel();
sending.add(key);
const callback = await submit(input);
return async (options) => {
try {
await (callback ? callback(options) : options.update());
} finally {
sending.delete(key);
}
};
};
}

/**
* Sends a form whose messages show as `action`'s: the add bar's, or a store's Mark all in its
* group, named by the group's key. When it can't be done, its message shows there as the
* server's would, and the page and what was typed stay (MEA-111).
*/
function send(action: string, submit?: SubmitFunction): SubmitFunction {
return keepPage((error) => (form = { action, error }), submit);
return oneAtATime(action, keepPage((error) => (form = { action, error }), submit));
}

// Add bar
Expand Down Expand Up @@ -108,8 +133,9 @@
});

// The prompt's own forms: errors show in the prompt, and success clears the add bar only
// where it still holds the item the prompt was about.
const submitPrompt: SubmitFunction = () => {
// where it still holds the item the prompt was about. Each is an add, so it waits for the
// add bar's, and the add bar for it.
const submitPrompt = oneAtATime('add', () => {
return async ({ result, update }) => {
const problem = problemIn(result);
if (problem !== null) {
Expand All @@ -126,7 +152,7 @@
if (promptSent) clearIfUnchanged(promptSent);
}
};
};
});

// One line's actions. Their errors show next to the line or in its sheet, not elsewhere.

Expand All @@ -139,10 +165,22 @@
return problemIn(result);
}

function lineSubmit(where: Where): SubmitFunction {
return () => {
/**
* Sends a change to a line as `key` in `sending`, with its errors at `where`. `repeat` is for
* checking off a line this page has checked off before.
*/
function lineSubmit(where: Where, key: string | number, repeat = false): SubmitFunction {
return oneAtATime(key, () => {
return async ({ result, update }) => {
const message = lineFailure(result);
if (repeat && message === ALREADY_RECEIVED) {
// The check-off before went through, though its answer never came, as when the
// connection dropped on the way back (6.11). So this one is done too (MEA-133).
lineError = null;
await refreshAll();
pickOpen = false;
return;
}
if (message !== null) {
lineError = { where, message };
// Someone else may have changed the line; show the list as it is now. Not when
Expand All @@ -158,6 +196,18 @@
pickOpen = false;
}
};
});
}

// Lines this page has sent a check-off for.
const checkedOff = new Set<number>();

/** Checks a line off, from its circle or, for a line without a store, "Where did you get it?". */
function checkOff(id: number, where: Where = id): SubmitFunction {
return (input) => {
const repeat = checkedOff.has(id);
checkedOff.add(id);
return lineSubmit(where, id, repeat)(input);
};
}

Expand All @@ -172,7 +222,11 @@

// An error from Got fewer shows beside its amount, and any other at the top of the sheet.
const submitSheet: SubmitFunction = (input) =>
lineSubmit(input.submitter === fewerSave ? 'fewer' : 'sheet')(input);
lineSubmit(input.submitter === fewerSave ? 'fewer' : 'sheet', 'sheet')(input);

// "Where did you get it?" checks off the line it was opened for, read as the form is sent,
// since the same form stays on the page from one line to the next.
const submitPicker: SubmitFunction = (input) => checkOff(picking!.id, 'picker')(input);

function edit(line: Line) {
editing = line;
Expand Down Expand Up @@ -282,7 +336,7 @@
</select>
</div>
<input type="hidden" name="resolution" value="none" />
<button class="primary">Add</button>
<button class="primary">{sending.has('add') ? 'Adding…' : 'Add'}</button>
{#if form?.action === 'add' && form.error}<p class="error" role="alert">{form.error}</p>{/if}
</form>

Expand Down Expand Up @@ -363,37 +417,48 @@
{/each}

{#snippet row(line: Line)}
<!-- While its circle's change is on its way, the circle shows it and the row's buttons wait. -->
{@const busy = sending.has(line.status === 'received' ? `undo-${line.id}` : line.id)}
<li class="line" class:done={line.status === 'received'}>
{#if line.status === 'received'}
<form method="POST" action="?/undoReceive" use:enhance={lineSubmit(line.id)}>
<form method="POST" action="?/undoReceive" use:enhance={lineSubmit(line.id, `undo-${line.id}`)}>
<input type="hidden" name="id" value={line.id} />
<button class="check" aria-label="Undo received: {line.itemName}">
<span class="circle checked" aria-hidden="true">✓</span>
<button class="check" aria-label="Undo received: {line.itemName}" disabled={busy}>
{@render circle(!busy)}
</button>
</form>
{:else if line.storeId === null}
<button class="check" aria-label="Mark received: {line.itemName}" onclick={() => pickStore(line)}>
<span class="circle" aria-hidden="true"></span>
<button
class="check"
aria-label="Mark received: {line.itemName}"
disabled={busy}
onclick={() => pickStore(line)}
>
{@render circle(busy)}
</button>
{:else}
<form method="POST" action="?/receive" use:enhance={lineSubmit(line.id)}>
<form method="POST" action="?/receive" use:enhance={checkOff(line.id)}>
<input type="hidden" name="id" value={line.id} />
<button class="check" aria-label="Mark received: {line.itemName}">
<span class="circle" aria-hidden="true"></span>
<button class="check" aria-label="Mark received: {line.itemName}" disabled={busy}>
{@render circle(busy)}
</button>
</form>
{/if}
{#if line.status === 'received'}
<div class="body">{@render lineText(line)}</div>
{:else}
<button class="body" onclick={() => edit(line)}>{@render lineText(line)}</button>
<button class="body" disabled={busy} onclick={() => edit(line)}>{@render lineText(line)}</button>
{/if}
{#if lineError?.where === line.id}
<p class="error line-error" role="alert">{lineError.message}</p>
{/if}
</li>
{/snippet}

{#snippet circle(checked: boolean)}
<span class="circle" class:checked aria-hidden="true">{checked ? '✓' : ''}</span>
{/snippet}

{#snippet lineText(line: Line)}
<ItemThumb photoKey={line.itemPhotoKey} size={40} />
<span class="text">
Expand Down Expand Up @@ -486,10 +551,12 @@
{#if activeStores.length === 0}
<p>Add your stores on the <a class="link-tap" href="/household">Household</a> page first.</p>
{:else}
<form method="POST" action="?/receive" use:enhance={lineSubmit('picker')} class="stores">
<form method="POST" action="?/receive" use:enhance={submitPicker} class="stores">
<input type="hidden" name="id" value={picking.id} />
{#each activeStores as option (option.id)}
<button name="store" value={option.id}>{option.name}</button>
<button name="store" value={option.id} disabled={sending.has(picking.id)}>
{option.name}
</button>
{/each}
</form>
{/if}
Expand Down
Loading
Loading