@zeyolabs/shopping 2026.11.37 → 2026.11.39

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.
Files changed (46) hide show
  1. package/basket/client.remote.ts +16 -2
  2. package/basket/cookies.server.ts +13 -1
  3. package/basket/library.ts +58 -0
  4. package/basket/pages/Basket.svelte +80 -30
  5. package/basket/service.server.ts +139 -6
  6. package/bundles/admin/BundleEditPage.svelte +7 -4
  7. package/bundles/admin/BundleForm.svelte +50 -11
  8. package/bundles/admin/BundlesPage.svelte +14 -10
  9. package/bundles/client.remote.ts +18 -6
  10. package/bundles/library.ts +51 -0
  11. package/bundles/pages/Bundle.svelte +233 -68
  12. package/bundles/schema.ts +13 -2
  13. package/bundles/service.server.ts +185 -33
  14. package/catalogue/admin/CategoriesPage.svelte +13 -10
  15. package/catalogue/admin/CategoryForm.svelte +4 -3
  16. package/catalogue/admin/ProductEditPage.svelte +7 -4
  17. package/catalogue/admin/ProductForm.svelte +31 -29
  18. package/catalogue/admin/ProductsPage.svelte +16 -14
  19. package/catalogue/client.remote.ts +23 -9
  20. package/catalogue/components/ProductCard.svelte +22 -14
  21. package/catalogue/library.ts +91 -0
  22. package/catalogue/loads.server.ts +8 -4
  23. package/catalogue/pages/Category.svelte +15 -6
  24. package/catalogue/pages/Product.svelte +164 -45
  25. package/catalogue/pages/Search.svelte +12 -7
  26. package/catalogue/pages/Shop.svelte +12 -12
  27. package/catalogue/service.server.ts +44 -9
  28. package/checkout/manager.server.ts +68 -16
  29. package/checkout/pages/CheckoutSuccess.svelte +10 -8
  30. package/customers/pages/Account.svelte +19 -15
  31. package/db/schema.ts +27 -3
  32. package/fulfilment/admin/OrderPage.svelte +60 -56
  33. package/fulfilment/admin/OrdersPage.svelte +26 -22
  34. package/orders/manager.server.ts +45 -11
  35. package/orders/pages/Order.svelte +35 -29
  36. package/orders/service.server.ts +25 -3
  37. package/package.json +5 -4
  38. package/reviews/admin/ReviewsPage.svelte +23 -19
  39. package/reviews/components/ReviewForm.svelte +3 -2
  40. package/reviews/components/Reviews.svelte +17 -13
  41. package/stock/admin/StockPage.svelte +12 -9
  42. package/stock/admin/StockRow.svelte +3 -2
  43. package/stock/service.server.ts +6 -1
  44. package/ui/PageLinks.svelte +45 -0
  45. package/ui/PageTitle.svelte +20 -4
  46. package/ui/Stars.svelte +3 -1
@@ -1,40 +1,44 @@
1
1
  <script lang="ts">
2
2
  import { resolve } from '$app/paths';
3
- import { getAdminBundles } from '../admin.remote';
4
3
  import Badge from '@zeyolabs/ui/badge/Badge.svelte';
5
4
  import Boundary from '@zeyolabs/ui/boundary/Boundary.svelte';
5
+ import Span from '@zeyolabs/ui/text/Span.svelte';
6
+ import Text from '@zeyolabs/ui/text/Text.svelte';
7
+ import Title from '@zeyolabs/ui/text/Title.svelte';
8
+
9
+ import { getAdminBundles } from '../admin.remote';
6
10
  </script>
7
11
 
8
12
  <svelte:head><title>Bundles · Admin</title></svelte:head>
9
13
 
10
- <p class="text-sm"><a href={resolve('/products')}>← Products</a></p>
14
+ <Text><a href={resolve('/products')}>← Products</a></Text>
11
15
  <div class="mt-2 flex items-center justify-between">
12
- <h1 class="text-2xl font-semibold">Bundles</h1>
16
+ <Title level={1} size="md">Bundles</Title>
13
17
  <a
14
- class="rounded-md bg-[var(--leaf-dark)] px-3 py-1.5 text-sm text-white no-underline"
18
+ class="rounded-md bg-[var(--acc)] px-3 py-1.5 text-sm text-white no-underline"
15
19
  href={resolve('/bundles/new')}>New bundle</a
16
20
  >
17
21
  </div>
18
- <p class="mt-1 text-sm text-[var(--ink-muted)]">
22
+ <Text class="mt-1" color="secondary">
19
23
  A bundle is built in steps; the customer's picks go into the basket as ordinary products at their
20
24
  own prices. Use a Stripe promotion code for a bundle discount.
21
- </p>
25
+ </Text>
22
26
 
23
27
  <Boundary message="Could not load bundles.">
24
28
  {let rows = $derived(await getAdminBundles())}
25
- <ul class="mt-4 divide-y divide-[var(--line)] rounded-xl border border-[var(--line)] bg-white">
29
+ <ul class="mt-4 divide-y divide-[var(--bd)] rounded-xl border border-[var(--bd)] bg-white">
26
30
  {#each rows as row (row.id)}
27
31
  <li class="px-4 py-2 text-sm">
28
32
  <a class="font-medium" href={resolve('/bundles/[id]', { id: String(row.id) })}>{row.name}</a
29
33
  >
30
- <span class="text-[var(--ink-muted)]">/bundle/{row.slug}</span>
34
+ <Span color="secondary">/bundle/{row.slug}</Span>
31
35
  {#if !row.active}<Badge color="neutral">inactive</Badge>{/if}
32
36
  </li>
33
37
  {:else}
34
- <li class="px-4 py-6 text-[var(--ink-muted)]">No bundles yet.</li>
38
+ <li class="px-4 py-6 text-[var(--txs)]">No bundles yet.</li>
35
39
  {/each}
36
40
  </ul>
37
41
  {#snippet pending()}
38
- <p class="mt-6 text-[var(--ink-muted)]">Loading…</p>
42
+ <Text class="mt-6" color="secondary">Loading…</Text>
39
43
  {/snippet}
40
44
  </Boundary>
@@ -4,7 +4,7 @@ import { Error404 } from '@zeyolabs/kit/utils/errors';
4
4
 
5
5
  import { getBasket } from '../basket/client.remote';
6
6
  import { BasketCookies } from '../basket/cookies.server';
7
- import { BasketCookie } from '../basket/library';
7
+ import { BasketBundles, BasketCookie } from '../basket/library';
8
8
  import { shopping } from '../config';
9
9
  import { ShopSchema } from '../utils/schemas';
10
10
  import { BundleChoiceSchema } from './schema';
@@ -17,13 +17,25 @@ export const getBundle = Remote.Query(ShopSchema.SlugParam, async (slug) => {
17
17
  return bundle;
18
18
  });
19
19
 
20
- /** Adds the chosen products to the basket as ordinary lines, after checking the choice. */
20
+ /**
21
+ * Checks the choice, then adds it to the basket: a kit or discounted bundle as one line (kept with
22
+ * its choices, re-priced on every read), a plain bundle as ordinary product lines.
23
+ */
21
24
  export const addBundleToBasket = Remote.Cmd(BundleChoiceSchema, async ({ bundleId, choices }) => {
22
25
  const service = new BundleService(shopping().db());
23
- const lines = await service.expand(bundleId, choices);
26
+ const resolved = await service.resolve(bundleId, choices);
24
27
  const { cookies } = getRequestEvent();
25
- let basket = BasketCookies.read(cookies);
26
- for (const [productId, quantity] of lines) basket = BasketCookie.add(basket, productId, quantity);
27
- BasketCookies.write(cookies, basket);
28
+ if (resolved.grouped) {
29
+ BasketCookies.writeBundles(
30
+ cookies,
31
+ BasketBundles.add(BasketCookies.readBundles(cookies), { bundleId, choices })
32
+ );
33
+ } else {
34
+ let basket = BasketCookies.read(cookies);
35
+ for (const part of resolved.parts) {
36
+ basket = BasketCookie.add(basket, part.productId, part.quantity);
37
+ }
38
+ BasketCookies.write(cookies, basket);
39
+ }
28
40
  await getBasket().refresh();
29
41
  });
@@ -0,0 +1,51 @@
1
+ /** Bundle editor values. Pure. */
2
+ export const BundleSku = {
3
+ /** The editor's text for a step's product: the reverse of `parse`. */
4
+ format(p: { pricePence: null | number; sku: string; slots: null | number }): string {
5
+ if (!p.slots) return p.sku;
6
+ return p.pricePence === null
7
+ ? `${p.sku}=${p.slots}`
8
+ : `${p.sku}=${p.slots}@${(p.pricePence / 100).toFixed(2)}`;
9
+ },
10
+
11
+ /**
12
+ * A step's product as the editor writes it: `SKU`, or for a kit base `SKU=16` (holds 16) or
13
+ * `SKU=35@37.00` (holds 35, and the kit costs £37.00 rather than the product's own price).
14
+ */
15
+ parse(entry: string): { pricePence: null | number; sku: string; slots: null | number } {
16
+ const match = /^(.+?)=(\d{1,3})(?:@(\d{1,5}(?:\.\d{1,2})?))?$/.exec(entry.trim());
17
+ if (!match) return { pricePence: null, sku: entry.trim(), slots: null };
18
+ const slots = Number(match[2]);
19
+ return {
20
+ pricePence: match[3] ? Math.round(Number(match[3]) * 100) : null,
21
+ sku: match[1].trim(),
22
+ slots: slots > 0 ? slots : null,
23
+ };
24
+ },
25
+ };
26
+
27
+ export type EncodedPart = { productId: number; quantity: number; singleUnits: boolean };
28
+
29
+ /**
30
+ * A grouped bundle's parts as one Stripe metadata value (500 characters at most): `id:qty:s` per
31
+ * part, `s` 1 for single units, joined by `|`. Pure.
32
+ */
33
+ export const BundleParts = {
34
+ decode(value: string | undefined): EncodedPart[] {
35
+ if (!value) return [];
36
+ return value.split('|').flatMap((chunk) => {
37
+ const [id, qty, single] = chunk.split(':').map(Number);
38
+ return Number.isInteger(id) && id > 0 && Number.isInteger(qty) && qty > 0
39
+ ? [{ productId: id, quantity: qty, singleUnits: single === 1 }]
40
+ : [];
41
+ });
42
+ },
43
+
44
+ encode(parts: readonly EncodedPart[]): string {
45
+ const value = parts
46
+ .map((p) => `${p.productId}:${p.quantity}:${p.singleUnits ? 1 : 0}`)
47
+ .join('|');
48
+ if (value.length > 500) throw new Error('Bundle has too many parts for Stripe metadata');
49
+ return value;
50
+ },
51
+ };
@@ -1,15 +1,21 @@
1
1
  <script lang="ts">
2
- import { ErrorMessage } from '../../utils/errors';
3
2
  import { goto } from '$app/navigation';
4
3
  import { resolve } from '$app/paths';
5
- import PageTitle from '../../ui/PageTitle.svelte';
6
- import { addBundleToBasket, type getBundle } from '../client.remote';
4
+ import Minus from '@lucide/svelte/icons/minus';
5
+ import Plus from '@lucide/svelte/icons/plus';
6
+ import Button from '@zeyolabs/ui/form/Button.svelte';
7
+ import Span from '@zeyolabs/ui/text/Span.svelte';
8
+ import Text from '@zeyolabs/ui/text/Text.svelte';
9
+ import Title from '@zeyolabs/ui/text/Title.svelte';
10
+ import { toast } from 'svelte-sonner';
11
+ import { SvelteMap, SvelteSet } from 'svelte/reactivity';
12
+
7
13
  import { shopContext } from '../../context';
14
+ import PageTitle from '../../ui/PageTitle.svelte';
15
+ import { ErrorMessage } from '../../utils/errors';
8
16
  import { Images } from '../../utils/images';
9
17
  import { Money } from '../../utils/money';
10
- import { SvelteMap, SvelteSet } from 'svelte/reactivity';
11
- import { toast } from 'svelte-sonner';
12
- import Button from '@zeyolabs/ui/form/Button.svelte';
18
+ import { addBundleToBasket, type getBundle } from '../client.remote';
13
19
 
14
20
  let {
15
21
  bundle,
@@ -18,16 +24,32 @@
18
24
  bundle: Awaited<ReturnType<typeof getBundle>>;
19
25
  } = $props();
20
26
 
27
+ type Option = Step['products'][number];
28
+ type Step = (typeof bundle.steps)[number];
29
+
21
30
  const context = shopContext();
22
- /** Step id to the chosen product ids. */
31
+ /** pick / base / cover steps: step id to the chosen product ids. */
23
32
  const picked = new SvelteMap<number, SvelteSet<number>>();
33
+ /** fill steps: step id to product id to slots taken. */
34
+ const filled = new SvelteMap<number, SvelteMap<number, number>>();
24
35
  let busy = $state(false);
25
36
 
37
+ const kit = $derived(bundle.steps.some((s) => s.role === 'base'));
26
38
  const chosen = (stepId: number) => picked.get(stepId) ?? new SvelteSet<number>();
39
+ const counts = (stepId: number) => filled.get(stepId) ?? new SvelteMap<number, number>();
40
+ const used = (stepId: number) => [...counts(stepId).values()].reduce((a, b) => a + b, 0);
27
41
 
28
- function toggle(step: { id: number; maxChoices: number }, productId: number) {
42
+ /** Slots of the chosen base: what fill and cover steps work to. */
43
+ const slots = $derived.by(() => {
44
+ const base = bundle.steps.find((s) => s.role === 'base');
45
+ const id = base && [...chosen(base.id)][0];
46
+ return base?.products.find((p) => p.id === id)?.slots ?? 0;
47
+ });
48
+
49
+ function toggle(step: Step, productId: number) {
29
50
  const set = picked.get(step.id) ?? new SvelteSet<number>();
30
- if (step.maxChoices === 1) {
51
+ const single = step.role !== 'pick' || step.maxChoices === 1;
52
+ if (single) {
31
53
  const was = set.has(productId);
32
54
  set.clear();
33
55
  if (!was) set.add(productId);
@@ -37,26 +59,107 @@
37
59
  set.add(productId);
38
60
  }
39
61
  picked.set(step.id, set);
62
+ if (step.role === 'base') filled.clear();
63
+ }
64
+
65
+ function adjust(step: Step, product: Option, by: -1 | 1) {
66
+ const map = filled.get(step.id) ?? new SvelteMap<number, number>();
67
+ const next = (map.get(product.id) ?? 0) + by;
68
+ if (next <= 0) map.delete(product.id);
69
+ else map.set(product.id, next);
70
+ filled.set(step.id, map);
40
71
  }
41
72
 
73
+ const canAdd = (step: Step, product: Option) => {
74
+ const n = counts(step.id).get(product.id) ?? 0;
75
+ return (
76
+ slots > 0 &&
77
+ used(step.id) < slots &&
78
+ n < product.units &&
79
+ (step.maxPerChoice === null || n < step.maxPerChoice)
80
+ );
81
+ };
82
+
83
+ const out = (step: Step, product: Option) =>
84
+ step.role === 'fill'
85
+ ? product.units < 1
86
+ : step.role === 'cover'
87
+ ? product.units < Math.max(1, slots)
88
+ : product.stock < 1;
89
+
90
+ const listPence = $derived(
91
+ bundle.steps
92
+ .filter((s) => s.role === 'pick' || s.role === 'base')
93
+ .reduce(
94
+ (sum, step) =>
95
+ sum +
96
+ step.products
97
+ .filter((p) => chosen(step.id).has(p.id))
98
+ .reduce(
99
+ (s, p) =>
100
+ s + (step.role === 'base' ? (p.kitPricePence ?? p.pricePence) : p.pricePence),
101
+ 0
102
+ ),
103
+ 0
104
+ )
105
+ );
42
106
  const totalPence = $derived(
43
- bundle.steps.reduce(
44
- (sum, step) =>
45
- sum +
46
- step.products
47
- .filter((p) => chosen(step.id).has(p.id))
48
- .reduce((s, p) => s + p.pricePence, 0),
49
- 0
107
+ kit ? listPence : Math.round((listPence * (100 - bundle.discountPercent)) / 100)
108
+ );
109
+
110
+ /** A step that must be chosen but has nothing left: the bundle cannot be made right now. */
111
+ const soldOut = $derived(
112
+ bundle.steps.find(
113
+ (s) =>
114
+ (s.role !== 'pick' || s.minChoices > 0) &&
115
+ s.products.every((p) =>
116
+ s.role === 'fill' || s.role === 'cover' ? p.units < 1 : p.stock < 1
117
+ )
50
118
  )
51
119
  );
52
- const missing = $derived(bundle.steps.find((s) => chosen(s.id).size < s.minChoices));
120
+
121
+ const missing = $derived(
122
+ bundle.steps.find((s) => {
123
+ if (s.role === 'fill') return slots === 0 || used(s.id) !== slots;
124
+ if (s.role === 'base' || s.role === 'cover') return chosen(s.id).size !== 1;
125
+ return chosen(s.id).size < s.minChoices;
126
+ })
127
+ );
128
+
129
+ function rule(s: Step): string {
130
+ if (s.role === 'base') return 'Choose a size';
131
+ if (s.role === 'cover') return slots ? `Choose 1 (${slots} included)` : 'Choose 1';
132
+ if (s.role === 'fill') {
133
+ if (!slots) return 'Choose a size first';
134
+ const each = s.maxPerChoice ? `, up to ${s.maxPerChoice} of each` : '';
135
+ return `${used(s.id)} of ${slots} chosen${each}`;
136
+ }
137
+ return s.minChoices === 0
138
+ ? `Optional, up to ${s.maxChoices}`
139
+ : s.minChoices === s.maxChoices
140
+ ? `Choose ${s.minChoices}`
141
+ : `Choose ${s.minChoices} to ${s.maxChoices}`;
142
+ }
143
+
144
+ const priceLabel = (step: Step, product: Option) =>
145
+ kit && (step.role === 'fill' || step.role === 'cover')
146
+ ? 'Included'
147
+ : step.role === 'base' && product.slots
148
+ ? `${Money.format(product.kitPricePence ?? product.pricePence)} · ${product.slots} slots`
149
+ : Money.format(product.pricePence);
53
150
 
54
151
  async function add() {
55
152
  busy = true;
56
153
  try {
57
154
  await addBundleToBasket({
58
155
  bundleId: bundle.id,
59
- choices: bundle.steps.map((s) => ({ productIds: [...chosen(s.id)], stepId: s.id })),
156
+ choices: bundle.steps.map((s) => ({
157
+ productIds:
158
+ s.role === 'fill'
159
+ ? [...counts(s.id)].flatMap(([id, n]) => Array.from({ length: n }, () => id))
160
+ : [...chosen(s.id)],
161
+ stepId: s.id,
162
+ })),
60
163
  });
61
164
  toast.success('Bundle added to your basket');
62
165
  await goto(resolve('/basket'));
@@ -66,61 +169,117 @@
66
169
  busy = false;
67
170
  }
68
171
  }
69
-
70
- const rule = (s: { maxChoices: number; minChoices: number }) =>
71
- s.minChoices === 0
72
- ? `Optional, up to ${s.maxChoices}`
73
- : s.minChoices === s.maxChoices
74
- ? `Choose ${s.minChoices}`
75
- : `Choose ${s.minChoices} to ${s.maxChoices}`;
76
172
  </script>
77
173
 
78
174
  <PageTitle description={bundle.description.slice(0, 160)} title={bundle.name} />
79
175
 
80
176
  {#if bundle.description}
81
- <p class="-mt-4 mb-8 max-w-prose whitespace-pre-line">{bundle.description}</p>
177
+ <Text class="-mt-4 mb-4 max-w-prose whitespace-pre-line" color="secondary"
178
+ >{bundle.description}</Text
179
+ >
180
+ {/if}
181
+ {#if !kit && bundle.discountPercent > 0}
182
+ <Text
183
+ class="mb-8 inline-flex rounded-full bg-[var(--primary-soft)] px-3 py-1 text-[var(--primary-strong)]"
184
+ bold
185
+ >
186
+ {bundle.discountPercent}% off when bought as a bundle
187
+ </Text>
188
+ {/if}
189
+
190
+ {#if soldOut}
191
+ <div class="mb-6 rounded-xl border border-[var(--bd)] bg-[var(--sfh)] px-4 py-3" role="status">
192
+ <Text>Not available right now: every {soldOut.title.toLowerCase()} option is sold out.</Text>
193
+ </div>
82
194
  {/if}
83
195
 
84
- <ol class="space-y-10">
196
+ <ol class="mt-4 space-y-10">
85
197
  {#each bundle.steps as step, i (step.id)}
86
- <li>
87
- <h2 class="text-lg font-semibold">{i + 1}. {step.title}</h2>
88
- <p class="text-sm text-[var(--txs)]">
198
+ {const locked = $derived((step.role === 'fill' || step.role === 'cover') && slots === 0)}
199
+ <li class={locked ? 'opacity-50' : ''}>
200
+ <Title level={2} size="sm">{i + 1}. {step.title}</Title>
201
+ <Text color="secondary">
89
202
  {rule(step)}{step.description ? ` · ${step.description}` : ''}
90
- </p>
203
+ </Text>
91
204
  <ul class="mt-3 grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
92
205
  {#each step.products as product (product.id)}
93
- {const on = chosen(step.id).has(product.id)}
94
- {const out = product.stock < 1}
206
+ {const sold = $derived(out(step, product))}
207
+ {const label = $derived(
208
+ product.variationLabel ? `${product.name} · ${product.variationLabel}` : product.name
209
+ )}
95
210
  <li>
96
- <button
97
- class="flex w-full items-center gap-3 rounded-xl border p-2 text-left transition {on
98
- ? 'border-[var(--primary-strong)] bg-[var(--primary-soft)]'
99
- : 'border-[var(--bd)] bg-white'} disabled:opacity-50"
100
- aria-pressed={on}
101
- disabled={out}
102
- onclick={() => toggle(step, product.id)}
103
- type="button"
104
- >
105
- {#if product.cover}
106
- <img
107
- class="size-16 shrink-0 rounded-md object-cover"
108
- alt={product.cover.alt || product.name}
109
- loading="lazy"
110
- src={Images.url(context.imageBaseUrl, product.cover.src)}
111
- />
112
- {/if}
113
- <span class="min-w-0">
114
- <span class="block font-medium"
115
- >{product.name}{product.variationLabel
116
- ? ` · ${product.variationLabel}`
117
- : ''}</span
118
- >
119
- <span class="block text-sm text-[var(--txs)]">
120
- {out ? 'Out of stock' : Money.format(product.pricePence)}
121
- </span>
122
- </span>
123
- </button>
211
+ {#if step.role === 'fill'}
212
+ {const n = $derived(counts(step.id).get(product.id) ?? 0)}
213
+ <div
214
+ class="flex w-full items-center gap-3 rounded-xl border p-2 transition {n > 0
215
+ ? 'border-[var(--primary-strong)] bg-[var(--primary-soft)]'
216
+ : 'border-[var(--bd)] bg-white'} {sold ? 'opacity-50' : ''}"
217
+ >
218
+ {#if product.cover}
219
+ <img
220
+ class="size-14 shrink-0 rounded-md object-cover"
221
+ alt={product.cover.alt || product.name}
222
+ loading="lazy"
223
+ src={Images.url(context.imageBaseUrl, product.cover.src)}
224
+ />
225
+ {/if}
226
+ <div class="min-w-0 flex-1">
227
+ <Span class="block" bold>{label}</Span>
228
+ <Span class="block" color="secondary" size="sm">
229
+ {sold ? 'Out of stock' : priceLabel(step, product)}
230
+ </Span>
231
+ </div>
232
+ <div class="flex shrink-0 items-center gap-1">
233
+ <Button
234
+ aria-label="One fewer {product.name}"
235
+ disabled={n === 0}
236
+ icon
237
+ onclick={() => adjust(step, product, -1)}
238
+ size="sm"
239
+ variant="secondary"
240
+ >
241
+ <Minus size={14} />
242
+ </Button>
243
+ <div class="w-6 text-center" aria-live="polite"><Span bold>{n}</Span></div>
244
+ <Button
245
+ aria-label="One more {product.name}"
246
+ disabled={sold || locked || !canAdd(step, product)}
247
+ icon
248
+ onclick={() => adjust(step, product, 1)}
249
+ size="sm"
250
+ variant="secondary"
251
+ >
252
+ <Plus size={14} />
253
+ </Button>
254
+ </div>
255
+ </div>
256
+ {:else}
257
+ {const on = $derived(chosen(step.id).has(product.id))}
258
+ <button
259
+ class="flex w-full items-center gap-3 rounded-xl border p-2 text-left transition {on
260
+ ? 'border-[var(--primary-strong)] bg-[var(--primary-soft)]'
261
+ : 'border-[var(--bd)] bg-white hover:border-[var(--primary-strong)]'} disabled:opacity-50 disabled:hover:border-[var(--bd)]"
262
+ aria-pressed={on}
263
+ disabled={sold || locked}
264
+ onclick={() => toggle(step, product.id)}
265
+ type="button"
266
+ >
267
+ {#if product.cover}
268
+ <img
269
+ class="size-14 shrink-0 rounded-md object-cover"
270
+ alt={product.cover.alt || product.name}
271
+ loading="lazy"
272
+ src={Images.url(context.imageBaseUrl, product.cover.src)}
273
+ />
274
+ {/if}
275
+ <Span class="min-w-0">
276
+ <Span class="block" bold>{label}</Span>
277
+ <Span class="block" color="secondary" size="sm">
278
+ {sold ? 'Out of stock' : priceLabel(step, product)}
279
+ </Span>
280
+ </Span>
281
+ </button>
282
+ {/if}
124
283
  </li>
125
284
  {/each}
126
285
  </ul>
@@ -131,14 +290,20 @@
131
290
  <div
132
291
  class="sticky bottom-0 mt-10 flex flex-wrap items-center justify-between gap-3 border-t border-[var(--bd)] bg-white py-4"
133
292
  >
134
- <p>
135
- <span class="text-[var(--txs)]">Total</span>
136
- <strong class="ml-2 text-xl">{Money.format(totalPence)}</strong>
137
- </p>
293
+ <Text class="flex items-baseline gap-2">
294
+ <Span color="secondary">Total</Span>
295
+ <strong class="text-xl">{Money.format(totalPence)}</strong>
296
+ {#if totalPence < listPence}
297
+ <s class="text-sm text-[var(--txt)]">{Money.format(listPence)}</s>
298
+ {/if}
299
+ </Text>
138
300
  <div class="flex items-center gap-3">
139
- {#if missing}<span class="text-sm text-[var(--txs)]">{missing.title}: {rule(missing)}</span
140
- >{/if}
141
- <Button disabled={busy || missing !== undefined || totalPence === 0} onclick={add} size="lg">
301
+ {#if missing}<Span color="secondary">{missing.title}: {rule(missing)}</Span>{/if}
302
+ <Button
303
+ disabled={busy || soldOut !== undefined || missing !== undefined || totalPence === 0}
304
+ onclick={add}
305
+ size="lg"
306
+ >
142
307
  Add bundle to basket
143
308
  </Button>
144
309
  </div>
package/bundles/schema.ts CHANGED
@@ -2,6 +2,7 @@ import { SchemaForm } from '@zeyolabs/kit/utils/form';
2
2
  import { Schema } from '@zeyolabs/kit/utils/schema';
3
3
  import { z } from 'zod';
4
4
 
5
+ import { BUNDLE_STEP_ROLES } from '../db/schema';
5
6
  import { FormInput } from '../utils/form-input';
6
7
  import { ShopSchema } from '../utils/schemas';
7
8
 
@@ -11,7 +12,8 @@ export const BundleChoiceSchema = z.object({
11
12
  choices: z
12
13
  .array(
13
14
  z.object({
14
- productIds: z.array(Schema.Id).max(30),
15
+ /** A `fill` step repeats an id once per slot it takes. */
16
+ productIds: z.array(Schema.Id).max(64),
15
17
  stepId: Schema.Id,
16
18
  })
17
19
  )
@@ -22,8 +24,11 @@ export const BundleStepInputSchema = z
22
24
  .object({
23
25
  description: z.string().trim().max(500),
24
26
  maxChoices: z.number().int().min(1).max(30),
27
+ /** A `fill` step: most slots one product may take; null for no limit. */
28
+ maxPerChoice: z.number().int().min(1).max(64).nullable(),
25
29
  minChoices: z.number().int().min(0).max(30),
26
- /** The products offered, by SKU, in display order. */
30
+ role: z.enum(BUNDLE_STEP_ROLES),
31
+ /** The products offered, by SKU, in display order; a `base` step's as `SKU=slots`. */
27
32
  skus: z.array(z.string().trim().min(1).max(60)).min(1, 'add at least one product').max(60),
28
33
  title: z.string().trim().min(1, 'name the step').max(120),
29
34
  })
@@ -33,6 +38,8 @@ export const BundleStepInputSchema = z
33
38
  export const BundleInputSchema = z.object({
34
39
  active: z.boolean(),
35
40
  description: z.string().trim().max(5000),
41
+ /** Off the products' total when bought as this bundle; kits ignore it. */
42
+ discountPercent: z.number().int().min(0).max(90),
36
43
  id: Schema.Id.optional(),
37
44
  image: z.string().trim().max(200).nullable(),
38
45
  name: z.string().trim().min(1, 'enter a name').max(200),
@@ -50,6 +57,7 @@ export const BundleFormSchema = z
50
57
  .object({
51
58
  active: SchemaForm.Boolean,
52
59
  description: ShopSchema.FormText,
60
+ discountPercent: SchemaForm.Int.default(0),
53
61
  id: Schema.Id.optional(),
54
62
  image: ShopSchema.FormText,
55
63
  name: ShopSchema.FormText,
@@ -60,7 +68,9 @@ export const BundleFormSchema = z
60
68
  z.object({
61
69
  description: ShopSchema.FormText,
62
70
  maxChoices: SchemaForm.Int,
71
+ maxPerChoice: ShopSchema.FormText,
63
72
  minChoices: SchemaForm.Int,
73
+ role: z.enum(BUNDLE_STEP_ROLES).default('pick'),
64
74
  skus: ShopSchema.FormText,
65
75
  title: ShopSchema.FormText,
66
76
  })
@@ -73,6 +83,7 @@ export const BundleFormSchema = z
73
83
  slug: b.slug.trim() || FormInput.slugify(b.name),
74
84
  steps: b.steps.map((step) => ({
75
85
  ...step,
86
+ maxPerChoice: Number(step.maxPerChoice) > 0 ? Number(step.maxPerChoice) : null,
76
87
  skus: step.skus
77
88
  .split(/[\s,]+/)
78
89
  .map((sku) => sku.trim())