@occluze/tether 0.6.8 → 0.7.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/README.md +11 -11
- package/dist/esm/astro.js +2 -2
- package/dist/esm/chunks/account-UIPWJQ3Y.js +1 -0
- package/dist/esm/chunks/account-pages-6ROA6Y4V.js +1 -0
- package/dist/esm/chunks/analytics-2V632IRV.js +1 -0
- package/dist/esm/chunks/chunk-4GCXM764.js +1 -0
- package/dist/esm/chunks/chunk-56FCLJJD.js +1 -0
- package/dist/esm/chunks/chunk-5Z5O3767.js +1 -0
- package/dist/esm/chunks/chunk-6OZJCAGZ.js +1 -0
- package/dist/esm/chunks/chunk-A5SYCVQM.js +1 -0
- package/dist/esm/chunks/chunk-BA4LQJJQ.js +1 -0
- package/dist/esm/chunks/chunk-BFTZR4OB.js +4 -0
- package/dist/esm/chunks/chunk-C5JYQCWH.js +1 -0
- package/dist/esm/chunks/chunk-E5YO6274.js +1 -0
- package/dist/esm/chunks/chunk-MPDWM2NN.js +1 -0
- package/dist/esm/chunks/chunk-T44MUL4Y.js +1 -0
- package/dist/esm/chunks/chunk-YLUULATY.js +1 -0
- package/dist/esm/chunks/diagnose-3FEKRIEK.js +14 -0
- package/dist/esm/chunks/extras-6POFBNOP.js +1 -0
- package/dist/esm/chunks/markets-ui-FD6NI2OG.js +1 -0
- package/dist/esm/chunks/search-UJ73YBQ5.js +1 -0
- package/dist/esm/index.js +1 -1
- package/dist/esm/server.js +2 -2
- package/dist/presets/barba.esm.js +19 -23
- package/dist/tether.account-pages.js +320 -365
- package/dist/tether.account-pages.min.js +1 -1
- package/dist/tether.account.js +242 -491
- package/dist/tether.account.min.js +1 -1
- package/dist/tether.analytics.js +66 -88
- package/dist/tether.analytics.min.js +1 -1
- package/dist/tether.diagnose.js +36 -33
- package/dist/tether.diagnose.min.js +2 -2
- package/dist/tether.extras.js +28 -36
- package/dist/tether.extras.min.js +1 -1
- package/dist/tether.js +1270 -894
- package/dist/tether.markets.js +73 -90
- package/dist/tether.markets.min.js +1 -1
- package/dist/tether.min.js +0 -0
- package/dist/tether.min.js.map +4 -4
- package/dist/tether.search.js +44 -68
- package/dist/tether.search.min.js +1 -1
- package/dist/types/server/src/shared/account-orders.d.ts +44 -0
- package/dist/types/server/src/shared/account-routes.d.ts +153 -0
- package/dist/types/server/src/shared/api-versions.d.ts +13 -0
- package/dist/types/server/src/shared/error-message.d.ts +2 -0
- package/dist/types/server/src/shared/json.d.ts +25 -0
- package/dist/types/server/src/shared/market-codes.d.ts +16 -0
- package/dist/types/src/account/addresses-view.d.ts +5 -0
- package/dist/types/src/account/answers.d.ts +40 -0
- package/dist/types/src/account/markup.d.ts +19 -0
- package/dist/types/src/account/operations.d.ts +41 -0
- package/dist/types/src/account/order-lines.d.ts +13 -0
- package/dist/types/src/account/order-list.d.ts +12 -0
- package/dist/types/src/account/order-page.d.ts +6 -0
- package/dist/types/src/account/page-answers.d.ts +182 -0
- package/dist/types/src/account/page-events.d.ts +2 -0
- package/dist/types/src/account/server-client.d.ts +60 -0
- package/dist/types/src/account/subscriptions-view.d.ts +2 -0
- package/dist/types/src/account-api.d.ts +18 -0
- package/dist/types/src/account-marks.d.ts +15 -0
- package/dist/types/src/account-pages.d.ts +8 -0
- package/dist/types/src/account.d.ts +64 -0
- package/dist/types/src/amounts.d.ts +12 -0
- package/dist/types/src/analytics-shopify.d.ts +41 -0
- package/dist/types/src/analytics.d.ts +2 -0
- package/dist/types/src/api-version.d.ts +28 -0
- package/dist/types/src/astro.d.ts +42 -0
- package/dist/types/src/cart/cart-shape.d.ts +43 -0
- package/dist/types/src/cart/checkout.d.ts +16 -0
- package/dist/types/src/cart/drawer.d.ts +18 -0
- package/dist/types/src/cart/mutations.d.ts +79 -0
- package/dist/types/src/cart/render.d.ts +17 -0
- package/dist/types/src/cart/store.d.ts +182 -0
- package/dist/types/src/cart.d.ts +106 -0
- package/dist/types/src/client.d.ts +100 -0
- package/dist/types/src/collection.d.ts +25 -0
- package/dist/types/src/config-sections.d.ts +40 -0
- package/dist/types/src/config.d.ts +185 -0
- package/dist/types/src/controls/cart-controls.d.ts +2 -0
- package/dist/types/src/controls/discount.d.ts +2 -0
- package/dist/types/src/controls/extras.d.ts +5 -0
- package/dist/types/src/controls/navigation.d.ts +12 -0
- package/dist/types/src/diagnose-markup.d.ts +19 -0
- package/dist/types/src/diagnose.d.ts +60 -0
- package/dist/types/src/dom.d.ts +75 -0
- package/dist/types/src/extras.d.ts +21 -0
- package/dist/types/src/fragments.d.ts +158 -0
- package/dist/types/src/globals.d.ts +15 -0
- package/dist/types/src/images.d.ts +20 -0
- package/dist/types/src/index.d.ts +167 -0
- package/dist/types/src/initial.d.ts +56 -0
- package/dist/types/src/lazy.d.ts +28 -0
- package/dist/types/src/loader.d.ts +84 -0
- package/dist/types/src/market-codes.d.ts +6 -0
- package/dist/types/src/markets-ui.d.ts +32 -0
- package/dist/types/src/markets.d.ts +72 -0
- package/dist/types/src/markup.d.ts +131 -0
- package/dist/types/src/mounting.d.ts +46 -0
- package/dist/types/src/page-market.d.ts +27 -0
- package/dist/types/src/presets/barba.d.ts +56 -0
- package/dist/types/src/product-events.d.ts +2 -0
- package/dist/types/src/product-render.d.ts +17 -0
- package/dist/types/src/product-shape.d.ts +88 -0
- package/dist/types/src/product-state.d.ts +110 -0
- package/dist/types/src/product.d.ts +66 -0
- package/dist/types/src/repeaters.d.ts +16 -0
- package/dist/types/src/search.d.ts +33 -0
- package/dist/types/src/selling-plans.d.ts +64 -0
- package/dist/types/src/server.d.ts +88 -0
- package/dist/types/src/shopify-values.d.ts +46 -0
- package/dist/types/src/status.d.ts +33 -0
- package/dist/types/src/storage.d.ts +12 -0
- package/dist/types/src/store-html.d.ts +39 -0
- package/dist/types/src/storefront/catalog.d.ts +38 -0
- package/dist/types/src/storefront/json-ld.d.ts +20 -0
- package/dist/types/src/variant-render.d.ts +14 -0
- package/package.json +24 -23
- package/src/account/addresses-view.ts +58 -0
- package/src/account/answers.ts +79 -0
- package/src/account/markup.ts +36 -0
- package/src/account/operations.ts +145 -0
- package/src/account/{order-lines.js → order-lines.ts} +20 -41
- package/src/account/order-list.ts +107 -0
- package/src/account/order-page.ts +137 -0
- package/src/account/page-answers.ts +317 -0
- package/src/account/{page-events.js → page-events.ts} +55 -90
- package/src/account/server-client.ts +183 -0
- package/src/account/{subscriptions-view.js → subscriptions-view.ts} +25 -68
- package/src/account-api.ts +98 -0
- package/src/account-marks.ts +35 -0
- package/src/{account-pages.js → account-pages.ts} +29 -37
- package/src/account.ts +284 -0
- package/src/amounts.ts +64 -0
- package/src/{analytics-shopify.js → analytics-shopify.ts} +112 -107
- package/src/analytics.ts +315 -0
- package/src/{api-version.js → api-version.ts} +25 -31
- package/src/{astro.js → astro.ts} +110 -104
- package/src/cart/cart-shape.ts +25 -0
- package/src/cart/checkout.ts +101 -0
- package/src/cart/{drawer.js → drawer.ts} +18 -31
- package/src/cart/mutations.ts +237 -0
- package/src/cart/{render.js → render.ts} +68 -96
- package/src/cart/store.ts +279 -0
- package/src/{cart.js → cart.ts} +82 -128
- package/src/client.ts +391 -0
- package/src/{collection.js → collection.ts} +30 -38
- package/src/config-sections.ts +284 -0
- package/src/config.ts +480 -0
- package/src/controls/{cart-controls.js → cart-controls.ts} +22 -54
- package/src/controls/{discount.js → discount.ts} +13 -34
- package/src/controls/{extras.js → extras.ts} +15 -24
- package/src/controls/{navigation.js → navigation.ts} +24 -30
- package/src/{diagnose-markup.js → diagnose-markup.ts} +37 -72
- package/src/{diagnose.js → diagnose.ts} +95 -96
- package/src/dom.ts +268 -0
- package/src/{extras.js → extras.ts} +43 -52
- package/src/fragments.ts +261 -0
- package/src/globals.d.ts +15 -0
- package/src/{images.js → images.ts} +30 -43
- package/src/index.ts +415 -0
- package/src/initial.ts +101 -0
- package/src/lazy-iife.ts +197 -0
- package/src/lazy.ts +42 -0
- package/src/{loader.js → loader.ts} +149 -123
- package/src/market-codes.ts +6 -0
- package/src/{markets-ui.js → markets-ui.ts} +91 -68
- package/src/markets.ts +246 -0
- package/src/markup.ts +127 -0
- package/src/{mounting.js → mounting.ts} +88 -101
- package/src/{page-market.js → page-market.ts} +32 -38
- package/src/part-integrity.ts +7 -0
- package/src/parts/account-pages.ts +7 -0
- package/src/parts/account.ts +7 -0
- package/src/parts/analytics.ts +7 -0
- package/src/parts/diagnose.ts +7 -0
- package/src/parts/extras.ts +7 -0
- package/src/parts/markets.ts +7 -0
- package/src/parts/search.ts +7 -0
- package/src/presets/barba.ts +194 -0
- package/src/{product-events.js → product-events.ts} +33 -48
- package/src/{product-render.js → product-render.ts} +38 -77
- package/src/product-shape.ts +49 -0
- package/src/product-state.ts +318 -0
- package/src/{product.js → product.ts} +90 -126
- package/src/{repeaters.js → repeaters.ts} +40 -52
- package/src/{search.js → search.ts} +85 -103
- package/src/selling-plans.ts +145 -0
- package/src/server.ts +211 -0
- package/src/{shopify-values.js → shopify-values.ts} +24 -22
- package/src/{status.js → status.ts} +34 -54
- package/src/storage.ts +34 -0
- package/src/store-html.ts +359 -0
- package/src/storefront/catalog.ts +196 -0
- package/src/storefront/{json-ld.js → json-ld.ts} +45 -68
- package/src/{variant-render.js → variant-render.ts} +24 -50
- package/dist/esm/chunks/account-C3NP6HFB.js +0 -1
- package/dist/esm/chunks/account-pages-WLK374FO.js +0 -1
- package/dist/esm/chunks/analytics-G3SAWMNL.js +0 -1
- package/dist/esm/chunks/chunk-27PJJZGC.js +0 -1
- package/dist/esm/chunks/chunk-2FSXS55U.js +0 -1
- package/dist/esm/chunks/chunk-FCSWQDVV.js +0 -1
- package/dist/esm/chunks/chunk-FOMSMIAM.js +0 -1
- package/dist/esm/chunks/chunk-GZJTCLLW.js +0 -1
- package/dist/esm/chunks/chunk-HEQILZVR.js +0 -1
- package/dist/esm/chunks/chunk-JOOEXYF3.js +0 -4
- package/dist/esm/chunks/chunk-MPTQYLSM.js +0 -1
- package/dist/esm/chunks/chunk-NEHZ37TU.js +0 -3
- package/dist/esm/chunks/chunk-V74KGEJF.js +0 -1
- package/dist/esm/chunks/chunk-VT4CDUJJ.js +0 -1
- package/dist/esm/chunks/chunk-YJLWX2HK.js +0 -1
- package/dist/esm/chunks/diagnose-6VVGZNTY.js +0 -14
- package/dist/esm/chunks/extras-GJVRDFM3.js +0 -1
- package/dist/esm/chunks/markets-ui-UNB4PTA7.js +0 -1
- package/dist/esm/chunks/search-OU75RNQL.js +0 -1
- package/src/account/addresses-view.js +0 -92
- package/src/account/browser-session.js +0 -359
- package/src/account/markup.js +0 -48
- package/src/account/operations.js +0 -180
- package/src/account/order-list.js +0 -91
- package/src/account/order-page.js +0 -192
- package/src/account/server-client.js +0 -172
- package/src/account/shared.js +0 -111
- package/src/account/shopify-auth.js +0 -150
- package/src/account-api.js +0 -118
- package/src/account-links.js +0 -35
- package/src/account.js +0 -342
- package/src/amounts.js +0 -36
- package/src/analytics.js +0 -352
- package/src/cart/checkout.js +0 -97
- package/src/cart/mutations.js +0 -234
- package/src/cart/store.js +0 -326
- package/src/client.js +0 -267
- package/src/config.js +0 -349
- package/src/dom.js +0 -287
- package/src/fragments.js +0 -237
- package/src/index.js +0 -353
- package/src/initial.js +0 -87
- package/src/lazy-iife.js +0 -116
- package/src/lazy.js +0 -39
- package/src/market-codes.js +0 -35
- package/src/markets.js +0 -334
- package/src/markup.js +0 -64
- package/src/parts/account-pages.js +0 -6
- package/src/parts/account.js +0 -6
- package/src/parts/analytics.js +0 -6
- package/src/parts/diagnose.js +0 -6
- package/src/parts/extras.js +0 -6
- package/src/parts/markets.js +0 -6
- package/src/parts/search.js +0 -6
- package/src/presets/barba.js +0 -190
- package/src/product-state.js +0 -318
- package/src/selling-plans.js +0 -154
- package/src/server.js +0 -215
- package/src/store-html.js +0 -0
- package/src/storefront/catalog.js +0 -199
- package/types/astro.d.ts +0 -28
- package/types/barba.d.ts +0 -16
- package/types/index.d.ts +0 -577
- package/types/server.d.ts +0 -182
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a product scope shows for the visitor's choice: the variant, the purchase it is bought as, its prices, the
|
|
3
|
+
* text of every `tether-bind` and every `tether-show` condition. Pure, no DOM and no config: the browser scope
|
|
4
|
+
* (`product.ts`) and the server's `initialState()` (`initial.ts`) derive their view here, so a server-rendered page
|
|
5
|
+
* shows what the script shows when it takes over.
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
GID,
|
|
9
|
+
allPlans,
|
|
10
|
+
allocationFor,
|
|
11
|
+
findPlan,
|
|
12
|
+
planAdjustment,
|
|
13
|
+
planCheckoutCharge,
|
|
14
|
+
planPrice,
|
|
15
|
+
planSavings,
|
|
16
|
+
} from './selling-plans.ts';
|
|
17
|
+
import { fromMinorUnits, toMinorUnits } from './amounts.ts';
|
|
18
|
+
import type { Money } from './shopify-values.ts';
|
|
19
|
+
import type { Product, ProductVariant } from './fragments.ts';
|
|
20
|
+
import type { PlanInGroup } from './selling-plans.ts';
|
|
21
|
+
|
|
22
|
+
/** Option name → chosen value. */
|
|
23
|
+
export type Selection = Record<string, string>;
|
|
24
|
+
/**
|
|
25
|
+
* How the visitor wants to buy. Buying once keeps the plan offered next to the one-time price (the subscription
|
|
26
|
+
* binds `subscription-price` and `plan-name` show it, and subscribing switches to it); `null` for a product without
|
|
27
|
+
* plans.
|
|
28
|
+
*/
|
|
29
|
+
export type Purchase = { kind: 'one-time'; offeredPlanId: string | null } | { kind: 'subscription'; planId: string };
|
|
30
|
+
/** How the line is bought: on a plan only when the visitor subscribes and the variant is on that plan. */
|
|
31
|
+
type LinePurchase = { kind: 'one-time' } | { kind: 'subscription'; planId: string };
|
|
32
|
+
/** What the visitor chose, before it is checked against the product. */
|
|
33
|
+
interface ProductChoice {
|
|
34
|
+
selected: Selection;
|
|
35
|
+
purchase: Purchase;
|
|
36
|
+
quantity: number;
|
|
37
|
+
}
|
|
38
|
+
export type ShowCondition =
|
|
39
|
+
| 'in-stock'
|
|
40
|
+
| 'out-of-stock'
|
|
41
|
+
| 'backorder'
|
|
42
|
+
| 'on-sale'
|
|
43
|
+
| 'not-on-sale'
|
|
44
|
+
| 'has-variants'
|
|
45
|
+
| 'single-variant'
|
|
46
|
+
| 'low-stock'
|
|
47
|
+
| 'plenty-in-stock'
|
|
48
|
+
| 'subscription-available'
|
|
49
|
+
| 'subscription-unavailable'
|
|
50
|
+
| 'subscription-only'
|
|
51
|
+
| 'one-time-available'
|
|
52
|
+
| 'subscription-selected'
|
|
53
|
+
| 'one-time-selected'
|
|
54
|
+
| 'has-savings';
|
|
55
|
+
export interface ProductView {
|
|
56
|
+
variant: ProductVariant;
|
|
57
|
+
/** The selection, matching `variant`. */
|
|
58
|
+
selected: Selection;
|
|
59
|
+
/** The choice, made valid for the product and `variant`. */
|
|
60
|
+
purchase: Purchase;
|
|
61
|
+
/** The purchase's plan, offered or chosen. */
|
|
62
|
+
plan: PlanInGroup | null;
|
|
63
|
+
/** How the line is bought. */
|
|
64
|
+
line: LinePurchase;
|
|
65
|
+
/** The variant is on at least one plan. */
|
|
66
|
+
subscriptionAvailable: boolean;
|
|
67
|
+
quantity: number;
|
|
68
|
+
/** What one item costs as bought. */
|
|
69
|
+
price: Money;
|
|
70
|
+
/** Only when on sale. */
|
|
71
|
+
compareAtPrice: Money | null;
|
|
72
|
+
/** What `plan` saves on the one-time price. */
|
|
73
|
+
savings: { amount: Money | null; percentage: number };
|
|
74
|
+
show: Record<ShowCondition, boolean>;
|
|
75
|
+
}
|
|
76
|
+
/** How the caller writes values as text. */
|
|
77
|
+
export interface Formatters {
|
|
78
|
+
money: (money: Money | null) => string;
|
|
79
|
+
percent: (percentage: number) => string;
|
|
80
|
+
}
|
|
81
|
+
/** What a bind writes; `null` and `undefined` clear it. */
|
|
82
|
+
type BindText = string | number | null | undefined;
|
|
83
|
+
|
|
84
|
+
const PERCENT = 100;
|
|
85
|
+
|
|
86
|
+
const timesQuantity = (money: Money, quantity: number): Money =>
|
|
87
|
+
fromMinorUnits(toMinorUnits(money) * quantity, money.currencyCode);
|
|
88
|
+
|
|
89
|
+
export const selectionOf = (variant: ProductVariant): Selection =>
|
|
90
|
+
Object.fromEntries(variant.selectedOptions.map((option) => [option.name, option.value]));
|
|
91
|
+
|
|
92
|
+
/** @returns The variant with exactly these option values. */
|
|
93
|
+
function findVariant(product: Product, selected: Selection): ProductVariant | null {
|
|
94
|
+
return (
|
|
95
|
+
product.variants.nodes.find((variant) =>
|
|
96
|
+
variant.selectedOptions.every((option) => selected[option.name] === option.value),
|
|
97
|
+
) ?? null
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param name An option name.
|
|
103
|
+
* @returns True when a variant for sale has this value and the other selected values.
|
|
104
|
+
*/
|
|
105
|
+
export function isValueAvailable(product: Product, selected: Selection, name: string, value: string): boolean {
|
|
106
|
+
return product.variants.nodes.some(
|
|
107
|
+
(variant) =>
|
|
108
|
+
variant.availableForSale &&
|
|
109
|
+
variant.selectedOptions.every((option) =>
|
|
110
|
+
option.name === name ? option.value === value : selected[option.name] === option.value,
|
|
111
|
+
),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** @returns The plan subscribed on, or offered while buying once. */
|
|
116
|
+
export const purchasePlanId = (purchase: Purchase): string | null =>
|
|
117
|
+
purchase.kind === 'subscription' ? purchase.planId : purchase.offeredPlanId;
|
|
118
|
+
|
|
119
|
+
/** @returns On the plan when subscribing and the variant is on it, else once. */
|
|
120
|
+
export function linePurchase(purchase: Purchase, variant: ProductVariant | null): LinePurchase {
|
|
121
|
+
if (purchase.kind === 'subscription' && allocationFor(variant, purchase.planId)) return purchase;
|
|
122
|
+
return { kind: 'one-time' };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The purchase the visitor wants, made valid for the product and variant: one-time without plans, a subscription when
|
|
127
|
+
* the product requires one, and a plan the variant is on when it is on any (else a plan of the product, for the binds
|
|
128
|
+
* to offer).
|
|
129
|
+
*
|
|
130
|
+
* @param kind The way of buying the visitor asked for.
|
|
131
|
+
* @param preferredPlanId The plan to keep when it is the product's: the visitor's last one.
|
|
132
|
+
*/
|
|
133
|
+
export function normalisePurchase(
|
|
134
|
+
product: Product,
|
|
135
|
+
variant: ProductVariant | null,
|
|
136
|
+
kind: Purchase['kind'],
|
|
137
|
+
preferredPlanId: string | null,
|
|
138
|
+
): Purchase {
|
|
139
|
+
const plans = allPlans(product);
|
|
140
|
+
const known = plans.find((plan) => plan.id === preferredPlanId) ?? plans[0];
|
|
141
|
+
if (!known) return { kind: 'one-time', offeredPlanId: null };
|
|
142
|
+
const onVariant = allocationFor(variant, known.id)
|
|
143
|
+
? known
|
|
144
|
+
: (plans.find((plan) => allocationFor(variant, plan.id)) ?? known);
|
|
145
|
+
if (product.requiresSellingPlan || kind === 'subscription') return { kind: 'subscription', planId: onVariant.id };
|
|
146
|
+
return { kind: 'one-time', offeredPlanId: onVariant.id };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
interface InitialChoiceOptions {
|
|
150
|
+
/** A variant id or gid (`?variant=`); the first variant for sale without. */
|
|
151
|
+
variant?: string | null | undefined;
|
|
152
|
+
/** A plan to subscribe on (`?selling_plan=`). */
|
|
153
|
+
sellingPlan?: string | null | undefined;
|
|
154
|
+
/** The plan offered first, by id, name or option value. */
|
|
155
|
+
defaultPlan?: string | null | undefined;
|
|
156
|
+
/** `subscription` subscribes; anything else buys once. */
|
|
157
|
+
defaultPurchase?: string | null | undefined;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** What a product starts with before the visitor chooses anything. */
|
|
161
|
+
export function initialChoice(
|
|
162
|
+
product: Product,
|
|
163
|
+
{ variant, sellingPlan, defaultPlan, defaultPurchase }: InitialChoiceOptions,
|
|
164
|
+
): Omit<ProductChoice, 'quantity'> {
|
|
165
|
+
const variants = product.variants.nodes;
|
|
166
|
+
const wantedId = GID.variant(variant);
|
|
167
|
+
const start =
|
|
168
|
+
variants.find((candidate) => candidate.id === wantedId) ??
|
|
169
|
+
variants.find((candidate) => candidate.availableForSale) ??
|
|
170
|
+
variants[0] ??
|
|
171
|
+
null;
|
|
172
|
+
const fromUrl = findPlan(product, sellingPlan);
|
|
173
|
+
const offered = fromUrl ?? findPlan(product, defaultPlan);
|
|
174
|
+
const kind = fromUrl || defaultPurchase === 'subscription' ? 'subscription' : 'one-time';
|
|
175
|
+
return {
|
|
176
|
+
selected: start ? selectionOf(start) : {},
|
|
177
|
+
purchase: normalisePurchase(product, start, kind, offered?.id ?? null),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function showConditions(
|
|
182
|
+
variant: ProductVariant,
|
|
183
|
+
lowStock: number,
|
|
184
|
+
variantCount: number,
|
|
185
|
+
sale: Pick<ProductView, 'line' | 'subscriptionAvailable' | 'compareAtPrice' | 'savings'>,
|
|
186
|
+
requiresSellingPlan: boolean,
|
|
187
|
+
): Record<ShowCondition, boolean> {
|
|
188
|
+
const stock = variant.quantityAvailable;
|
|
189
|
+
const low = stock != null && stock > 0 && stock <= lowStock;
|
|
190
|
+
const onSale = sale.compareAtPrice !== null;
|
|
191
|
+
const onPlan = sale.line.kind === 'subscription';
|
|
192
|
+
return {
|
|
193
|
+
'in-stock': variant.availableForSale,
|
|
194
|
+
'out-of-stock': !variant.availableForSale,
|
|
195
|
+
backorder: variant.availableForSale && variant.currentlyNotInStock,
|
|
196
|
+
'on-sale': onSale,
|
|
197
|
+
'not-on-sale': !onSale,
|
|
198
|
+
'has-variants': variantCount > 1,
|
|
199
|
+
'single-variant': variantCount === 1,
|
|
200
|
+
'low-stock': low,
|
|
201
|
+
'plenty-in-stock': variant.availableForSale && !low,
|
|
202
|
+
'subscription-available': sale.subscriptionAvailable,
|
|
203
|
+
'subscription-unavailable': !sale.subscriptionAvailable,
|
|
204
|
+
'subscription-only': requiresSellingPlan,
|
|
205
|
+
'one-time-available': !requiresSellingPlan,
|
|
206
|
+
'subscription-selected': onPlan,
|
|
207
|
+
'one-time-selected': !onPlan,
|
|
208
|
+
'has-savings': onPlan ? sale.savings.percentage > 0 : onSale,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The view for the visitor's choice. A selection no variant has falls back to the first variant.
|
|
214
|
+
*
|
|
215
|
+
* @param lowStock `low-stock` at this many or fewer.
|
|
216
|
+
* @returns `null` for a product without variants.
|
|
217
|
+
*/
|
|
218
|
+
export function productView(product: Product, choice: ProductChoice, lowStock: number): ProductView | null {
|
|
219
|
+
const exact = findVariant(product, choice.selected);
|
|
220
|
+
const variant = exact ?? product.variants.nodes[0];
|
|
221
|
+
if (!variant) return null;
|
|
222
|
+
const purchase = normalisePurchase(product, variant, choice.purchase.kind, purchasePlanId(choice.purchase));
|
|
223
|
+
const planId = purchasePlanId(purchase);
|
|
224
|
+
const plans = allPlans(product);
|
|
225
|
+
const line = linePurchase(purchase, variant);
|
|
226
|
+
const allocation = line.kind === 'subscription' ? allocationFor(variant, line.planId) : null;
|
|
227
|
+
// The plan helpers fall back to the one-time price only without a variant, which `variant` rules out.
|
|
228
|
+
const price = allocation ? (planPrice(variant, planId) ?? variant.price) : variant.price;
|
|
229
|
+
const compareAt = allocation?.priceAdjustments[0]?.compareAtPrice ?? variant.compareAtPrice;
|
|
230
|
+
const sale = {
|
|
231
|
+
line,
|
|
232
|
+
subscriptionAvailable: plans.some((plan) => allocationFor(variant, plan.id)),
|
|
233
|
+
compareAtPrice: compareAt && parseFloat(compareAt.amount) > parseFloat(price.amount) ? compareAt : null,
|
|
234
|
+
savings: planSavings(variant, planId),
|
|
235
|
+
};
|
|
236
|
+
return {
|
|
237
|
+
...sale,
|
|
238
|
+
variant,
|
|
239
|
+
selected: exact ? choice.selected : selectionOf(variant),
|
|
240
|
+
purchase,
|
|
241
|
+
plan: plans.find((plan) => plan.id === planId) ?? null,
|
|
242
|
+
quantity: choice.quantity,
|
|
243
|
+
price,
|
|
244
|
+
show: showConditions(variant, lowStock, product.variants.nodes.length, sale, product.requiresSellingPlan),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** The `tether-bind` values that follow the variant and purchase. Thunks, so only the binds on the page are computed. */
|
|
249
|
+
export function variantBindTexts(view: ProductView, format: Formatters): Map<string, () => BindText> {
|
|
250
|
+
const { variant, plan, price, compareAtPrice, quantity } = view;
|
|
251
|
+
const planId = purchasePlanId(view.purchase);
|
|
252
|
+
// Prices for the quantity count one item when it is 0 (an optional bundle item).
|
|
253
|
+
const forQuantity = (money: Money) => timesQuantity(money, Math.max(1, quantity));
|
|
254
|
+
const checkoutCharge = () => {
|
|
255
|
+
const charge = view.line.kind === 'subscription' ? (planCheckoutCharge(variant, planId) ?? price) : price;
|
|
256
|
+
return format.money(forQuantity(charge));
|
|
257
|
+
};
|
|
258
|
+
const discountPercentage = () => {
|
|
259
|
+
if (!compareAtPrice) return '0%';
|
|
260
|
+
return `${Math.round((1 - parseFloat(price.amount) / parseFloat(compareAtPrice.amount)) * PERCENT)}%`;
|
|
261
|
+
};
|
|
262
|
+
const discountAmount = () => {
|
|
263
|
+
if (!compareAtPrice) return '';
|
|
264
|
+
return format.money(fromMinorUnits(toMinorUnits(compareAtPrice) - toMinorUnits(price), price.currencyCode));
|
|
265
|
+
};
|
|
266
|
+
const texts: Record<string, () => BindText> = {
|
|
267
|
+
price: () => format.money(price),
|
|
268
|
+
'total-price': () => format.money(forQuantity(price)),
|
|
269
|
+
'one-time-price': () => format.money(variant.price),
|
|
270
|
+
'subscription-price': () => format.money(planId ? planPrice(variant, planId) : null),
|
|
271
|
+
'subscription-savings': () => format.percent(view.savings.percentage),
|
|
272
|
+
'subscription-savings-amount': () => format.money(view.savings.amount),
|
|
273
|
+
'checkout-charge': checkoutCharge,
|
|
274
|
+
'plan-name': () => plan?.name,
|
|
275
|
+
'plan-description': () => plan?.description,
|
|
276
|
+
'plan-group': () => plan?.group.name,
|
|
277
|
+
'compare-at-price': () => format.money(compareAtPrice),
|
|
278
|
+
'unit-price': () => format.money(variant.unitPrice),
|
|
279
|
+
'discount-percentage': discountPercentage,
|
|
280
|
+
'discount-amount': discountAmount,
|
|
281
|
+
'variant-title': () => variant.title,
|
|
282
|
+
'variant-id': () => variant.id,
|
|
283
|
+
sku: () => variant.sku,
|
|
284
|
+
barcode: () => variant.barcode,
|
|
285
|
+
stock: () => variant.quantityAvailable,
|
|
286
|
+
quantity: () => quantity,
|
|
287
|
+
};
|
|
288
|
+
return new Map(Object.entries(texts));
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* The `tether-plan-bind` values of one plan in a plan list, for the variant.
|
|
292
|
+
*
|
|
293
|
+
* @param planId The list item's `tether-plan-value`.
|
|
294
|
+
* @param plan The product's plan with that id.
|
|
295
|
+
*/
|
|
296
|
+
export function planBindTexts(
|
|
297
|
+
variant: ProductVariant,
|
|
298
|
+
planId: string,
|
|
299
|
+
plan: PlanInGroup | undefined,
|
|
300
|
+
format: Formatters,
|
|
301
|
+
): Map<string, () => BindText> {
|
|
302
|
+
const adjustmentText = () => {
|
|
303
|
+
const adjustment = planAdjustment(plan);
|
|
304
|
+
if (!adjustment) return '';
|
|
305
|
+
return adjustment.type === 'percentage' ? `${adjustment.value}%` : format.money(adjustment.value);
|
|
306
|
+
};
|
|
307
|
+
const texts: Record<string, () => BindText> = {
|
|
308
|
+
name: () => plan?.name,
|
|
309
|
+
// The option values alone ("1 month"), without the price text a store may put in the plan name.
|
|
310
|
+
option: () => (plan?.options ?? []).map((option) => option.value).join(', '),
|
|
311
|
+
description: () => plan?.description,
|
|
312
|
+
price: () => format.money(planPrice(variant, planId)),
|
|
313
|
+
savings: () => format.percent(planSavings(variant, planId).percentage),
|
|
314
|
+
'savings-amount': () => format.money(planSavings(variant, planId).amount),
|
|
315
|
+
adjustment: adjustmentText,
|
|
316
|
+
};
|
|
317
|
+
return new Map(Object.entries(texts));
|
|
318
|
+
}
|