@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.
- package/basket/client.remote.ts +16 -2
- package/basket/cookies.server.ts +13 -1
- package/basket/library.ts +58 -0
- package/basket/pages/Basket.svelte +80 -30
- package/basket/service.server.ts +139 -6
- package/bundles/admin/BundleEditPage.svelte +7 -4
- package/bundles/admin/BundleForm.svelte +50 -11
- package/bundles/admin/BundlesPage.svelte +14 -10
- package/bundles/client.remote.ts +18 -6
- package/bundles/library.ts +51 -0
- package/bundles/pages/Bundle.svelte +233 -68
- package/bundles/schema.ts +13 -2
- package/bundles/service.server.ts +185 -33
- package/catalogue/admin/CategoriesPage.svelte +13 -10
- package/catalogue/admin/CategoryForm.svelte +4 -3
- package/catalogue/admin/ProductEditPage.svelte +7 -4
- package/catalogue/admin/ProductForm.svelte +31 -29
- package/catalogue/admin/ProductsPage.svelte +16 -14
- package/catalogue/client.remote.ts +23 -9
- package/catalogue/components/ProductCard.svelte +22 -14
- package/catalogue/library.ts +91 -0
- package/catalogue/loads.server.ts +8 -4
- package/catalogue/pages/Category.svelte +15 -6
- package/catalogue/pages/Product.svelte +164 -45
- package/catalogue/pages/Search.svelte +12 -7
- package/catalogue/pages/Shop.svelte +12 -12
- package/catalogue/service.server.ts +44 -9
- package/checkout/manager.server.ts +68 -16
- package/checkout/pages/CheckoutSuccess.svelte +10 -8
- package/customers/pages/Account.svelte +19 -15
- package/db/schema.ts +27 -3
- package/fulfilment/admin/OrderPage.svelte +60 -56
- package/fulfilment/admin/OrdersPage.svelte +26 -22
- package/orders/manager.server.ts +45 -11
- package/orders/pages/Order.svelte +35 -29
- package/orders/service.server.ts +25 -3
- package/package.json +5 -4
- package/reviews/admin/ReviewsPage.svelte +23 -19
- package/reviews/components/ReviewForm.svelte +3 -2
- package/reviews/components/Reviews.svelte +17 -13
- package/stock/admin/StockPage.svelte +12 -9
- package/stock/admin/StockRow.svelte +3 -2
- package/stock/service.server.ts +6 -1
- package/ui/PageLinks.svelte +45 -0
- package/ui/PageTitle.svelte +20 -4
- 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
|
-
<
|
|
14
|
+
<Text><a href={resolve('/products')}>← Products</a></Text>
|
|
11
15
|
<div class="mt-2 flex items-center justify-between">
|
|
12
|
-
<
|
|
16
|
+
<Title level={1} size="md">Bundles</Title>
|
|
13
17
|
<a
|
|
14
|
-
class="rounded-md bg-[var(--
|
|
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
|
-
<
|
|
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
|
-
</
|
|
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(--
|
|
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
|
-
<
|
|
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(--
|
|
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
|
-
<
|
|
42
|
+
<Text class="mt-6" color="secondary">Loading…</Text>
|
|
39
43
|
{/snippet}
|
|
40
44
|
</Boundary>
|
package/bundles/client.remote.ts
CHANGED
|
@@ -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
|
-
/**
|
|
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
|
|
26
|
+
const resolved = await service.resolve(bundleId, choices);
|
|
24
27
|
const { cookies } = getRequestEvent();
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
|
6
|
-
import
|
|
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 {
|
|
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
|
-
/**
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
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) => ({
|
|
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
|
-
<
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
<
|
|
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
|
-
</
|
|
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
|
|
94
|
-
{const
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
>
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
-
<
|
|
135
|
-
<
|
|
136
|
-
<strong class="
|
|
137
|
-
|
|
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}<
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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())
|