@zeyolabs/shopping 0.0.0-stage → 2026.11.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -2
- package/admin/OrderPage.svelte +289 -0
- package/admin/OrdersPage.svelte +123 -0
- package/admin/filters.ts +8 -0
- package/admin/orders.schema.ts +50 -0
- package/client.ts +24 -0
- package/components/PageTitle.svelte +24 -0
- package/components/ProductCard.svelte +66 -0
- package/config.ts +73 -0
- package/db/index.ts +6 -0
- package/db/schema.ts +254 -0
- package/emails/dispatch.ts +60 -0
- package/emails/order.ts +103 -0
- package/images.ts +2 -0
- package/money.ts +4 -0
- package/order-links.ts +11 -0
- package/package.json +97 -5
- package/packing.ts +49 -0
- package/pages/Basket.svelte +155 -0
- package/pages/Category.svelte +34 -0
- package/pages/CheckoutSuccess.svelte +36 -0
- package/pages/Order.svelte +125 -0
- package/pages/Product.svelte +89 -0
- package/pages/Shop.svelte +54 -0
- package/remote/admin-orders.remote.ts +75 -0
- package/remote/basket.remote.ts +63 -0
- package/remote/catalogue.remote.ts +45 -0
- package/remote/order.remote.ts +59 -0
- package/routes/catalogue-loads.ts +52 -0
- package/routes/checkout-complete.ts +29 -0
- package/routes/order-label.ts +20 -0
- package/routes/stripe-webhook.ts +42 -0
- package/royalmail.ts +148 -0
- package/server/admin-orders.ts +304 -0
- package/server/basket.ts +183 -0
- package/server/catalogue.ts +107 -0
- package/server/checkout.ts +41 -0
- package/server/mail.ts +67 -0
- package/server/orders.ts +181 -0
- package/server/shipping.ts +82 -0
- package/server/stripe.ts +70 -0
- package/shipping-rules.ts +15 -0
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zeyolabs/shopping
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shop and order fulfilment for SvelteKit 3 on Cloudflare Workers + D1: catalogue, basket, Stripe
|
|
4
|
+
Checkout and webhook, orders, Royal Mail Click & Drop, order and dispatch emails, packing, the
|
|
5
|
+
shop pages and the admin order pages.
|
|
6
|
+
|
|
7
|
+
Published as source (no build step): the consuming app compiles it.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
pnpm add -E @zeyolabs/shopping @zeyolabs/ui @zeyolabs/kit
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Using it in an app
|
|
16
|
+
|
|
17
|
+
1. Register the site's config once on the server: `configureShopping({...})` (brand, shipping,
|
|
18
|
+
order prefix, `db`, secret names, `mail`), imported by `hooks.server.ts`, and pass its secret
|
|
19
|
+
names to `runWithContext`.
|
|
20
|
+
2. In the root layout, `setShoppingClient({ imageBaseUrl, siteName, siteUrl, supportEmail })`.
|
|
21
|
+
3. Thin routes: render the pages (`<Basket startCheckout={...} />`, `<Shop description intro shop={data.shop} />`)
|
|
22
|
+
and re-export handlers and loads from `@zeyolabs/shopping/routes/*`.
|
|
23
|
+
4. Tailwind must scan the components: `@source "../node_modules/@zeyolabs/shopping";`.
|
|
24
|
+
5. Generate D1 migrations in the app from `@zeyolabs/shopping/db/schema` (drizzle-kit).
|
|
25
|
+
|
|
26
|
+
## Versions
|
|
27
|
+
|
|
28
|
+
Every merge to `main` publishes a new version (`.github/workflows/publish.yaml`); there is no
|
|
29
|
+
release step. Versions are `YYYY.M.N` in London time: `N` counts that month's releases and resets
|
|
30
|
+
each month (`2026.11.7`, then `2026.12.0`). CI sets the version and pushes the `vYYYY.M.N` tag;
|
|
31
|
+
docs- or CI-only changes don't publish. Publishing uses npm trusted publishing (no stored token).
|
|
32
|
+
|
|
33
|
+
## Checks
|
|
34
|
+
|
|
35
|
+
`pnpm check`: prettier, eslint, vitest.
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { resolve } from '$app/paths';
|
|
3
|
+
import { page } from '$app/state';
|
|
4
|
+
import {
|
|
5
|
+
createOrderLabel,
|
|
6
|
+
getOrder,
|
|
7
|
+
parcelForm,
|
|
8
|
+
shipForm,
|
|
9
|
+
updateOrderForm,
|
|
10
|
+
} from '../remote/admin-orders.remote';
|
|
11
|
+
import { OrderStepSchema, ParcelSchema, ShipSchema } from './orders.schema';
|
|
12
|
+
import { formatMoney } from '../money';
|
|
13
|
+
import { trackingUrl } from '../order-links';
|
|
14
|
+
import { toast } from 'svelte-sonner';
|
|
15
|
+
import { PACKAGE_FORMATS } from '../db/schema';
|
|
16
|
+
import { FORMAT_LABELS, packingStage, STAGE_COLORS, STAGE_LABELS } from '../packing';
|
|
17
|
+
import Badge from '@zeyolabs/ui/badge/Badge.svelte';
|
|
18
|
+
import Boundary from '@zeyolabs/ui/boundary/Boundary.svelte';
|
|
19
|
+
import { Form, NumberField, SelectField, TextField } from '@zeyolabs/ui/form';
|
|
20
|
+
import Button from '@zeyolabs/ui/form/Button.svelte';
|
|
21
|
+
|
|
22
|
+
const number = $derived(page.params.number ?? '');
|
|
23
|
+
|
|
24
|
+
let printing = $state(false);
|
|
25
|
+
|
|
26
|
+
/** Creates the label in Click & Drop, then opens the PDF to print. */
|
|
27
|
+
async function printLabel(orderNumber: string) {
|
|
28
|
+
printing = true;
|
|
29
|
+
try {
|
|
30
|
+
await createOrderLabel(orderNumber);
|
|
31
|
+
window.open(resolve('/orders/[number]/label', { number: orderNumber }), '_blank');
|
|
32
|
+
} catch (error) {
|
|
33
|
+
toast.error(error instanceof Error ? error.message : 'Could not create the label.');
|
|
34
|
+
} finally {
|
|
35
|
+
printing = false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const date = new Intl.DateTimeFormat('en-GB', {
|
|
40
|
+
dateStyle: 'medium',
|
|
41
|
+
timeStyle: 'short',
|
|
42
|
+
timeZone: 'Europe/London',
|
|
43
|
+
});
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
<svelte:head><title>{number} · Admin</title></svelte:head>
|
|
47
|
+
|
|
48
|
+
<p><a href={resolve('/orders')}>← Orders</a></p>
|
|
49
|
+
|
|
50
|
+
<Boundary message="Could not load this order." statusMessages={{ 404: 'Order not found.' }}>
|
|
51
|
+
{let order = $derived(await getOrder(number))}
|
|
52
|
+
<div class="mt-4 flex flex-wrap items-center gap-3">
|
|
53
|
+
<h1 class="text-2xl font-semibold">{order.number}</h1>
|
|
54
|
+
<Badge
|
|
55
|
+
color={order.status === 'paid'
|
|
56
|
+
? 'warning'
|
|
57
|
+
: order.status === 'shipped'
|
|
58
|
+
? 'success'
|
|
59
|
+
: 'neutral'}
|
|
60
|
+
>
|
|
61
|
+
{order.status === 'paid' ? 'To ship' : order.status}
|
|
62
|
+
</Badge>
|
|
63
|
+
</div>
|
|
64
|
+
<p class="mt-1 text-sm text-[var(--ink-muted)]">
|
|
65
|
+
Placed {date.format(order.createdAt)}{order.paidAt
|
|
66
|
+
? ` · paid ${date.format(order.paidAt)}`
|
|
67
|
+
: ''}
|
|
68
|
+
</p>
|
|
69
|
+
|
|
70
|
+
<div class="mt-6 grid gap-6 md:grid-cols-2">
|
|
71
|
+
<section class="rounded-xl border border-[var(--line)] bg-white p-5">
|
|
72
|
+
<h2 class="font-semibold">Ship to</h2>
|
|
73
|
+
<address class="mt-2 not-italic select-all">
|
|
74
|
+
{order.address.name}<br />
|
|
75
|
+
{order.address.line1}<br />
|
|
76
|
+
{#if order.address.line2}{order.address.line2}<br />{/if}
|
|
77
|
+
{order.address.city}<br />
|
|
78
|
+
{order.address.postcode}<br />
|
|
79
|
+
{order.address.country}
|
|
80
|
+
</address>
|
|
81
|
+
<p class="mt-3 text-sm">
|
|
82
|
+
<a href="mailto:{order.email}">{order.email}</a>{order.address.phone
|
|
83
|
+
? ` · ${order.address.phone}`
|
|
84
|
+
: ''}
|
|
85
|
+
</p>
|
|
86
|
+
<p class="mt-3 text-sm text-[var(--ink-muted)]">
|
|
87
|
+
Service: {order.shippingService ?? 'n/a'}
|
|
88
|
+
{#if order.royalMailOrderId}· Click & Drop order {order.royalMailOrderId}{/if}
|
|
89
|
+
</p>
|
|
90
|
+
</section>
|
|
91
|
+
|
|
92
|
+
<section class="rounded-xl border border-[var(--line)] bg-white p-5">
|
|
93
|
+
<h2 class="font-semibold">Items</h2>
|
|
94
|
+
<ul class="mt-2 divide-y divide-[var(--line)]">
|
|
95
|
+
{#each order.items as item (item.id)}
|
|
96
|
+
<li class="flex justify-between gap-4 py-2">
|
|
97
|
+
<span>{item.name} <span class="text-[var(--ink-muted)]">× {item.quantity}</span></span>
|
|
98
|
+
<span>{formatMoney(item.quantity * item.unitPricePence)}</span>
|
|
99
|
+
</li>
|
|
100
|
+
{/each}
|
|
101
|
+
</ul>
|
|
102
|
+
<dl class="mt-3 space-y-1 text-sm">
|
|
103
|
+
<div class="flex justify-between">
|
|
104
|
+
<dt>Subtotal</dt>
|
|
105
|
+
<dd>{formatMoney(order.subtotalPence)}</dd>
|
|
106
|
+
</div>
|
|
107
|
+
{#if order.discountPence > 0}
|
|
108
|
+
<div class="flex justify-between">
|
|
109
|
+
<dt>Discount</dt>
|
|
110
|
+
<dd>-{formatMoney(order.discountPence)}</dd>
|
|
111
|
+
</div>
|
|
112
|
+
{/if}
|
|
113
|
+
<div class="flex justify-between">
|
|
114
|
+
<dt>Delivery</dt>
|
|
115
|
+
<dd>{order.shippingPence === 0 ? 'Free' : formatMoney(order.shippingPence)}</dd>
|
|
116
|
+
</div>
|
|
117
|
+
<div class="flex justify-between font-semibold">
|
|
118
|
+
<dt>Total</dt>
|
|
119
|
+
<dd>{formatMoney(order.totalPence)}</dd>
|
|
120
|
+
</div>
|
|
121
|
+
</dl>
|
|
122
|
+
</section>
|
|
123
|
+
</div>
|
|
124
|
+
|
|
125
|
+
{#if order.status === 'paid'}
|
|
126
|
+
{let stage = $derived(packingStage(order))}
|
|
127
|
+
<section class="mt-6 max-w-xl rounded-xl border border-[var(--line)] bg-white p-5">
|
|
128
|
+
<div class="flex flex-wrap items-center gap-3">
|
|
129
|
+
<h2 class="font-semibold">Packing</h2>
|
|
130
|
+
<Badge color={STAGE_COLORS[stage]}>{STAGE_LABELS[stage]}</Badge>
|
|
131
|
+
</div>
|
|
132
|
+
<p class="mt-1 text-sm text-[var(--ink-muted)]">
|
|
133
|
+
{#if order.heldAt}On hold since {date.format(order.heldAt)}.{/if}
|
|
134
|
+
{#if order.packingStartedAt}Packing started {date.format(order.packingStartedAt)}.{/if}
|
|
135
|
+
{#if order.packedAt}Packed {date.format(order.packedAt)}.{/if}
|
|
136
|
+
{#if order.labelCreatedAt}Label created {date.format(order.labelCreatedAt)}.{/if}
|
|
137
|
+
</p>
|
|
138
|
+
<Form
|
|
139
|
+
class="mt-4 flex flex-wrap gap-2"
|
|
140
|
+
form={updateOrderForm}
|
|
141
|
+
hidden={{ number: order.number }}
|
|
142
|
+
schema={OrderStepSchema}
|
|
143
|
+
>
|
|
144
|
+
{#if stage === 'new'}
|
|
145
|
+
<Button {...updateOrderForm.fields.step.as('submit', 'start')} form={updateOrderForm}
|
|
146
|
+
>Start packing</Button
|
|
147
|
+
>
|
|
148
|
+
{:else if stage === 'packing'}
|
|
149
|
+
<Button
|
|
150
|
+
{...updateOrderForm.fields.step.as('submit', 'pack')}
|
|
151
|
+
form={updateOrderForm}
|
|
152
|
+
variant="secondary"
|
|
153
|
+
>
|
|
154
|
+
Mark packed
|
|
155
|
+
</Button>
|
|
156
|
+
{:else if stage === 'packed' || stage === 'label'}
|
|
157
|
+
<Button disabled={printing} onclick={() => printLabel(order.number)}>
|
|
158
|
+
{order.labelCreatedAt ? 'Reprint label' : 'Print label'}
|
|
159
|
+
</Button>
|
|
160
|
+
{/if}
|
|
161
|
+
{#if stage === 'held'}
|
|
162
|
+
<Button
|
|
163
|
+
{...updateOrderForm.fields.step.as('submit', 'release')}
|
|
164
|
+
form={updateOrderForm}
|
|
165
|
+
variant="secondary"
|
|
166
|
+
>
|
|
167
|
+
Release hold
|
|
168
|
+
</Button>
|
|
169
|
+
{:else if stage !== 'label'}
|
|
170
|
+
<Button
|
|
171
|
+
{...updateOrderForm.fields.step.as('submit', 'hold')}
|
|
172
|
+
form={updateOrderForm}
|
|
173
|
+
variant="secondary"
|
|
174
|
+
>
|
|
175
|
+
Put on hold
|
|
176
|
+
</Button>
|
|
177
|
+
{/if}
|
|
178
|
+
</Form>
|
|
179
|
+
|
|
180
|
+
<p class="mt-3 text-sm text-[var(--ink-muted)]">
|
|
181
|
+
Printing the label sends the saved parcel details to Royal Mail and marks the order packed.
|
|
182
|
+
{#if order.labelCreatedAt}A label is already made, so later parcel changes will not change
|
|
183
|
+
it.{/if}
|
|
184
|
+
</p>
|
|
185
|
+
|
|
186
|
+
<h3 class="mt-6 font-medium">Parcel</h3>
|
|
187
|
+
<p class="mt-1 text-sm text-[var(--ink-muted)]">
|
|
188
|
+
{order.weightGrams === null
|
|
189
|
+
? `Estimated ${order.estimatedWeightGrams} g from the items. Weigh the parcel and correct it.`
|
|
190
|
+
: 'Weight and size go on the Royal Mail label.'}
|
|
191
|
+
</p>
|
|
192
|
+
<Form
|
|
193
|
+
class="mt-3 grid gap-4"
|
|
194
|
+
defaults={{
|
|
195
|
+
format: order.packageFormat,
|
|
196
|
+
heightMm: order.packageHeightMm ?? undefined,
|
|
197
|
+
lengthMm: order.packageLengthMm ?? undefined,
|
|
198
|
+
weightGrams: order.weightGrams ?? (order.estimatedWeightGrams || undefined),
|
|
199
|
+
widthMm: order.packageWidthMm ?? undefined,
|
|
200
|
+
}}
|
|
201
|
+
form={parcelForm}
|
|
202
|
+
hidden={{ number: order.number }}
|
|
203
|
+
reset={false}
|
|
204
|
+
schema={ParcelSchema}
|
|
205
|
+
>
|
|
206
|
+
<NumberField name="weightGrams" form={parcelForm} inputmode="numeric"
|
|
207
|
+
>Weight (g)</NumberField
|
|
208
|
+
>
|
|
209
|
+
<SelectField
|
|
210
|
+
name="format"
|
|
211
|
+
form={parcelForm}
|
|
212
|
+
getLabel={(f) => FORMAT_LABELS[f]}
|
|
213
|
+
items={PACKAGE_FORMATS}
|
|
214
|
+
placeholder="Package format"
|
|
215
|
+
/>
|
|
216
|
+
<div class="grid grid-cols-3 gap-3">
|
|
217
|
+
<NumberField name="lengthMm" form={parcelForm} inputmode="numeric" optional>
|
|
218
|
+
Length (mm)
|
|
219
|
+
</NumberField>
|
|
220
|
+
<NumberField name="widthMm" form={parcelForm} inputmode="numeric" optional>
|
|
221
|
+
Width (mm)
|
|
222
|
+
</NumberField>
|
|
223
|
+
<NumberField name="heightMm" form={parcelForm} inputmode="numeric" optional>
|
|
224
|
+
Height (mm)
|
|
225
|
+
</NumberField>
|
|
226
|
+
</div>
|
|
227
|
+
<div><Button form={parcelForm}>Save parcel</Button></div>
|
|
228
|
+
</Form>
|
|
229
|
+
</section>
|
|
230
|
+
{/if}
|
|
231
|
+
|
|
232
|
+
<section class="mt-6 max-w-xl rounded-xl border border-[var(--line)] bg-white p-5">
|
|
233
|
+
<h2 class="font-semibold">Dispatch</h2>
|
|
234
|
+
{#if order.status === 'paid' || order.status === 'shipped'}
|
|
235
|
+
{#if order.shippedAt}
|
|
236
|
+
<p class="mt-1 text-sm text-[var(--ink-muted)]">
|
|
237
|
+
Dispatched {date.format(order.shippedAt)}.
|
|
238
|
+
{#if order.dispatchEmailedAt}
|
|
239
|
+
Customer emailed {date.format(order.dispatchEmailedAt)}.
|
|
240
|
+
{:else}
|
|
241
|
+
The customer has <strong>not</strong> been emailed.
|
|
242
|
+
{/if}
|
|
243
|
+
</p>
|
|
244
|
+
{:else}
|
|
245
|
+
<p class="mt-1 text-sm text-[var(--ink-muted)]">
|
|
246
|
+
Enter the Royal Mail tracking number (leave blank if untracked), then mark it dispatched.
|
|
247
|
+
The customer is emailed straight away and their order page updates.
|
|
248
|
+
</p>
|
|
249
|
+
{/if}
|
|
250
|
+
<Form
|
|
251
|
+
class="mt-4 grid gap-4"
|
|
252
|
+
defaults={{ trackingNumber: order.trackingNumber ?? '' }}
|
|
253
|
+
form={shipForm}
|
|
254
|
+
hidden={{ number: order.number }}
|
|
255
|
+
reset={false}
|
|
256
|
+
schema={ShipSchema}
|
|
257
|
+
>
|
|
258
|
+
<TextField name="trackingNumber" autocapitalize="characters" form={shipForm} optional>
|
|
259
|
+
Tracking number
|
|
260
|
+
</TextField>
|
|
261
|
+
<div>
|
|
262
|
+
<Button form={shipForm} loadingMessage="Sending…">
|
|
263
|
+
{order.shippedAt ? 'Save and resend email' : 'Mark as dispatched and email customer'}
|
|
264
|
+
</Button>
|
|
265
|
+
</div>
|
|
266
|
+
</Form>
|
|
267
|
+
{:else}
|
|
268
|
+
<p class="mt-1 text-sm">This order is {order.status}, so it cannot be dispatched.</p>
|
|
269
|
+
{/if}
|
|
270
|
+
|
|
271
|
+
{#if order.trackingNumber}
|
|
272
|
+
<p class="mt-4 text-sm">
|
|
273
|
+
<!-- eslint-disable-next-line svelte/no-navigation-without-resolve -- external Royal Mail URL -->
|
|
274
|
+
<a href={trackingUrl(order.trackingNumber)} rel="noopener" target="_blank"
|
|
275
|
+
>Track {order.trackingNumber} on Royal Mail</a
|
|
276
|
+
>
|
|
277
|
+
</p>
|
|
278
|
+
{/if}
|
|
279
|
+
{#if order.customerUrl}
|
|
280
|
+
<p class="mt-2 text-sm">
|
|
281
|
+
<!-- eslint-disable-next-line svelte/no-navigation-without-resolve -- absolute URL on the shop domain -->
|
|
282
|
+
<a href={order.customerUrl} rel="noopener" target="_blank">Customer's order page</a>
|
|
283
|
+
</p>
|
|
284
|
+
{/if}
|
|
285
|
+
</section>
|
|
286
|
+
{#snippet pending()}
|
|
287
|
+
<p class="mt-6 text-[var(--ink-muted)]">Loading…</p>
|
|
288
|
+
{/snippet}
|
|
289
|
+
</Boundary>
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { goto } from '$app/navigation';
|
|
3
|
+
import { resolve } from '$app/paths';
|
|
4
|
+
import { page } from '$app/state';
|
|
5
|
+
import { FILTER_LABELS, ORDER_FILTERS, type OrderFilter } from './filters';
|
|
6
|
+
import { getOrderCounts, getOrders } from '../remote/admin-orders.remote';
|
|
7
|
+
import { formatMoney } from '../money';
|
|
8
|
+
import { SvelteURLSearchParams } from 'svelte/reactivity';
|
|
9
|
+
import { packingStage, STAGE_COLORS, STAGE_LABELS } from '../packing';
|
|
10
|
+
import Badge from '@zeyolabs/ui/badge/Badge.svelte';
|
|
11
|
+
import Boundary from '@zeyolabs/ui/boundary/Boundary.svelte';
|
|
12
|
+
|
|
13
|
+
const filter = $derived.by((): OrderFilter => {
|
|
14
|
+
const value = page.url.searchParams.get('filter');
|
|
15
|
+
return ORDER_FILTERS.find((f) => f === value) ?? 'to-ship';
|
|
16
|
+
});
|
|
17
|
+
const q = $derived(page.url.searchParams.get('q') ?? '');
|
|
18
|
+
|
|
19
|
+
const date = new Intl.DateTimeFormat('en-GB', {
|
|
20
|
+
dateStyle: 'medium',
|
|
21
|
+
timeStyle: 'short',
|
|
22
|
+
timeZone: 'Europe/London',
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
function href(next: { filter?: OrderFilter; q?: string }) {
|
|
26
|
+
const params = new SvelteURLSearchParams();
|
|
27
|
+
const f = next.filter ?? filter;
|
|
28
|
+
const query = next.q ?? q;
|
|
29
|
+
if (f !== 'to-ship') params.set('filter', f);
|
|
30
|
+
if (query) params.set('q', query);
|
|
31
|
+
const search = params.toString();
|
|
32
|
+
return `${resolve('/orders')}${search ? `?${search}` : ''}`;
|
|
33
|
+
}
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
<svelte:head><title>Orders · Admin</title></svelte:head>
|
|
37
|
+
|
|
38
|
+
<h1 class="text-2xl font-semibold">Orders</h1>
|
|
39
|
+
|
|
40
|
+
<nav class="mt-4 flex flex-wrap items-center gap-2" aria-label="Order filter">
|
|
41
|
+
{#each ORDER_FILTERS as f (f)}
|
|
42
|
+
<!-- eslint-disable svelte/no-navigation-without-resolve -- href() resolves the path -->
|
|
43
|
+
<a
|
|
44
|
+
class="rounded-full border px-3 py-1 text-sm no-underline {f === filter
|
|
45
|
+
? 'border-[var(--leaf-dark)] bg-[var(--leaf-dark)] text-white'
|
|
46
|
+
: 'border-[var(--line)] bg-white text-[var(--ink)]'}"
|
|
47
|
+
aria-current={f === filter ? 'page' : undefined}
|
|
48
|
+
href={href({ filter: f })}
|
|
49
|
+
>
|
|
50
|
+
{FILTER_LABELS[f]}
|
|
51
|
+
({(await getOrderCounts())[f]})
|
|
52
|
+
</a>
|
|
53
|
+
<!-- eslint-enable svelte/no-navigation-without-resolve -->
|
|
54
|
+
{/each}
|
|
55
|
+
<form
|
|
56
|
+
class="ml-auto"
|
|
57
|
+
onsubmit={(event) => {
|
|
58
|
+
event.preventDefault();
|
|
59
|
+
const input = new FormData(event.currentTarget).get('q');
|
|
60
|
+
// eslint-disable-next-line svelte/no-navigation-without-resolve -- href() already resolves the path
|
|
61
|
+
goto(href({ q: typeof input === 'string' ? input.trim() : '' }));
|
|
62
|
+
}}
|
|
63
|
+
>
|
|
64
|
+
<input
|
|
65
|
+
name="q"
|
|
66
|
+
class="w-56 rounded-md border border-[var(--line)] px-3 py-1.5 text-sm"
|
|
67
|
+
placeholder="Order, email or name"
|
|
68
|
+
type="search"
|
|
69
|
+
value={q}
|
|
70
|
+
/>
|
|
71
|
+
</form>
|
|
72
|
+
</nav>
|
|
73
|
+
|
|
74
|
+
<Boundary message="Could not load orders.">
|
|
75
|
+
{let rows = $derived(await getOrders({ filter, q }))}
|
|
76
|
+
{#if rows.length === 0}
|
|
77
|
+
<p class="mt-6 text-[var(--ink-muted)]">
|
|
78
|
+
{filter === 'to-ship' && !q ? 'Nothing to ship. All caught up.' : 'No orders match.'}
|
|
79
|
+
</p>
|
|
80
|
+
{:else}
|
|
81
|
+
<div class="mt-4 overflow-x-auto rounded-xl border border-[var(--line)] bg-white">
|
|
82
|
+
<table class="w-full text-left text-sm">
|
|
83
|
+
<thead class="border-b border-[var(--line)] text-[var(--ink-muted)]">
|
|
84
|
+
<tr>
|
|
85
|
+
<th class="px-4 py-2 font-medium">Order</th>
|
|
86
|
+
<th class="px-4 py-2 font-medium">Placed</th>
|
|
87
|
+
<th class="px-4 py-2 font-medium">Customer</th>
|
|
88
|
+
<th class="px-4 py-2 text-right font-medium">Total</th>
|
|
89
|
+
<th class="px-4 py-2 font-medium">Status</th>
|
|
90
|
+
</tr>
|
|
91
|
+
</thead>
|
|
92
|
+
<tbody class="divide-y divide-[var(--line)]">
|
|
93
|
+
{#each rows as row (row.number)}
|
|
94
|
+
<tr>
|
|
95
|
+
<td class="px-4 py-2 font-medium">
|
|
96
|
+
<a href={resolve('/orders/[number]', { number: row.number })}>{row.number}</a>
|
|
97
|
+
</td>
|
|
98
|
+
<td class="px-4 py-2 whitespace-nowrap">{date.format(row.createdAt)}</td>
|
|
99
|
+
<td class="px-4 py-2">
|
|
100
|
+
{row.name}
|
|
101
|
+
<span class="block text-xs text-[var(--ink-muted)]"
|
|
102
|
+
>{row.email} · {row.country}</span
|
|
103
|
+
>
|
|
104
|
+
</td>
|
|
105
|
+
<td class="px-4 py-2 text-right whitespace-nowrap">{formatMoney(row.totalPence)}</td>
|
|
106
|
+
<td class="px-4 py-2">
|
|
107
|
+
{#if row.status === 'paid' || row.status === 'shipped'}
|
|
108
|
+
{let stage = $derived(packingStage(row))}
|
|
109
|
+
<Badge color={STAGE_COLORS[stage]}>{STAGE_LABELS[stage]}</Badge>
|
|
110
|
+
{:else}
|
|
111
|
+
<Badge color="neutral">{row.status}</Badge>
|
|
112
|
+
{/if}
|
|
113
|
+
</td>
|
|
114
|
+
</tr>
|
|
115
|
+
{/each}
|
|
116
|
+
</tbody>
|
|
117
|
+
</table>
|
|
118
|
+
</div>
|
|
119
|
+
{/if}
|
|
120
|
+
{#snippet pending()}
|
|
121
|
+
<p class="mt-6 text-[var(--ink-muted)]">Loading…</p>
|
|
122
|
+
{/snippet}
|
|
123
|
+
</Boundary>
|
package/admin/filters.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
import { PACKAGE_FORMATS } from '../db/schema';
|
|
4
|
+
import { ORDER_FILTERS } from './filters';
|
|
5
|
+
|
|
6
|
+
export const OrderListSchema = z.object({
|
|
7
|
+
filter: z.enum(ORDER_FILTERS).default('to-ship'),
|
|
8
|
+
q: z.string().max(100).default(''),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
/** Royal Mail tracking numbers are letters and digits (e.g. AB123456789GB); blank means untracked. */
|
|
12
|
+
export const ShipSchema = z.object({
|
|
13
|
+
number: z.string().regex(/^[A-Z]{2,5}-[A-Z0-9]{6}$/, 'invalid order number'),
|
|
14
|
+
trackingNumber: z
|
|
15
|
+
.string()
|
|
16
|
+
.transform((value) => value.replace(/\s+/g, '').toUpperCase())
|
|
17
|
+
.pipe(
|
|
18
|
+
z.union([
|
|
19
|
+
z.literal(''),
|
|
20
|
+
z.string().regex(/^[A-Z0-9]{8,20}$/, 'enter the tracking number, letters and digits only'),
|
|
21
|
+
])
|
|
22
|
+
),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const OrderNumber = z.string().regex(/^[A-Z]{2,5}-[A-Z0-9]{6}$/, 'invalid order number');
|
|
26
|
+
|
|
27
|
+
export const ORDER_STEPS = ['start', 'pack', 'hold', 'release'] as const;
|
|
28
|
+
export type OrderStep = (typeof ORDER_STEPS)[number];
|
|
29
|
+
|
|
30
|
+
/** One form moves an order through packing: the order, and which button was pressed. */
|
|
31
|
+
export const OrderStepSchema = z.object({ number: OrderNumber, step: z.enum(ORDER_STEPS) });
|
|
32
|
+
|
|
33
|
+
const Millimetres = z.number().int().min(1).max(1000);
|
|
34
|
+
|
|
35
|
+
/** Click & Drop takes parcels up to 30 kg. The three measurements go together or not at all. */
|
|
36
|
+
export const ParcelSchema = z
|
|
37
|
+
.object({
|
|
38
|
+
format: z.enum(PACKAGE_FORMATS),
|
|
39
|
+
heightMm: Millimetres.optional(),
|
|
40
|
+
lengthMm: Millimetres.optional(),
|
|
41
|
+
number: OrderNumber,
|
|
42
|
+
weightGrams: z.number().int().min(1, 'enter the weight in grams').max(30_000),
|
|
43
|
+
widthMm: Millimetres.optional(),
|
|
44
|
+
})
|
|
45
|
+
.refine(
|
|
46
|
+
({ heightMm, lengthMm, widthMm }) =>
|
|
47
|
+
[heightMm, lengthMm, widthMm].every((v) => v === undefined) ||
|
|
48
|
+
[heightMm, lengthMm, widthMm].every((v) => v !== undefined),
|
|
49
|
+
{ message: 'give length, width and height, or leave all three blank', path: ['lengthMm'] }
|
|
50
|
+
);
|
package/client.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What pages need in the browser. The server config in `config.ts` never reaches the client, so
|
|
5
|
+
* the app hands these over once from its root layout with `setShoppingClient`.
|
|
6
|
+
*/
|
|
7
|
+
export type ShoppingClient = {
|
|
8
|
+
imageBaseUrl: string;
|
|
9
|
+
siteName: string;
|
|
10
|
+
siteUrl: string;
|
|
11
|
+
supportEmail: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const KEY = Symbol('@zeyolabs/shopping-client');
|
|
15
|
+
|
|
16
|
+
export function setShoppingClient(client: ShoppingClient): void {
|
|
17
|
+
setContext(KEY, client);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function shoppingClient(): ShoppingClient {
|
|
21
|
+
const client = getContext<ShoppingClient | undefined>(KEY);
|
|
22
|
+
if (!client) throw new Error('@zeyolabs/shopping: call setShoppingClient() in the root layout');
|
|
23
|
+
return client;
|
|
24
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { page } from '$app/state';
|
|
3
|
+
|
|
4
|
+
import { shoppingClient } from '../client';
|
|
5
|
+
|
|
6
|
+
let { description, title }: { description: string; title: string } = $props();
|
|
7
|
+
|
|
8
|
+
const client = shoppingClient();
|
|
9
|
+
// Canonical ignores the query string, so `?show=N` deep links don't count as duplicate pages.
|
|
10
|
+
const canonical = $derived(`${client.siteUrl}${page.url.pathname}`);
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<svelte:head>
|
|
14
|
+
<title>{title} | {client.siteName}</title>
|
|
15
|
+
<meta name="description" content={description} />
|
|
16
|
+
<link href={canonical} rel="canonical" />
|
|
17
|
+
<meta content={client.siteName} property="og:site_name" />
|
|
18
|
+
<meta content={title} property="og:title" />
|
|
19
|
+
<meta content={description} property="og:description" />
|
|
20
|
+
<meta content={canonical} property="og:url" />
|
|
21
|
+
<meta content="website" property="og:type" />
|
|
22
|
+
</svelte:head>
|
|
23
|
+
|
|
24
|
+
<h1 class="mb-10 text-4xl font-semibold tracking-tight text-[var(--primary-strong)]">{title}</h1>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { resolve } from '$app/paths';
|
|
3
|
+
import { shoppingClient } from '../client';
|
|
4
|
+
import { productImageUrl } from '../images';
|
|
5
|
+
import { formatMoney } from '../money';
|
|
6
|
+
|
|
7
|
+
type Product = {
|
|
8
|
+
comparePricePence: null | number;
|
|
9
|
+
cover?: { alt: string; src: string };
|
|
10
|
+
name: string;
|
|
11
|
+
pricePence: number;
|
|
12
|
+
slug: string;
|
|
13
|
+
stock: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
let { product }: { product: Product } = $props();
|
|
17
|
+
|
|
18
|
+
const client = shoppingClient();
|
|
19
|
+
|
|
20
|
+
const onSale = $derived(
|
|
21
|
+
product.comparePricePence !== null && product.comparePricePence > product.pricePence
|
|
22
|
+
);
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<!--
|
|
26
|
+
Concentric corners: the card is 16px (rounded-2xl) with 8px of padding, so the photo inside is
|
|
27
|
+
rounded-md (16 - 8 = 8px, on this project's radius scale) and its corners follow the card's curve instead of poking into it.
|
|
28
|
+
-->
|
|
29
|
+
<a
|
|
30
|
+
class="group block rounded-2xl border border-[var(--bd)] bg-white p-2 text-[var(--tx)] no-underline transition hover:border-[var(--primary-strong)] hover:text-[var(--tx)] hover:shadow-md"
|
|
31
|
+
href={resolve('/product/[slug]', { slug: product.slug })}
|
|
32
|
+
>
|
|
33
|
+
<div class="relative aspect-square overflow-hidden rounded-md bg-[var(--sfh)]">
|
|
34
|
+
{#if product.cover}
|
|
35
|
+
<img
|
|
36
|
+
class="size-full object-cover transition duration-300 group-hover:scale-105 {product.stock <=
|
|
37
|
+
0
|
|
38
|
+
? 'opacity-60'
|
|
39
|
+
: ''}"
|
|
40
|
+
alt={product.cover.alt || product.name}
|
|
41
|
+
loading="lazy"
|
|
42
|
+
src={productImageUrl(client.imageBaseUrl, product.cover.src)}
|
|
43
|
+
/>
|
|
44
|
+
{/if}
|
|
45
|
+
{#if product.stock <= 0}
|
|
46
|
+
<span
|
|
47
|
+
class="absolute top-2 left-2 rounded-full bg-white/95 px-2.5 py-1 text-xs font-medium text-[var(--txs)] shadow-sm"
|
|
48
|
+
>Out of stock</span
|
|
49
|
+
>
|
|
50
|
+
{:else if onSale}
|
|
51
|
+
<span
|
|
52
|
+
class="absolute top-2 left-2 rounded-full bg-[var(--primary-strong)] px-2.5 py-1 text-xs font-medium text-white"
|
|
53
|
+
>Sale</span
|
|
54
|
+
>
|
|
55
|
+
{/if}
|
|
56
|
+
</div>
|
|
57
|
+
<div class="px-2 pt-3 pb-2">
|
|
58
|
+
<p class="leading-snug font-medium">{product.name}</p>
|
|
59
|
+
<p class="mt-1 text-[var(--txs)]">
|
|
60
|
+
<span class="font-medium text-[var(--tx)]">{formatMoney(product.pricePence)}</span>
|
|
61
|
+
{#if onSale}
|
|
62
|
+
<s class="ml-1 text-sm">{formatMoney(product.comparePricePence!)}</s>
|
|
63
|
+
{/if}
|
|
64
|
+
</p>
|
|
65
|
+
</div>
|
|
66
|
+
</a>
|