@vectoriox/iox-ui 5.94.0 → 5.96.0
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/actions-core/index.d.ts +7 -2
- package/commerce-core/index.d.ts +312 -2
- package/composite-core/index.d.ts +306 -2
- package/design-core/index.d.ts +1049 -105
- package/fesm2022/vectoriox-iox-ui-actions-core.mjs +15 -4
- package/fesm2022/vectoriox-iox-ui-actions-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-commerce-core.mjs +527 -6
- package/fesm2022/vectoriox-iox-ui-commerce-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-composite-core.mjs +1193 -177
- package/fesm2022/vectoriox-iox-ui-composite-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-design-core.mjs +1147 -43
- package/fesm2022/vectoriox-iox-ui-design-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs +2 -2
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui.mjs +8 -1
- package/fesm2022/vectoriox-iox-ui.mjs.map +1 -1
- package/generation-core/index.d.ts +2 -2
- package/index.d.ts +2 -0
- package/package.json +1 -1
- package/src/actions-core/action.model.ts +1 -1
- package/src/actions-core/element-actions.ts +17 -3
- package/src/commerce-core/cart-transport.ts +211 -0
- package/src/commerce-core/public-api.ts +4 -0
- package/src/commerce-core/quantity.ts +56 -0
- package/src/commerce-core/shop-display.ts +117 -0
- package/src/commerce-core/variants.ts +296 -0
- package/src/composite-core/exclusive-runtime.ts +20 -7
- package/src/composite-core/public-api.ts +2 -0
- package/src/composite-core/shop-runtime.ts +626 -0
- package/src/composite-core/shop.ts +458 -0
- package/src/design-core/flavor-spec.generated.ts +1147 -43
- package/src/generation-core/site-spec.generated.ts +2 -2
- package/styles/iox-design.css +95 -0
|
@@ -4961,7 +4961,7 @@ declare const SPEC_SITE_LAYOUT: {
|
|
|
4961
4961
|
readonly signatures: readonly [{
|
|
4962
4962
|
readonly id: "words-lit";
|
|
4963
4963
|
readonly label: "words lit one by one";
|
|
4964
|
-
readonly how: "a kinetic word or
|
|
4964
|
+
readonly how: "a kinetic word, heading or line of at most seven words in a held section";
|
|
4965
4965
|
}, {
|
|
4966
4966
|
readonly id: "photo-unveiled";
|
|
4967
4967
|
readonly label: "a photo unveiled";
|
|
@@ -4993,7 +4993,7 @@ declare const SPEC_SITE_LAYOUT: {
|
|
|
4993
4993
|
}, {
|
|
4994
4994
|
readonly id: "held-screen";
|
|
4995
4995
|
readonly label: "a held screen";
|
|
4996
|
-
readonly how: "a held section
|
|
4996
|
+
readonly how: "a held section whose layers only fade or push by the scroll — something still moves (a fallback, never the aim)";
|
|
4997
4997
|
}];
|
|
4998
4998
|
readonly daringLevels: readonly ["lively", "expressive"];
|
|
4999
4999
|
readonly looks: readonly [{
|
package/index.d.ts
CHANGED
|
@@ -3112,6 +3112,8 @@ interface IoxPipe {
|
|
|
3112
3112
|
* builder canvas and the SSR engine (both pass the site's language). */
|
|
3113
3113
|
interface PipeContext {
|
|
3114
3114
|
locale?: string;
|
|
3115
|
+
/** The studio's currency (ISO 4217) — what the `money` pipe formats a bare number in when the binding names none. */
|
|
3116
|
+
currency?: string;
|
|
3115
3117
|
}
|
|
3116
3118
|
type PipeFn = (value: any, args: any[], ctx: PipeContext) => any;
|
|
3117
3119
|
/** The registry — adding a pipe here (+ a descriptor below) makes it work on both sides and appear in
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@ export type ActionKind = 'animated' | 'instant' | 'server';
|
|
|
11
11
|
|
|
12
12
|
/** Picker grouping. Flat lists die at ~10 entries; the group is data, never a second list. */
|
|
13
13
|
export type ActionGroup =
|
|
14
|
-
| 'motion' | 'visibility' | 'navigate' | 'dialog' | 'popover' | 'notice' | 'item' | 'form' | 'data' | 'consent' | 'service';
|
|
14
|
+
| 'motion' | 'visibility' | 'navigate' | 'dialog' | 'popover' | 'notice' | 'item' | 'form' | 'data' | 'consent' | 'shop' | 'service';
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* What a step does inside the live builder canvas (the canvas RUNS interactions):
|
|
@@ -29,6 +29,9 @@ const SCROLL_ONLY: ReadonlyArray<string> = Object.freeze([SCROLL_PROGRESS_TRIGGE
|
|
|
29
29
|
* fires on its own — when it comes into view, or with the page in the first screen behind an entrance. Under a click or a
|
|
30
30
|
* hover it would stand at its start value until pressed; it is never armed there, and never offered.
|
|
31
31
|
*/
|
|
32
|
+
/** The plan feature selling sits behind (C-D18: the control plane's `features.shop`). */
|
|
33
|
+
export const SHOP_ENTITLEMENT = 'shop';
|
|
34
|
+
|
|
32
35
|
export const COUNT_UP_TRIGGERS: ReadonlyArray<string> = Object.freeze(['viewportEnter', 'pageLoad']);
|
|
33
36
|
|
|
34
37
|
export const ELEMENT_ACTION_DESCRIPTORS: readonly ActionDescriptor[] = Object.freeze([
|
|
@@ -174,13 +177,24 @@ export const ELEMENT_ACTION_DESCRIPTORS: readonly ActionDescriptor[] = Object.fr
|
|
|
174
177
|
{ name: 'values', type: 'string', label: 'Field values (JSON)' },
|
|
175
178
|
] },
|
|
176
179
|
|
|
180
|
+
// ── Shop (commerce E1c, C-D16): the shop parts' behaviour, run by composite-core's shop runtime through the host's
|
|
181
|
+
// cart transport (forms' transport rule — the part owns no HTTP). Add to cart SIMULATES on the canvas: the drawer opens
|
|
182
|
+
// with a sample line and no cart is written (T8). Buy now is INERT there — it must never take anyone to a payment.
|
|
183
|
+
// Gated by the ONE plan feature `features.shop` (C-D18); a manual payment method needs no payment connector (E1).
|
|
184
|
+
{ type: 'addToCart', labelKey: 'builder.actions.addToCart', kind: 'server', group: 'shop', builderBehaviour: 'simulate',
|
|
185
|
+
entitlement: SHOP_ENTITLEMENT },
|
|
186
|
+
{ type: 'startCheckout', labelKey: 'builder.actions.startCheckout', kind: 'server', group: 'shop', builderBehaviour: 'inert',
|
|
187
|
+
entitlement: SHOP_ENTITLEMENT },
|
|
188
|
+
|
|
177
189
|
// ── Services (D-A5: visible, greyed, with the reason — never hidden, never live) ──
|
|
178
190
|
{ type: 'signIn', labelKey: 'builder.actions.signIn', kind: 'server', group: 'service', builderBehaviour: 'inert',
|
|
179
191
|
planned: true, entitlement: 'visitorIdentity' },
|
|
180
|
-
{ type: 'startCheckout', labelKey: 'builder.actions.startCheckout', kind: 'server', group: 'service', builderBehaviour: 'inert',
|
|
181
|
-
planned: true, connector: 'payment', entitlement: 'commerce' },
|
|
182
192
|
]);
|
|
183
193
|
|
|
194
|
+
/** The shop actions' dispatch set (commerce E1c) — the engines branch on it beside DIALOG / BEHAVIOUR and hand the action
|
|
195
|
+
* to composite-core `runShopAction`, which reads the product, the variant and the quantity around the element. */
|
|
196
|
+
export const SHOP_ACTION_TYPES: ReadonlySet<string> = new Set(['addToCart', 'startCheckout']);
|
|
197
|
+
|
|
184
198
|
/** The A2 server executors' dispatch set — the live engine POSTs these to the public actions
|
|
185
199
|
* route; the canvas simulates them (a server action never runs from the ADMIN's browser). */
|
|
186
200
|
export const SERVER_ELEMENT_ACTION_TYPES: ReadonlySet<string> =
|
|
@@ -219,7 +233,7 @@ export const CONSENT_ACTION_TYPES: ReadonlySet<string> =
|
|
|
219
233
|
|
|
220
234
|
/** Picker order — most-used groups first, services last (they are the shop window, not the shop). */
|
|
221
235
|
export const ELEMENT_ACTION_GROUP_ORDER: readonly string[] =
|
|
222
|
-
Object.freeze(['motion', 'visibility', 'navigate', 'dialog', 'popover', 'notice', 'item', 'form', 'data', 'consent', 'service']);
|
|
236
|
+
Object.freeze(['motion', 'visibility', 'navigate', 'dialog', 'popover', 'notice', 'item', 'form', 'data', 'consent', 'shop', 'service']);
|
|
223
237
|
|
|
224
238
|
/** True when `d` is offered under `trigger` (its `triggers`, else every trigger) — the picker's rule and the runtimes'. */
|
|
225
239
|
export function actionOfferedUnder(d: Pick<ActionDescriptor, 'triggers'>, trigger: string | null | undefined): boolean {
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { CatalogEntry, catalogKey } from './catalog';
|
|
2
|
+
import { Cart, CART_ISSUE_LABELS, cartOf, CartIssue, PricedCart, PricedLine, priceCart, StudioCommerce } from './cart';
|
|
3
|
+
import { CurrencyCode } from './currency';
|
|
4
|
+
import { CommerceLabel, labelIn } from './labels';
|
|
5
|
+
import { MAX_LINE_QUANTITY } from './limits';
|
|
6
|
+
import { Money } from './money';
|
|
7
|
+
import { quantityBounds } from './quantity';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The CART TRANSPORT CONTRACT (commerce plan §3d, C-D16) — what a host provides so the builder's shop parts can show and
|
|
11
|
+
* change a visitor's cart. The parts own no HTTP, exactly like a form (forms-core `transport.ts`, the §6b lesson: a
|
|
12
|
+
* part that emits and a host that never answers ships a dead button behind green tests):
|
|
13
|
+
*
|
|
14
|
+
* - the LIVE SITE (the engine, commerce E1d) implements it over iox-cms's hardened cart routes (`cartEndpointPath`): the
|
|
15
|
+
* server prices every read with `priceCart` and the transport hands back its `CartView` — a browser never computes a
|
|
16
|
+
* price it charges (trap T1);
|
|
17
|
+
* - the BUILDER CANVAS uses `simulatedCartTransport`: an in-memory cart over sample data, never written anywhere
|
|
18
|
+
* (trap T8: the canvas never writes a cart).
|
|
19
|
+
*
|
|
20
|
+
* Every method resolves with the WHOLE cart as it is now, or rejects with a `CartTransportError`.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** One line of the cart a visitor sees: the server's priced line + the id its routes know it by + what it may hold. */
|
|
24
|
+
export interface CartViewLine extends PricedLine {
|
|
25
|
+
lineId: string;
|
|
26
|
+
/** The most this line may hold (the stepper's top). */
|
|
27
|
+
maxQuantity: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface CartView {
|
|
31
|
+
/** null only for a cart that has never been priced (the no-transport stub). */
|
|
32
|
+
currency: CurrencyCode | null;
|
|
33
|
+
lines: CartViewLine[];
|
|
34
|
+
/** How many units — the cart button's badge (`$cart.count`). */
|
|
35
|
+
count: number;
|
|
36
|
+
empty: boolean;
|
|
37
|
+
subtotal: Money | null;
|
|
38
|
+
total: Money | null;
|
|
39
|
+
vat: Money | null;
|
|
40
|
+
vatPercent: number;
|
|
41
|
+
issues: CartIssue[];
|
|
42
|
+
/** What is still missing for a free-delivery method, when the studio offers one (the drawer's note); else null. */
|
|
43
|
+
freeShippingRemaining: Money | null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface AddToCartRequest {
|
|
47
|
+
productId: string;
|
|
48
|
+
variantId?: string | null;
|
|
49
|
+
quantity: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface CartTransport {
|
|
53
|
+
read(): Promise<CartView>;
|
|
54
|
+
add(request: AddToCartRequest): Promise<CartView>;
|
|
55
|
+
setQuantity(lineId: string, quantity: number): Promise<CartView>;
|
|
56
|
+
remove(lineId: string): Promise<CartView>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export enum CartTransportErrorCode {
|
|
60
|
+
/** No cart on this host (the engine before E1d, a site with no shop). */
|
|
61
|
+
Unavailable = 'unavailable',
|
|
62
|
+
/** The server said no (a product no longer sold, a rate limit). */
|
|
63
|
+
Refused = 'refused',
|
|
64
|
+
/** The call never reached the server. */
|
|
65
|
+
Network = 'network',
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const CART_TRANSPORT_ERROR_LABELS: Readonly<Record<CartTransportErrorCode, CommerceLabel>> = Object.freeze({
|
|
69
|
+
[CartTransportErrorCode.Unavailable]: { en: 'The shop is not open yet.', he: 'החנות עוד לא פתוחה.' },
|
|
70
|
+
[CartTransportErrorCode.Refused]: { en: 'That could not be added to your bag.', he: 'לא ניתן היה להוסיף לסל.' },
|
|
71
|
+
[CartTransportErrorCode.Network]: { en: 'We could not reach the shop. Try again.', he: 'לא הצלחנו להגיע לחנות. נסו שוב.' },
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export class CartTransportError extends Error {
|
|
75
|
+
constructor(readonly code: CartTransportErrorCode, message?: string) {
|
|
76
|
+
super(message ?? code);
|
|
77
|
+
this.name = 'CartTransportError';
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** A line's id when the host has none of its own: the product and the variant, URL-safe. */
|
|
82
|
+
export function cartLineId(productId: string, variantId?: string | null): string {
|
|
83
|
+
return `${encodeURIComponent(productId)}~${encodeURIComponent(variantId ?? '')}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The public cart routes (§3d) — ROOT-relative, like the forms submit path (a relative path under the org's
|
|
87
|
+
* `<base href>` reaches the engine, never iox-cms). `lineId` addresses one line. */
|
|
88
|
+
export function cartEndpointPath(orgId: string, lineId?: string | null): string {
|
|
89
|
+
const base = `/api/site/${encodeURIComponent(orgId)}/cart`;
|
|
90
|
+
return lineId ? `${base}/lines/${encodeURIComponent(lineId)}` : base;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The fixed checkout route (C-D3), relative to the site's `<base href="/<orgLabel>/">`. */
|
|
94
|
+
export const CHECKOUT_PATH = 'checkout';
|
|
95
|
+
|
|
96
|
+
/** The checkout's href: `/<orgLabel>/checkout` under a label, else `/checkout`. */
|
|
97
|
+
export function checkoutHref(orgLabel?: string | null): string {
|
|
98
|
+
const label = String(orgLabel ?? '').replace(/^\/+|\/+$/g, '');
|
|
99
|
+
return label ? `/${encodeURIComponent(label)}/${CHECKOUT_PATH}` : `/${CHECKOUT_PATH}`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A cart with nothing in it. */
|
|
103
|
+
export function emptyCartView(currency: CurrencyCode | null = null): CartView {
|
|
104
|
+
return { currency, lines: [], count: 0, empty: true, subtotal: null, total: null, vat: null, vatPercent: 0, issues: [], freeShippingRemaining: null };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The view of a priced cart. `lineIdOf` names each line the way the host's routes do (default `cartLineId`);
|
|
108
|
+
* `maxOf` says how many a line may hold (default the line cap). */
|
|
109
|
+
export function cartViewOf(priced: PricedCart, options: {
|
|
110
|
+
lineIdOf?: (line: PricedLine) => string;
|
|
111
|
+
maxOf?: (line: PricedLine) => number;
|
|
112
|
+
freeShippingRemaining?: Money | null;
|
|
113
|
+
} = {}): CartView {
|
|
114
|
+
const lineIdOf = options.lineIdOf ?? (line => cartLineId(line.productId, line.variantId));
|
|
115
|
+
const maxOf = options.maxOf ?? (() => MAX_LINE_QUANTITY);
|
|
116
|
+
const lines = priced.lines.map(line => ({ ...line, lineId: lineIdOf(line), maxQuantity: Math.max(line.quantity, maxOf(line)) }));
|
|
117
|
+
const count = lines.reduce((sum, line) => sum + line.quantity, 0);
|
|
118
|
+
return {
|
|
119
|
+
currency: priced.currency,
|
|
120
|
+
lines,
|
|
121
|
+
count,
|
|
122
|
+
empty: lines.length === 0,
|
|
123
|
+
subtotal: priced.subtotal,
|
|
124
|
+
total: priced.total,
|
|
125
|
+
vat: priced.vat,
|
|
126
|
+
vatPercent: priced.vatPercent,
|
|
127
|
+
issues: priced.issues,
|
|
128
|
+
freeShippingRemaining: options.freeShippingRemaining ?? null,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The cart iox-cms's public routes answer (E1b, `shop.md` "The cart"): the priced cart, each line under its own id
|
|
133
|
+
* (`ln_…`), and the unit count. */
|
|
134
|
+
export interface SiteCartView extends PricedCart {
|
|
135
|
+
lines: Array<PricedLine & { id: string }>;
|
|
136
|
+
itemCount?: number;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** iox-cms's answer as the view the parts paint — the server's line ids, its prices untouched. */
|
|
140
|
+
export function siteCartView(site: SiteCartView, freeShippingRemaining: Money | null = null): CartView {
|
|
141
|
+
const ids = new Map(site.lines.map(line => [line, line.id]));
|
|
142
|
+
return cartViewOf(site, { lineIdOf: line => ids.get(line as PricedLine & { id: string }) ?? cartLineId(line.productId, line.variantId), freeShippingRemaining });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** How a refused cart call is said: no cart service (501, 503) or a shop off the plan (403) is Unavailable, a
|
|
146
|
+
* status the server chose is Refused, anything that never got an answer is Network. */
|
|
147
|
+
export function cartTransportErrorFor(status: number | null | undefined): CartTransportError {
|
|
148
|
+
if (!status) return new CartTransportError(CartTransportErrorCode.Network);
|
|
149
|
+
if (status === 501 || status === 503 || status === 403) return new CartTransportError(CartTransportErrorCode.Unavailable, String(status));
|
|
150
|
+
return new CartTransportError(CartTransportErrorCode.Refused, String(status));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The words a cart's issues are said with, once each. */
|
|
154
|
+
export function cartIssueWords(view: Pick<CartView, 'issues'>, lang: string | null | undefined): string[] {
|
|
155
|
+
const codes = [...new Set(view.issues.map(i => i.code))];
|
|
156
|
+
return codes.map(code => labelIn(CART_ISSUE_LABELS[code], lang));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The host with no cart yet (the engine until E1d binds its own): every read is an empty cart, every change refused. */
|
|
160
|
+
export const NO_CART_TRANSPORT: CartTransport = Object.freeze({
|
|
161
|
+
read: () => Promise.resolve(emptyCartView()),
|
|
162
|
+
add: () => Promise.reject(new CartTransportError(CartTransportErrorCode.Unavailable)),
|
|
163
|
+
setQuantity: () => Promise.reject(new CartTransportError(CartTransportErrorCode.Unavailable)),
|
|
164
|
+
remove: () => Promise.reject(new CartTransportError(CartTransportErrorCode.Unavailable)),
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The builder canvas's cart: in memory, priced by the ONE `priceCart` over the catalog it is given (sample data, or the
|
|
169
|
+
* product on the canvas), written nowhere. `initial` is what it starts holding (a sample line, so the drawer has one to
|
|
170
|
+
* design). Every product the canvas adds must be in `catalog` — `addEntries` widens it.
|
|
171
|
+
*/
|
|
172
|
+
export interface SimulatedCartTransport extends CartTransport {
|
|
173
|
+
/** Make more units known to the simulation (the product an Add to cart on the canvas names). */
|
|
174
|
+
addEntries(entries: ReadonlyArray<CatalogEntry>): void;
|
|
175
|
+
/** The cart as it stands, priced — no promise (the canvas paints it at once). */
|
|
176
|
+
peek(): CartView;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function simulatedCartTransport(catalog: ReadonlyArray<CatalogEntry>, studio: StudioCommerce, initial: Cart = { lines: [] }): SimulatedCartTransport {
|
|
180
|
+
const entries = new Map<string, CatalogEntry>();
|
|
181
|
+
const known = (list: ReadonlyArray<CatalogEntry>) => list.forEach(e => entries.set(catalogKey(e.productId, e.variantId), e));
|
|
182
|
+
known(catalog);
|
|
183
|
+
let cart: Cart = { lines: [] };
|
|
184
|
+
const maxOf = (line: PricedLine) => quantityBounds(entries.get(catalogKey(line.productId, line.variantId))).max;
|
|
185
|
+
const price = (next: Cart): CartView => {
|
|
186
|
+
const priced = priceCart(next, [...entries.values()], studio);
|
|
187
|
+
cart = cartOf(priced);
|
|
188
|
+
return cartViewOf(priced, { maxOf });
|
|
189
|
+
};
|
|
190
|
+
price(initial);
|
|
191
|
+
const lineOf = (lineId: string) => cart.lines.find(l => cartLineId(l.productId, l.variantId) === lineId);
|
|
192
|
+
return {
|
|
193
|
+
addEntries: list => known(list),
|
|
194
|
+
peek: () => price(cart),
|
|
195
|
+
read: () => Promise.resolve(price(cart)),
|
|
196
|
+
add: request => {
|
|
197
|
+
if (!entries.has(catalogKey(request.productId, request.variantId))) return Promise.reject(new CartTransportError(CartTransportErrorCode.Refused));
|
|
198
|
+
const same = cart.lines.find(l => l.productId === request.productId && (l.variantId ?? null) === (request.variantId ?? null));
|
|
199
|
+
const lines = same
|
|
200
|
+
? cart.lines.map(l => (l === same ? { ...l, quantity: l.quantity + request.quantity } : l))
|
|
201
|
+
: [...cart.lines, { productId: request.productId, variantId: request.variantId ?? null, quantity: request.quantity }];
|
|
202
|
+
return Promise.resolve(price({ lines }));
|
|
203
|
+
},
|
|
204
|
+
setQuantity: (lineId, quantity) => {
|
|
205
|
+
const line = lineOf(lineId);
|
|
206
|
+
if (!line) return Promise.reject(new CartTransportError(CartTransportErrorCode.Refused));
|
|
207
|
+
return Promise.resolve(price({ lines: cart.lines.map(l => (l === line ? { ...l, quantity } : l)) }));
|
|
208
|
+
},
|
|
209
|
+
remove: lineId => Promise.resolve(price({ lines: cart.lines.filter(l => l !== lineOf(lineId)) })),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Availability } from './catalog';
|
|
2
|
+
import { MAX_LINE_QUANTITY } from './limits';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The quantity a buyer may ask for — the stepper on a product page and on every cart line (commerce plan §3i). Whole
|
|
6
|
+
* numbers only, the forms-core number rule: anything that is not a whole number reads as the least. The most is the
|
|
7
|
+
* one-line cap (`MAX_LINE_QUANTITY`), lowered to the count when the count is known (iox-cms) — the storefront knows only
|
|
8
|
+
* a word, so it caps at the line cap and the server clamps (`priceCart` QuantityClamped).
|
|
9
|
+
*/
|
|
10
|
+
export const MIN_LINE_QUANTITY = 1;
|
|
11
|
+
|
|
12
|
+
export interface QuantityBounds {
|
|
13
|
+
min: number;
|
|
14
|
+
/** Below `min` (0) when nothing can be bought: the stepper is off. */
|
|
15
|
+
max: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export enum QuantityStep {
|
|
19
|
+
Down = -1,
|
|
20
|
+
Up = 1,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** What a stepper allows for a unit with this count / word. `out` (or a count of 0) allows nothing. */
|
|
24
|
+
export function quantityBounds(unit?: { stock?: number | null; availability?: string | null } | null): QuantityBounds {
|
|
25
|
+
if (typeof unit?.stock === 'number') {
|
|
26
|
+
const left = Number.isSafeInteger(unit.stock) && unit.stock > 0 ? unit.stock : 0;
|
|
27
|
+
return { min: MIN_LINE_QUANTITY, max: Math.min(MAX_LINE_QUANTITY, left) };
|
|
28
|
+
}
|
|
29
|
+
if (unit?.availability === Availability.Out) return { min: MIN_LINE_QUANTITY, max: 0 };
|
|
30
|
+
return { min: MIN_LINE_QUANTITY, max: MAX_LINE_QUANTITY };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** True when the bounds allow buying at all. */
|
|
34
|
+
export function canBuy(bounds: QuantityBounds): boolean {
|
|
35
|
+
return bounds.max >= bounds.min;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A quantity held within the bounds; not a whole number → the least. */
|
|
39
|
+
export function clampQuantity(value: unknown, bounds: QuantityBounds): number {
|
|
40
|
+
const n = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value) : NaN;
|
|
41
|
+
if (!Number.isSafeInteger(n)) return bounds.min;
|
|
42
|
+
if (!canBuy(bounds)) return bounds.min;
|
|
43
|
+
return Math.min(bounds.max, Math.max(bounds.min, n));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** True when a step in `direction` stays within the bounds. */
|
|
47
|
+
export function canStep(value: number, direction: QuantityStep, bounds: QuantityBounds): boolean {
|
|
48
|
+
if (!canBuy(bounds)) return false;
|
|
49
|
+
const next = clampQuantity(value, bounds) + direction;
|
|
50
|
+
return next >= bounds.min && next <= bounds.max;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One step, held within the bounds. */
|
|
54
|
+
export function stepQuantity(value: unknown, direction: QuantityStep, bounds: QuantityBounds): number {
|
|
55
|
+
return clampQuantity(clampQuantity(value, bounds) + direction, bounds);
|
|
56
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { CurrencyCode, isCurrencyCode } from './currency';
|
|
2
|
+
import { CommerceLabel } from './labels';
|
|
3
|
+
import { formatMoney, isMoney } from './money';
|
|
4
|
+
import { storefrontMoney, StorefrontProduct } from './variants';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* How a price READS on a page — the `money` binding pipe (shop-aware plan A-D2, amended by C-D7) on the builder canvas,
|
|
8
|
+
* the engine's SSR and its hydration: ONE function, so the three never disagree on a digit.
|
|
9
|
+
*
|
|
10
|
+
* - a `Money` (minor units + its currency — `$variant.price`, a cart's totals) is formatted as it is;
|
|
11
|
+
* - a bare number or numeric string (`pricing.price` as the storefront serves it: MAJOR units) needs a currency — the
|
|
12
|
+
* pipe's own argument, else the studio's (`PipeContext.currency`); with none it is left as it was, never guessed;
|
|
13
|
+
* - nothing (null, undefined, '') reads as nothing.
|
|
14
|
+
* The locale is the page's language (`en` when unsaid, so the canvas and the server agree).
|
|
15
|
+
*/
|
|
16
|
+
export const DEFAULT_MONEY_LOCALE = 'en';
|
|
17
|
+
|
|
18
|
+
export function formatMoneyValue(value: unknown, currency?: string | null, locale?: string | null): unknown {
|
|
19
|
+
if (value === null || value === undefined || value === '') return '';
|
|
20
|
+
const lang = locale || DEFAULT_MONEY_LOCALE;
|
|
21
|
+
if (isMoney(value)) {
|
|
22
|
+
try { return formatMoney(value, lang); } catch { return value; }
|
|
23
|
+
}
|
|
24
|
+
const code = String(currency ?? '').trim().toUpperCase();
|
|
25
|
+
if (!isCurrencyCode(code)) return value;
|
|
26
|
+
const amount = storefrontMoney(value, code as CurrencyCode);
|
|
27
|
+
if (!amount) return value;
|
|
28
|
+
try { return formatMoney(amount, lang); } catch { return value; }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The two slots the shop parts write at runtime (the element-context channel — a slot is a `resolvedData` key written
|
|
33
|
+
* at runtime, element-context-state plan): the variant the buyer has chosen (`$variant.price | money`) and the cart
|
|
34
|
+
* (`$cart.count` on the cart button's badge). Written by the shop runtime through its host, read by ordinary bindings.
|
|
35
|
+
*/
|
|
36
|
+
export enum ShopSlot {
|
|
37
|
+
Variant = '$variant',
|
|
38
|
+
Cart = '$cart',
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const SHOP_SLOTS: ReadonlyArray<string> = Object.freeze(Object.values(ShopSlot));
|
|
42
|
+
|
|
43
|
+
// ── the shop parts' words (a string-rendered site has no translation service — the labels.ts rule) ──────────────
|
|
44
|
+
|
|
45
|
+
export enum ShopWord {
|
|
46
|
+
Added = 'added',
|
|
47
|
+
SoldOut = 'soldOut',
|
|
48
|
+
ValueSoldOut = 'valueSoldOut',
|
|
49
|
+
Decrease = 'decrease',
|
|
50
|
+
Increase = 'increase',
|
|
51
|
+
Remove = 'remove',
|
|
52
|
+
Cart = 'cart',
|
|
53
|
+
CartCount = 'cartCount',
|
|
54
|
+
Quantity = 'quantity',
|
|
55
|
+
FreeShippingGap = 'freeShippingGap',
|
|
56
|
+
ChooseOption = 'chooseOption',
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** `{name}` / `{n}` / `{amount}` are filled by `shopWord`. */
|
|
60
|
+
export const SHOP_WORDS: Readonly<Record<ShopWord, CommerceLabel>> = Object.freeze({
|
|
61
|
+
[ShopWord.Added]: { en: 'Added ✓', he: 'נוסף ✓' },
|
|
62
|
+
[ShopWord.SoldOut]: { en: 'Sold out', he: 'אזל' },
|
|
63
|
+
[ShopWord.ValueSoldOut]: { en: '{name} is sold out', he: '{name} אזל מהמלאי' },
|
|
64
|
+
[ShopWord.Decrease]: { en: 'Decrease quantity', he: 'הפחתת כמות' },
|
|
65
|
+
[ShopWord.Increase]: { en: 'Increase quantity', he: 'הגדלת כמות' },
|
|
66
|
+
[ShopWord.Remove]: { en: 'Remove {name}', he: 'הסרת {name}' },
|
|
67
|
+
[ShopWord.Cart]: { en: 'Bag', he: 'סל' },
|
|
68
|
+
[ShopWord.CartCount]: { en: 'Bag, {n} items', he: 'סל, {n} פריטים' },
|
|
69
|
+
[ShopWord.Quantity]: { en: 'Quantity', he: 'כמות' },
|
|
70
|
+
[ShopWord.FreeShippingGap]: { en: 'Add {amount} more for free delivery', he: 'עוד {amount} למשלוח חינם' },
|
|
71
|
+
[ShopWord.ChooseOption]: { en: 'Choose {name}', he: 'בחירת {name}' },
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export function shopWord(word: ShopWord, lang: string | null | undefined, fill: Readonly<Record<string, string | number>> = {}): string {
|
|
75
|
+
const primary = String(lang ?? '').toLowerCase().split(/[-_]/)[0];
|
|
76
|
+
const label = SHOP_WORDS[word];
|
|
77
|
+
const raw = primary === 'he' || primary === 'iw' ? label.he : label.en;
|
|
78
|
+
return raw.replace(/\{(\w+)\}/g, (all, key) => (key in fill ? String(fill[key]) : all));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ── the sample a canvas shows when no product is bound (commerce board 10, "Studio Kadar") ────────────────────────
|
|
82
|
+
|
|
83
|
+
/** Board 10's tall speckled vase: Glaze (Sand · Ash · Charcoal — Charcoal sold out) × Size (24 · 32 · 40 cm — 40 sold
|
|
84
|
+
* out), ₪240 against ₪280. Major units, as the storefront serves them. */
|
|
85
|
+
export const SAMPLE_PRODUCT: StorefrontProduct = Object.freeze({
|
|
86
|
+
_id: 'sample-vase',
|
|
87
|
+
title: 'Tall speckled vase',
|
|
88
|
+
slug: 'tall-speckled-vase',
|
|
89
|
+
mainImage: null,
|
|
90
|
+
productVariants: true,
|
|
91
|
+
currency: CurrencyCode.ILS,
|
|
92
|
+
availability: 'in_stock',
|
|
93
|
+
pricing: { price: 240, compareat: 280 },
|
|
94
|
+
options: [
|
|
95
|
+
{ name: 'Glaze', values: ['Sand', 'Ash', 'Charcoal'] },
|
|
96
|
+
{ name: 'Size', values: ['24 cm', '32 cm', '40 cm'] },
|
|
97
|
+
],
|
|
98
|
+
// Rows in the order a page opens on: Sand · 32 cm first (the first sellable row is the default choice).
|
|
99
|
+
variants: ['Sand', 'Ash', 'Charcoal'].flatMap(glaze => ['32 cm', '24 cm', '40 cm'].map(size => ({
|
|
100
|
+
_id: `sample-${glaze}-${size}`.toLowerCase().replace(/\s+/g, ''),
|
|
101
|
+
name: `${glaze}-${size}`,
|
|
102
|
+
enable: true,
|
|
103
|
+
image: null,
|
|
104
|
+
price: size === '24 cm' ? 190 : 240,
|
|
105
|
+
compareat: size === '32 cm' ? 280 : null,
|
|
106
|
+
availability: glaze === 'Charcoal' || size === '40 cm' ? 'out' : 'in_stock',
|
|
107
|
+
}))),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/** The colours board 10 draws its sample glazes with (a sample value has no colour of its own). */
|
|
111
|
+
export const SAMPLE_SWATCHES: Readonly<Record<string, string>> = Object.freeze({ Sand: '#D8C6A8', Ash: '#F3F1EC', Charcoal: '#3A3B3D' });
|
|
112
|
+
|
|
113
|
+
/** The sample studio's money (VAT 18 %, ₪). */
|
|
114
|
+
export const SAMPLE_STUDIO = Object.freeze({ currency: CurrencyCode.ILS, vatPercent: 18, shippingMethods: [] as never[] });
|
|
115
|
+
|
|
116
|
+
/** The cart the canvas starts with: one sample line, so the drawer has a line to design. */
|
|
117
|
+
export const SAMPLE_CART_LINE = Object.freeze({ productId: 'sample-vase', variantId: 'sample-sand-32cm', quantity: 1 });
|