@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.3
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/LICENSE +21 -0
- package/README.md +98 -10
- package/dist/abi/canvas-live.d.ts +49 -0
- package/dist/abi/canvas-live.js +88 -0
- package/dist/abi/cart.d.ts +48 -0
- package/dist/abi/cart.js +256 -0
- package/dist/abi/convert.d.ts +126 -0
- package/dist/abi/convert.js +493 -0
- package/dist/abi/data-api.d.ts +11 -0
- package/dist/abi/data-api.js +178 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/page-data.d.ts +198 -0
- package/dist/abi/page-data.js +577 -0
- package/dist/abi/page.d.ts +23 -0
- package/dist/abi/page.js +46 -0
- package/dist/abi/referrals.d.ts +23 -0
- package/dist/abi/referrals.js +89 -0
- package/dist/abi/rewards.d.ts +27 -0
- package/dist/abi/rewards.js +82 -0
- package/dist/abi/seo.d.ts +47 -0
- package/dist/abi/seo.js +68 -0
- package/dist/abi/shell.d.ts +22 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/wire.d.ts +38 -0
- package/dist/abi/wire.js +84 -0
- package/dist/build.d.ts +113 -0
- package/dist/build.js +294 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.js +76 -0
- package/dist/contract.d.ts +6 -0
- package/dist/contract.js +6 -0
- package/dist/headless/ai.d.ts +132 -0
- package/dist/headless/ai.js +99 -0
- package/dist/headless/hooks.d.ts +273 -0
- package/dist/headless/hooks.js +533 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +36 -0
- package/dist/headless/index.js +38 -0
- package/dist/headless/money.d.ts +25 -0
- package/dist/headless/money.js +61 -0
- package/dist/headless/page.d.ts +43 -0
- package/dist/headless/page.js +53 -0
- package/dist/headless/platform.d.ts +55 -0
- package/dist/headless/platform.js +77 -0
- package/dist/headless/primitives/cart.d.ts +90 -0
- package/dist/headless/primitives/cart.js +153 -0
- package/dist/headless/primitives/collection.d.ts +68 -0
- package/dist/headless/primitives/collection.js +80 -0
- package/dist/headless/primitives/product.d.ts +101 -0
- package/dist/headless/primitives/product.js +117 -0
- package/dist/headless/primitives/storefront.d.ts +106 -0
- package/dist/headless/primitives/storefront.js +186 -0
- package/dist/headless/primitives/util.d.ts +19 -0
- package/dist/headless/primitives/util.js +42 -0
- package/dist/headless/strings.d.ts +9 -0
- package/dist/headless/strings.js +186 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/theme.d.ts +99 -0
- package/dist/headless/theme.js +54 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +18 -553
- package/dist/runtime/data/StoreProvider.d.ts +56 -0
- package/dist/runtime/data/StoreProvider.js +308 -0
- package/dist/runtime/data/handle.d.ts +14 -0
- package/dist/runtime/data/handle.js +16 -0
- package/dist/runtime/data/routes.d.ts +87 -0
- package/dist/runtime/data/routes.js +154 -0
- package/dist/runtime/data/to-ui.d.ts +237 -0
- package/dist/runtime/data/to-ui.js +759 -0
- package/dist/runtime/entries/register.d.ts +9 -0
- package/dist/runtime/entries/register.js +9 -0
- package/dist/runtime/entries/sections.d.ts +16 -0
- package/dist/runtime/entries/sections.js +19 -0
- package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
- package/dist/runtime/kit/SectionTree.js +100 -0
- package/dist/runtime/kit/analytics.d.ts +63 -0
- package/dist/runtime/kit/analytics.js +233 -0
- package/dist/runtime/kit/cart.d.ts +167 -0
- package/dist/runtime/kit/cart.js +544 -0
- package/dist/{types → runtime/kit}/composition.d.ts +1 -1
- package/dist/runtime/kit/composition.js +3 -0
- package/dist/{types → runtime/kit}/config.d.ts +2 -2
- package/dist/runtime/kit/config.js +6 -0
- package/dist/runtime/kit/consent.d.ts +29 -0
- package/dist/runtime/kit/consent.js +159 -0
- package/dist/runtime/kit/contract/content.d.ts +164 -0
- package/dist/runtime/kit/contract/content.js +83 -0
- package/dist/runtime/kit/contract/field-types.d.ts +290 -0
- package/dist/runtime/kit/contract/field-types.js +129 -0
- package/dist/runtime/kit/contract/groups.d.ts +230 -0
- package/dist/runtime/kit/contract/groups.js +402 -0
- package/dist/runtime/kit/contract/index.d.ts +33 -0
- package/dist/runtime/kit/contract/index.js +33 -0
- package/dist/runtime/kit/contract/transform.d.ts +82 -0
- package/dist/runtime/kit/contract/transform.js +133 -0
- package/dist/runtime/kit/contract/validate.d.ts +67 -0
- package/dist/runtime/kit/contract/validate.js +388 -0
- package/dist/{types → runtime/kit}/data.d.ts +276 -7
- package/dist/runtime/kit/data.js +1117 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/{types → runtime/kit}/dynamic-source.d.ts +4 -4
- package/dist/runtime/kit/dynamic-source.js +143 -0
- package/dist/runtime/kit/index.d.ts +24 -0
- package/dist/runtime/kit/index.js +24 -0
- package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
- package/dist/runtime/kit/jsx-to-json.js +28 -0
- package/dist/{types → runtime/kit}/mount.d.ts +25 -4
- package/dist/runtime/kit/mount.js +124 -0
- package/dist/{types → runtime/kit}/registry.d.ts +1 -1
- package/dist/runtime/kit/registry.js +12 -0
- package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
- package/dist/runtime/kit/ssg.js +56 -0
- package/dist/{types → runtime/kit}/storefront.d.ts +206 -7
- package/dist/runtime/kit/storefront.js +723 -0
- package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
- package/dist/runtime/kit/theme-context.js +25 -0
- package/dist/runtime/kit/types.d.ts +142 -0
- package/dist/runtime/kit/types.js +1 -0
- package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
- package/dist/runtime/platform/TrackingPixels.js +63 -0
- package/dist/runtime/platform/environment.d.ts +68 -0
- package/dist/runtime/platform/environment.js +85 -0
- package/dist/runtime/platform/locale.d.ts +27 -0
- package/dist/runtime/platform/locale.js +42 -0
- package/dist/runtime/platform/route-analytics.d.ts +15 -0
- package/dist/runtime/platform/route-analytics.js +72 -0
- package/dist/runtime/platform/safe-href.d.ts +25 -0
- package/dist/runtime/platform/safe-href.js +69 -0
- package/dist/runtime/platform/safe-values.d.ts +37 -0
- package/dist/runtime/platform/safe-values.js +140 -0
- package/dist/runtime/render/SectionRenderer.d.ts +4 -0
- package/dist/runtime/render/SectionRenderer.js +111 -0
- package/dist/runtime/render/listing-query.d.ts +47 -0
- package/dist/runtime/render/listing-query.js +114 -0
- package/dist/runtime/render/localized-copy.d.ts +86 -0
- package/dist/runtime/render/localized-copy.js +129 -0
- package/dist/runtime/render/product-listing.d.ts +11 -0
- package/dist/runtime/render/product-listing.js +240 -0
- package/dist/runtime/render/section-actions.d.ts +44 -0
- package/dist/runtime/render/section-actions.js +169 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +81 -0
- package/dist/runtime/seo/head.d.ts +87 -0
- package/dist/runtime/seo/head.js +224 -0
- package/dist/runtime/seo/structured-data.d.ts +22 -0
- package/dist/runtime/seo/structured-data.js +115 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +113 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +14 -0
- package/dist/runtime/shell/RuntimeBridge.js +82 -0
- package/dist/runtime/shell/overlay-state.d.ts +31 -0
- package/dist/runtime/shell/overlay-state.js +59 -0
- package/dist/runtime/shell/preview-mode.d.ts +2 -0
- package/dist/runtime/shell/preview-mode.js +11 -0
- package/dist/runtime/shell/spa-router.d.ts +14 -0
- package/dist/runtime/shell/spa-router.js +76 -0
- package/dist/runtime/shell/url-redirects.d.ts +6 -0
- package/dist/runtime/shell/url-redirects.js +37 -0
- package/dist/runtime/slots.d.ts +74 -0
- package/dist/runtime/slots.js +16 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +40 -0
- package/dist/sdk/index.d.ts +24 -0
- package/dist/sdk/index.js +24 -0
- package/dist/sdk/referrals.d.ts +62 -0
- package/dist/sdk/referrals.js +20 -0
- package/dist/sdk/rewards.d.ts +85 -0
- package/dist/sdk/rewards.js +29 -0
- package/dist/sdk/runtime-context.d.ts +120 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +197 -0
- package/dist/sdk/schema.js +121 -0
- package/dist/sdk/server-render.d.ts +8 -0
- package/dist/sdk/server-render.js +12 -0
- package/dist/sdk/store-lookup.d.ts +27 -0
- package/dist/sdk/store-lookup.js +29 -0
- package/dist/sdk/store.d.ts +115 -0
- package/dist/sdk/store.js +1 -0
- package/dist/sdk/sx.d.ts +40 -0
- package/dist/sdk/sx.js +112 -0
- package/dist/sdk/theme.d.ts +152 -0
- package/dist/sdk/theme.js +10 -0
- package/dist/sdk/types.d.ts +395 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +65 -0
- package/dist/server.js +370 -0
- package/docs/headless-api.md +220 -0
- package/package.json +77 -23
- package/dist/index.js.map +0 -1
- package/dist/ssg-BZ5FYTw7.js +0 -1802
- package/dist/ssg-BZ5FYTw7.js.map +0 -1
- package/dist/ssg.js +0 -8
- package/dist/ssg.js.map +0 -1
- package/dist/types/cart.d.ts +0 -86
- package/dist/types/editor.d.ts +0 -5
- package/dist/types/index.d.ts +0 -20
- package/dist/types/preview-bridge.d.ts +0 -9
- package/dist/types/types.d.ts +0 -44
package/dist/ssg-BZ5FYTw7.js
DELETED
|
@@ -1,1802 +0,0 @@
|
|
|
1
|
-
import { jsx, Fragment } from "react/jsx-runtime";
|
|
2
|
-
import { renderToString } from "react-dom/server";
|
|
3
|
-
import { createContext, useContext, useState, useRef, useCallback, useEffect } from "react";
|
|
4
|
-
const SettingsContext = createContext({});
|
|
5
|
-
const LocaleContext = createContext({});
|
|
6
|
-
function ThemeProvider({
|
|
7
|
-
settings = {},
|
|
8
|
-
locale = {},
|
|
9
|
-
children
|
|
10
|
-
}) {
|
|
11
|
-
return /* @__PURE__ */ jsx(SettingsContext.Provider, { value: settings, children: /* @__PURE__ */ jsx(LocaleContext.Provider, { value: locale, children }) });
|
|
12
|
-
}
|
|
13
|
-
function useSettings() {
|
|
14
|
-
return useContext(SettingsContext);
|
|
15
|
-
}
|
|
16
|
-
function useT() {
|
|
17
|
-
const locale = useContext(LocaleContext);
|
|
18
|
-
return (key) => locale[key] ?? key;
|
|
19
|
-
}
|
|
20
|
-
const registry = /* @__PURE__ */ new Map();
|
|
21
|
-
function registerSections(defs) {
|
|
22
|
-
for (const def of defs) registry.set(def.name, def);
|
|
23
|
-
}
|
|
24
|
-
function getSection(name) {
|
|
25
|
-
return registry.get(name);
|
|
26
|
-
}
|
|
27
|
-
function allSections() {
|
|
28
|
-
return [...registry.values()];
|
|
29
|
-
}
|
|
30
|
-
function resolveAttributes(def, settings) {
|
|
31
|
-
const out = { ...settings ?? {} };
|
|
32
|
-
for (const [key, spec] of Object.entries(def.attributes ?? {})) {
|
|
33
|
-
if (out[key] === void 0 && spec.default !== void 0) out[key] = spec.default;
|
|
34
|
-
}
|
|
35
|
-
return out;
|
|
36
|
-
}
|
|
37
|
-
function normalizeBlocks(blocks, order) {
|
|
38
|
-
if (Array.isArray(blocks)) return blocks;
|
|
39
|
-
if (blocks && typeof blocks === "object") {
|
|
40
|
-
const nodes = [];
|
|
41
|
-
const byRef = /* @__PURE__ */ new Map();
|
|
42
|
-
for (const [key, raw] of Object.entries(blocks)) {
|
|
43
|
-
if (!raw || typeof raw !== "object" || typeof raw.type !== "string") continue;
|
|
44
|
-
const node = { ...raw, id: raw.id ?? key };
|
|
45
|
-
nodes.push(node);
|
|
46
|
-
byRef.set(key, node);
|
|
47
|
-
if (node.id) byRef.set(node.id, node);
|
|
48
|
-
}
|
|
49
|
-
if (Array.isArray(order) && order.length) {
|
|
50
|
-
const ordered = [];
|
|
51
|
-
const used = /* @__PURE__ */ new Set();
|
|
52
|
-
for (const ref of order) {
|
|
53
|
-
const n = byRef.get(ref);
|
|
54
|
-
if (n && !used.has(n)) {
|
|
55
|
-
ordered.push(n);
|
|
56
|
-
used.add(n);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
for (const n of nodes) if (!used.has(n)) ordered.push(n);
|
|
60
|
-
return ordered;
|
|
61
|
-
}
|
|
62
|
-
return nodes;
|
|
63
|
-
}
|
|
64
|
-
return [];
|
|
65
|
-
}
|
|
66
|
-
function RenderNode({
|
|
67
|
-
node,
|
|
68
|
-
path
|
|
69
|
-
}) {
|
|
70
|
-
const def = getSection(node.type);
|
|
71
|
-
if (!def) {
|
|
72
|
-
console.warn(`[tanqory] unknown section "${node.type}"`);
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
const Comp = def.component;
|
|
76
|
-
const attributes = resolveAttributes(def, node.settings);
|
|
77
|
-
return /* @__PURE__ */ jsx(
|
|
78
|
-
"div",
|
|
79
|
-
{
|
|
80
|
-
"data-tq-section-id": node.id,
|
|
81
|
-
"data-tq-section-type": node.type,
|
|
82
|
-
"data-tq-path": path,
|
|
83
|
-
style: { display: "contents" },
|
|
84
|
-
children: /* @__PURE__ */ jsx(Comp, { attributes, children: normalizeBlocks(node.blocks, node.order).map(
|
|
85
|
-
(child, i) => /* @__PURE__ */ jsx(RenderNode, { node: child, path: `${path}.${i}` }, child.id ?? i)
|
|
86
|
-
) })
|
|
87
|
-
}
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
function SectionTree({ tree }) {
|
|
91
|
-
return /* @__PURE__ */ jsx(Fragment, { children: tree.map((node, i) => /* @__PURE__ */ jsx(RenderNode, { node, path: String(i) }, node.id ?? i)) });
|
|
92
|
-
}
|
|
93
|
-
const EMPTY_PAGE_INFO = {
|
|
94
|
-
hasNextPage: false,
|
|
95
|
-
hasPreviousPage: false,
|
|
96
|
-
startCursor: null,
|
|
97
|
-
endCursor: null
|
|
98
|
-
};
|
|
99
|
-
function normalizePageInfo(n) {
|
|
100
|
-
if (!n) return EMPTY_PAGE_INFO;
|
|
101
|
-
return {
|
|
102
|
-
hasNextPage: Boolean(n.hasNextPage),
|
|
103
|
-
hasPreviousPage: Boolean(n.hasPreviousPage),
|
|
104
|
-
startCursor: n.startCursor ?? null,
|
|
105
|
-
endCursor: n.endCursor ?? null
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
function normalizeFilters(arr) {
|
|
109
|
-
return (arr ?? []).map((f) => ({
|
|
110
|
-
id: f.id,
|
|
111
|
-
label: f.label,
|
|
112
|
-
type: f.type,
|
|
113
|
-
values: (f.values ?? []).map((v) => ({
|
|
114
|
-
id: v.id,
|
|
115
|
-
label: v.label,
|
|
116
|
-
count: Number(v.count ?? 0),
|
|
117
|
-
input: typeof v.input === "string" ? v.input : JSON.stringify(v.input)
|
|
118
|
-
}))
|
|
119
|
-
}));
|
|
120
|
-
}
|
|
121
|
-
const MENU_FIELDS = (
|
|
122
|
-
/* GraphQL */
|
|
123
|
-
`
|
|
124
|
-
fragment MenuFields on Menu {
|
|
125
|
-
id
|
|
126
|
-
handle
|
|
127
|
-
title
|
|
128
|
-
items {
|
|
129
|
-
...MenuItemFields
|
|
130
|
-
items { ...MenuItemFields items { ...MenuItemFields } }
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
fragment MenuItemFields on MenuItem { id title url type resourceId }
|
|
134
|
-
`
|
|
135
|
-
);
|
|
136
|
-
const ARTICLE_FIELDS = (
|
|
137
|
-
/* GraphQL */
|
|
138
|
-
`
|
|
139
|
-
fragment ArticleFields on Article {
|
|
140
|
-
id
|
|
141
|
-
handle
|
|
142
|
-
title
|
|
143
|
-
excerpt
|
|
144
|
-
contentHtml
|
|
145
|
-
image { url altText }
|
|
146
|
-
author: authorV2 { name }
|
|
147
|
-
publishedAt
|
|
148
|
-
tags
|
|
149
|
-
}
|
|
150
|
-
`
|
|
151
|
-
);
|
|
152
|
-
const PRODUCT_CARD = (
|
|
153
|
-
/* GraphQL */
|
|
154
|
-
`
|
|
155
|
-
fragment ProductCard on Product {
|
|
156
|
-
id
|
|
157
|
-
handle
|
|
158
|
-
title
|
|
159
|
-
availableForSale
|
|
160
|
-
vendor
|
|
161
|
-
productType
|
|
162
|
-
tags
|
|
163
|
-
featuredImage { url altText }
|
|
164
|
-
priceRange { minVariantPrice { amount currencyCode } }
|
|
165
|
-
compareAtPriceRange { maxVariantPrice { amount currencyCode } }
|
|
166
|
-
firstAvailableVariant { id }
|
|
167
|
-
}
|
|
168
|
-
`
|
|
169
|
-
);
|
|
170
|
-
const CUSTOMER_FIELDS = (
|
|
171
|
-
/* GraphQL */
|
|
172
|
-
`
|
|
173
|
-
fragment AddressFields on MailingAddress {
|
|
174
|
-
id firstName lastName company address1 address2 city province country zip phone
|
|
175
|
-
formatted(withName: true)
|
|
176
|
-
}
|
|
177
|
-
fragment CustomerFields on Customer {
|
|
178
|
-
id email firstName lastName displayName phone acceptsMarketing numberOfOrders
|
|
179
|
-
defaultAddress { ...AddressFields }
|
|
180
|
-
addresses(first: 20) { nodes { ...AddressFields } }
|
|
181
|
-
}
|
|
182
|
-
`
|
|
183
|
-
);
|
|
184
|
-
const ORDER_FIELDS = (
|
|
185
|
-
/* GraphQL */
|
|
186
|
-
`
|
|
187
|
-
fragment OrderFields on Order {
|
|
188
|
-
id name orderNumber processedAt financialStatus fulfillmentStatus statusUrl
|
|
189
|
-
currentTotalPrice { amount currencyCode }
|
|
190
|
-
lineItems(first: 50) {
|
|
191
|
-
nodes {
|
|
192
|
-
title variantTitle quantity
|
|
193
|
-
discountedTotalPrice { amount currencyCode }
|
|
194
|
-
variant { image { url altText } }
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
`
|
|
199
|
-
);
|
|
200
|
-
const ZERO = { amount: "0", currencyCode: "USD" };
|
|
201
|
-
function img(node) {
|
|
202
|
-
if (!node || !node.url) return null;
|
|
203
|
-
return node.altText ? { url: node.url, altText: node.altText } : { url: node.url };
|
|
204
|
-
}
|
|
205
|
-
function normalizeMenuItem(n) {
|
|
206
|
-
return {
|
|
207
|
-
id: n.id,
|
|
208
|
-
title: n.title,
|
|
209
|
-
url: n.url ?? null,
|
|
210
|
-
type: n.type,
|
|
211
|
-
resourceId: n.resourceId ?? null,
|
|
212
|
-
items: (n.items ?? []).map(normalizeMenuItem)
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
function normalizeMenu(n) {
|
|
216
|
-
if (!n) return null;
|
|
217
|
-
return { id: n.id, handle: n.handle, title: n.title, items: (n.items ?? []).map(normalizeMenuItem) };
|
|
218
|
-
}
|
|
219
|
-
function normalizeArticle(n, blogHandle) {
|
|
220
|
-
return {
|
|
221
|
-
id: n.id,
|
|
222
|
-
handle: n.handle,
|
|
223
|
-
title: n.title,
|
|
224
|
-
excerpt: n.excerpt ?? null,
|
|
225
|
-
contentHtml: n.contentHtml ?? "",
|
|
226
|
-
image: img(n.image),
|
|
227
|
-
author: n.author?.name ?? null,
|
|
228
|
-
...n.publishedAt ? { publishedAt: n.publishedAt } : {},
|
|
229
|
-
tags: n.tags ?? [],
|
|
230
|
-
...blogHandle ? { blogHandle } : {}
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
function normalizeMetaobject(n) {
|
|
234
|
-
const fields = (n.fields ?? []).map((f) => ({
|
|
235
|
-
key: f.key,
|
|
236
|
-
value: f.value ?? null,
|
|
237
|
-
type: f.type
|
|
238
|
-
}));
|
|
239
|
-
const values = {};
|
|
240
|
-
for (const f of fields) values[f.key] = f.value;
|
|
241
|
-
return { id: n.id, handle: n.handle, type: n.type, fields, values };
|
|
242
|
-
}
|
|
243
|
-
function normalizeAddress(n) {
|
|
244
|
-
return {
|
|
245
|
-
id: n.id,
|
|
246
|
-
firstName: n.firstName ?? null,
|
|
247
|
-
lastName: n.lastName ?? null,
|
|
248
|
-
company: n.company ?? null,
|
|
249
|
-
address1: n.address1 ?? null,
|
|
250
|
-
address2: n.address2 ?? null,
|
|
251
|
-
city: n.city ?? null,
|
|
252
|
-
province: n.province ?? null,
|
|
253
|
-
country: n.country ?? null,
|
|
254
|
-
zip: n.zip ?? null,
|
|
255
|
-
phone: n.phone ?? null,
|
|
256
|
-
formatted: n.formatted ?? []
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
function normalizeCustomer(n) {
|
|
260
|
-
return {
|
|
261
|
-
id: n.id,
|
|
262
|
-
email: n.email ?? null,
|
|
263
|
-
firstName: n.firstName ?? null,
|
|
264
|
-
lastName: n.lastName ?? null,
|
|
265
|
-
displayName: n.displayName ?? "",
|
|
266
|
-
phone: n.phone ?? null,
|
|
267
|
-
acceptsMarketing: Boolean(n.acceptsMarketing),
|
|
268
|
-
numberOfOrders: Number(n.numberOfOrders ?? 0),
|
|
269
|
-
defaultAddress: n.defaultAddress ? normalizeAddress(n.defaultAddress) : null,
|
|
270
|
-
addresses: (n.addresses?.nodes ?? []).map(normalizeAddress)
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
function normalizeOrder(n) {
|
|
274
|
-
return {
|
|
275
|
-
id: n.id,
|
|
276
|
-
name: n.name,
|
|
277
|
-
orderNumber: Number(n.orderNumber ?? 0),
|
|
278
|
-
processedAt: n.processedAt,
|
|
279
|
-
financialStatus: n.financialStatus ?? null,
|
|
280
|
-
fulfillmentStatus: n.fulfillmentStatus ?? "UNFULFILLED",
|
|
281
|
-
totalPrice: n.currentTotalPrice ?? ZERO,
|
|
282
|
-
statusUrl: n.statusUrl ?? "",
|
|
283
|
-
lineItems: (n.lineItems?.nodes ?? []).map((li) => ({
|
|
284
|
-
title: li.title,
|
|
285
|
-
variantTitle: li.variantTitle ?? null,
|
|
286
|
-
quantity: Number(li.quantity ?? 0),
|
|
287
|
-
image: img(li.variant?.image),
|
|
288
|
-
totalPrice: li.discountedTotalPrice ?? ZERO
|
|
289
|
-
}))
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
function normalizeShopPolicy(n) {
|
|
293
|
-
if (!n) return null;
|
|
294
|
-
return { handle: n.handle, title: n.title, url: n.url, ...n.body ? { body: n.body } : {} };
|
|
295
|
-
}
|
|
296
|
-
function normalizeShop(n) {
|
|
297
|
-
if (!n) return null;
|
|
298
|
-
return {
|
|
299
|
-
name: n.name,
|
|
300
|
-
description: n.description ?? null,
|
|
301
|
-
brand: n.brand ? {
|
|
302
|
-
logo: img(n.brand.logo),
|
|
303
|
-
slogan: n.brand.slogan ?? null,
|
|
304
|
-
shortDescription: n.brand.shortDescription ?? null
|
|
305
|
-
} : null,
|
|
306
|
-
policies: {
|
|
307
|
-
privacy: normalizeShopPolicy(n.privacyPolicy),
|
|
308
|
-
refund: normalizeShopPolicy(n.refundPolicy),
|
|
309
|
-
termsOfService: normalizeShopPolicy(n.termsOfService),
|
|
310
|
-
shipping: normalizeShopPolicy(n.shippingPolicy),
|
|
311
|
-
subscription: normalizeShopPolicy(n.subscriptionPolicy)
|
|
312
|
-
},
|
|
313
|
-
cookieBanner: n.cookieBanner ? {
|
|
314
|
-
enabled: Boolean(n.cookieBanner.enabled),
|
|
315
|
-
dataSharingTitle: n.cookieBanner.dataSharingTitle ?? null,
|
|
316
|
-
dataSharingVisible: n.cookieBanner.dataSharingVisible !== false
|
|
317
|
-
} : null
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
function errs(arr) {
|
|
321
|
-
return (arr ?? []).map((e) => e?.message).filter((m) => Boolean(m));
|
|
322
|
-
}
|
|
323
|
-
const BOOTSTRAP_SHOP_MENU = (
|
|
324
|
-
/* GraphQL */
|
|
325
|
-
`
|
|
326
|
-
shop {
|
|
327
|
-
name
|
|
328
|
-
description
|
|
329
|
-
brand { logo { url altText } slogan shortDescription }
|
|
330
|
-
privacyPolicy { handle title url }
|
|
331
|
-
refundPolicy { handle title url }
|
|
332
|
-
termsOfService { handle title url }
|
|
333
|
-
shippingPolicy { handle title url }
|
|
334
|
-
subscriptionPolicy { handle title url }
|
|
335
|
-
cookieBanner { enabled dataSharingTitle dataSharingVisible }
|
|
336
|
-
}
|
|
337
|
-
mainMenu: menu(handle: "main-menu") { ...MenuFields }
|
|
338
|
-
footerMenu: menu(handle: "footer") { ...MenuFields }
|
|
339
|
-
`
|
|
340
|
-
);
|
|
341
|
-
const BOOTSTRAP_SHOP_MENU_FRAGMENTS = MENU_FIELDS;
|
|
342
|
-
function readBootstrapShopMenu(boot) {
|
|
343
|
-
const menus = /* @__PURE__ */ new Map();
|
|
344
|
-
const main = normalizeMenu(boot?.mainMenu);
|
|
345
|
-
if (main) menus.set("main-menu", main);
|
|
346
|
-
const footer = normalizeMenu(boot?.footerMenu);
|
|
347
|
-
if (footer) menus.set("footer", footer);
|
|
348
|
-
return { shop: normalizeShop(boot?.shop), menus };
|
|
349
|
-
}
|
|
350
|
-
function createStorefrontMethods(graphql, helpers, menuCache) {
|
|
351
|
-
const { normalizeProduct: normalizeProduct2 } = helpers;
|
|
352
|
-
const fetchMenu = async (handle) => {
|
|
353
|
-
const cached = menuCache.get(handle);
|
|
354
|
-
if (cached) return cached;
|
|
355
|
-
const res = await graphql(
|
|
356
|
-
`${MENU_FIELDS}
|
|
357
|
-
query Menu($handle: String!) { menu(handle: $handle) { ...MenuFields } }`,
|
|
358
|
-
{ handle }
|
|
359
|
-
);
|
|
360
|
-
const menu = normalizeMenu(res?.menu);
|
|
361
|
-
if (menu) menuCache.set(handle, menu);
|
|
362
|
-
return menu;
|
|
363
|
-
};
|
|
364
|
-
const search = async (query, opts) => {
|
|
365
|
-
const res = await graphql(
|
|
366
|
-
`${PRODUCT_CARD}${ARTICLE_FIELDS}
|
|
367
|
-
query Search($query: String!, $first: Int!, $after: String, $types: [SearchType!]) {
|
|
368
|
-
search(query: $query, first: $first, after: $after, types: $types) {
|
|
369
|
-
totalCount
|
|
370
|
-
productFilters { id label type values { id label count input } }
|
|
371
|
-
pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
|
|
372
|
-
nodes {
|
|
373
|
-
__typename
|
|
374
|
-
... on Product { ...ProductCard }
|
|
375
|
-
... on Page { handle title }
|
|
376
|
-
... on Article { ...ArticleFields }
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}`,
|
|
380
|
-
{ query, first: opts?.first ?? 24, after: opts?.after ?? null, types: opts?.types ?? null }
|
|
381
|
-
);
|
|
382
|
-
const s = res?.search ?? {};
|
|
383
|
-
const nodes = s.nodes ?? [];
|
|
384
|
-
return {
|
|
385
|
-
totalCount: Number(s.totalCount ?? 0),
|
|
386
|
-
products: nodes.filter((n) => n.__typename === "Product").map(normalizeProduct2),
|
|
387
|
-
pages: nodes.filter((n) => n.__typename === "Page").map((n) => ({ handle: n.handle, title: n.title })),
|
|
388
|
-
articles: nodes.filter((n) => n.__typename === "Article").map((n) => normalizeArticle(n)),
|
|
389
|
-
filters: normalizeFilters(s.productFilters),
|
|
390
|
-
pageInfo: normalizePageInfo(s.pageInfo)
|
|
391
|
-
};
|
|
392
|
-
};
|
|
393
|
-
const collectionProducts = async (handle, opts) => {
|
|
394
|
-
const res = await graphql(
|
|
395
|
-
`${PRODUCT_CARD}
|
|
396
|
-
query CollectionProducts(
|
|
397
|
-
$handle: String!, $first: Int!, $after: String,
|
|
398
|
-
$sortKey: ProductCollectionSortKey, $reverse: Boolean, $filters: [ProductFilterInput!]
|
|
399
|
-
) {
|
|
400
|
-
collection(handle: $handle) {
|
|
401
|
-
products(first: $first, after: $after, sortKey: $sortKey, reverse: $reverse, filters: $filters) {
|
|
402
|
-
nodes { ...ProductCard }
|
|
403
|
-
filters { id label type values { id label count input } }
|
|
404
|
-
pageInfo { hasNextPage hasPreviousPage startCursor endCursor }
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
}`,
|
|
408
|
-
{
|
|
409
|
-
handle,
|
|
410
|
-
first: opts?.first ?? 24,
|
|
411
|
-
after: opts?.after ?? null,
|
|
412
|
-
sortKey: opts?.sortKey ?? null,
|
|
413
|
-
reverse: opts?.reverse ?? null,
|
|
414
|
-
filters: opts?.filters ?? null
|
|
415
|
-
}
|
|
416
|
-
);
|
|
417
|
-
const c = res?.collection?.products ?? {};
|
|
418
|
-
return {
|
|
419
|
-
products: (c.nodes ?? []).map(normalizeProduct2),
|
|
420
|
-
filters: normalizeFilters(c.filters),
|
|
421
|
-
pageInfo: normalizePageInfo(c.pageInfo)
|
|
422
|
-
};
|
|
423
|
-
};
|
|
424
|
-
const predictiveSearch = async (query, opts) => {
|
|
425
|
-
const res = await graphql(
|
|
426
|
-
`${PRODUCT_CARD}${ARTICLE_FIELDS}
|
|
427
|
-
query Predictive($query: String!, $limit: Int!) {
|
|
428
|
-
predictiveSearch(query: $query, limit: $limit) {
|
|
429
|
-
queries { text }
|
|
430
|
-
products { ...ProductCard }
|
|
431
|
-
collections { handle title image { url altText } }
|
|
432
|
-
pages { handle title }
|
|
433
|
-
articles { ...ArticleFields }
|
|
434
|
-
}
|
|
435
|
-
}`,
|
|
436
|
-
{ query, limit: opts?.limit ?? 4 }
|
|
437
|
-
);
|
|
438
|
-
const ps = res?.predictiveSearch ?? {};
|
|
439
|
-
return {
|
|
440
|
-
queries: (ps.queries ?? []).map((q) => q.text),
|
|
441
|
-
products: (ps.products ?? []).map(normalizeProduct2),
|
|
442
|
-
collections: (ps.collections ?? []).map((c) => ({
|
|
443
|
-
handle: c.handle,
|
|
444
|
-
title: c.title,
|
|
445
|
-
image: img(c.image)
|
|
446
|
-
})),
|
|
447
|
-
pages: (ps.pages ?? []).map((p) => ({ handle: p.handle, title: p.title })),
|
|
448
|
-
articles: (ps.articles ?? []).map((a) => normalizeArticle(a))
|
|
449
|
-
};
|
|
450
|
-
};
|
|
451
|
-
const productRecommendations = async (productId) => {
|
|
452
|
-
const res = await graphql(
|
|
453
|
-
`${PRODUCT_CARD}
|
|
454
|
-
query Recs($productId: ID!) {
|
|
455
|
-
productRecommendations(productId: $productId) { ...ProductCard }
|
|
456
|
-
}`,
|
|
457
|
-
{ productId }
|
|
458
|
-
);
|
|
459
|
-
return (res?.productRecommendations ?? []).map(normalizeProduct2);
|
|
460
|
-
};
|
|
461
|
-
const blogByHandle = async (handle, opts) => {
|
|
462
|
-
const res = await graphql(
|
|
463
|
-
`${ARTICLE_FIELDS}
|
|
464
|
-
query Blog($handle: String!, $first: Int!) {
|
|
465
|
-
blog(handle: $handle) {
|
|
466
|
-
id handle title
|
|
467
|
-
articles(first: $first) { nodes { ...ArticleFields } }
|
|
468
|
-
}
|
|
469
|
-
}`,
|
|
470
|
-
{ handle, first: opts?.articles ?? 20 }
|
|
471
|
-
);
|
|
472
|
-
const b = res?.blog;
|
|
473
|
-
if (!b) return null;
|
|
474
|
-
return {
|
|
475
|
-
id: b.id,
|
|
476
|
-
handle: b.handle,
|
|
477
|
-
title: b.title,
|
|
478
|
-
articles: (b.articles?.nodes ?? []).map((a) => normalizeArticle(a, b.handle))
|
|
479
|
-
};
|
|
480
|
-
};
|
|
481
|
-
const articleByHandle = async (blogHandle, articleHandle) => {
|
|
482
|
-
const res = await graphql(
|
|
483
|
-
`${ARTICLE_FIELDS}
|
|
484
|
-
query Article($handle: ArticleHandleInput!) {
|
|
485
|
-
article(handle: $handle) { ...ArticleFields }
|
|
486
|
-
}`,
|
|
487
|
-
{ handle: { blogHandle, articleHandle } }
|
|
488
|
-
);
|
|
489
|
-
return res?.article ? normalizeArticle(res.article, blogHandle) : null;
|
|
490
|
-
};
|
|
491
|
-
const metaobject = async (type, handle) => {
|
|
492
|
-
const res = await graphql(
|
|
493
|
-
`query Metaobject($handle: MetaobjectHandleInput!) {
|
|
494
|
-
metaobject(handle: $handle) { id handle type fields { key value type } }
|
|
495
|
-
}`,
|
|
496
|
-
{ handle: { type, handle } }
|
|
497
|
-
);
|
|
498
|
-
return res?.metaobject ? normalizeMetaobject(res.metaobject) : null;
|
|
499
|
-
};
|
|
500
|
-
const metaobjects = async (type, opts) => {
|
|
501
|
-
const res = await graphql(
|
|
502
|
-
`query Metaobjects($type: String!, $first: Int!) {
|
|
503
|
-
metaobjects(type: $type, first: $first) {
|
|
504
|
-
nodes { id handle type fields { key value type } }
|
|
505
|
-
}
|
|
506
|
-
}`,
|
|
507
|
-
{ type, first: opts?.first ?? 50 }
|
|
508
|
-
);
|
|
509
|
-
return (res?.metaobjects?.nodes ?? []).map(normalizeMetaobject);
|
|
510
|
-
};
|
|
511
|
-
const customer = {
|
|
512
|
-
async login(email, password) {
|
|
513
|
-
const res = await graphql(
|
|
514
|
-
`mutation Login($input: CustomerAccessTokenCreateInput!) {
|
|
515
|
-
customerAccessTokenCreate(input: $input) {
|
|
516
|
-
customerAccessToken { accessToken expiresAt }
|
|
517
|
-
customerUserErrors { message }
|
|
518
|
-
}
|
|
519
|
-
}`,
|
|
520
|
-
{ input: { email, password } }
|
|
521
|
-
);
|
|
522
|
-
const p = res?.customerAccessTokenCreate;
|
|
523
|
-
return {
|
|
524
|
-
token: p?.customerAccessToken?.accessToken ?? null,
|
|
525
|
-
expiresAt: p?.customerAccessToken?.expiresAt ?? null,
|
|
526
|
-
errors: errs(p?.customerUserErrors)
|
|
527
|
-
};
|
|
528
|
-
},
|
|
529
|
-
async logout(token) {
|
|
530
|
-
await graphql(
|
|
531
|
-
`mutation Logout($t: String!) {
|
|
532
|
-
customerAccessTokenDelete(customerAccessToken: $t) { deletedAccessToken userErrors { message } }
|
|
533
|
-
}`,
|
|
534
|
-
{ t: token }
|
|
535
|
-
);
|
|
536
|
-
},
|
|
537
|
-
async renew(token) {
|
|
538
|
-
const res = await graphql(
|
|
539
|
-
`mutation Renew($t: String!) {
|
|
540
|
-
customerAccessTokenRenew(customerAccessToken: $t) {
|
|
541
|
-
customerAccessToken { accessToken expiresAt }
|
|
542
|
-
userErrors { message }
|
|
543
|
-
}
|
|
544
|
-
}`,
|
|
545
|
-
{ t: token }
|
|
546
|
-
);
|
|
547
|
-
const p = res?.customerAccessTokenRenew;
|
|
548
|
-
return {
|
|
549
|
-
token: p?.customerAccessToken?.accessToken ?? null,
|
|
550
|
-
expiresAt: p?.customerAccessToken?.expiresAt ?? null,
|
|
551
|
-
errors: errs(p?.userErrors)
|
|
552
|
-
};
|
|
553
|
-
},
|
|
554
|
-
async register(input) {
|
|
555
|
-
const res = await graphql(
|
|
556
|
-
`${CUSTOMER_FIELDS}
|
|
557
|
-
mutation Register($input: CustomerCreateInput!) {
|
|
558
|
-
customerCreate(input: $input) {
|
|
559
|
-
customer { ...CustomerFields }
|
|
560
|
-
customerUserErrors { message }
|
|
561
|
-
}
|
|
562
|
-
}`,
|
|
563
|
-
{ input }
|
|
564
|
-
);
|
|
565
|
-
const p = res?.customerCreate;
|
|
566
|
-
const errors = errs(p?.customerUserErrors);
|
|
567
|
-
return {
|
|
568
|
-
ok: Boolean(p?.customer) && errors.length === 0,
|
|
569
|
-
errors,
|
|
570
|
-
...p?.customer ? { data: normalizeCustomer(p.customer) } : {}
|
|
571
|
-
};
|
|
572
|
-
},
|
|
573
|
-
async get(token) {
|
|
574
|
-
const res = await graphql(
|
|
575
|
-
`${CUSTOMER_FIELDS}
|
|
576
|
-
query Me($t: String) { customer(customerAccessToken: $t) { ...CustomerFields } }`,
|
|
577
|
-
{ t: token }
|
|
578
|
-
);
|
|
579
|
-
return res?.customer ? normalizeCustomer(res.customer) : null;
|
|
580
|
-
},
|
|
581
|
-
async orders(token, opts) {
|
|
582
|
-
const res = await graphql(
|
|
583
|
-
`${ORDER_FIELDS}
|
|
584
|
-
query Orders($t: String, $first: Int!) {
|
|
585
|
-
customer(customerAccessToken: $t) {
|
|
586
|
-
orders(first: $first, sortKey: PROCESSED_AT, reverse: true) { nodes { ...OrderFields } }
|
|
587
|
-
}
|
|
588
|
-
}`,
|
|
589
|
-
{ t: token, first: opts?.first ?? 20 }
|
|
590
|
-
);
|
|
591
|
-
return (res?.customer?.orders?.nodes ?? []).map(normalizeOrder);
|
|
592
|
-
},
|
|
593
|
-
async orderByLookup(orderNumber, email) {
|
|
594
|
-
const res = await graphql(
|
|
595
|
-
`${ORDER_FIELDS}
|
|
596
|
-
query Lookup($n: String!, $e: String!) {
|
|
597
|
-
orderByLookup(orderNumber: $n, email: $e) { ...OrderFields }
|
|
598
|
-
}`,
|
|
599
|
-
{ n: orderNumber, e: email }
|
|
600
|
-
);
|
|
601
|
-
return res?.orderByLookup ? normalizeOrder(res.orderByLookup) : null;
|
|
602
|
-
},
|
|
603
|
-
async createAddress(token, address) {
|
|
604
|
-
const res = await graphql(
|
|
605
|
-
`mutation AddrCreate($t: String!, $a: MailingAddressInput!) {
|
|
606
|
-
customerAddressCreate(customerAccessToken: $t, address: $a) {
|
|
607
|
-
customerAddress { id }
|
|
608
|
-
customerUserErrors { message }
|
|
609
|
-
}
|
|
610
|
-
}`,
|
|
611
|
-
{ t: token, a: address }
|
|
612
|
-
);
|
|
613
|
-
const p = res?.customerAddressCreate;
|
|
614
|
-
const errors = errs(p?.customerUserErrors);
|
|
615
|
-
return {
|
|
616
|
-
ok: Boolean(p?.customerAddress?.id) && errors.length === 0,
|
|
617
|
-
errors,
|
|
618
|
-
...p?.customerAddress?.id ? { data: { id: p.customerAddress.id } } : {}
|
|
619
|
-
};
|
|
620
|
-
},
|
|
621
|
-
async updateAddress(token, id, address) {
|
|
622
|
-
const res = await graphql(
|
|
623
|
-
`mutation AddrUpdate($t: String!, $id: ID!, $a: MailingAddressInput!) {
|
|
624
|
-
customerAddressUpdate(customerAccessToken: $t, id: $id, address: $a) {
|
|
625
|
-
customerAddress { id }
|
|
626
|
-
customerUserErrors { message }
|
|
627
|
-
}
|
|
628
|
-
}`,
|
|
629
|
-
{ t: token, id, a: address }
|
|
630
|
-
);
|
|
631
|
-
const errors = errs(res?.customerAddressUpdate?.customerUserErrors);
|
|
632
|
-
return { ok: errors.length === 0, errors };
|
|
633
|
-
},
|
|
634
|
-
async deleteAddress(token, id) {
|
|
635
|
-
const res = await graphql(
|
|
636
|
-
`mutation AddrDelete($t: String!, $id: ID!) {
|
|
637
|
-
customerAddressDelete(customerAccessToken: $t, id: $id) {
|
|
638
|
-
deletedCustomerAddressId
|
|
639
|
-
customerUserErrors { message }
|
|
640
|
-
}
|
|
641
|
-
}`,
|
|
642
|
-
{ t: token, id }
|
|
643
|
-
);
|
|
644
|
-
const errors = errs(res?.customerAddressDelete?.customerUserErrors);
|
|
645
|
-
return { ok: errors.length === 0, errors };
|
|
646
|
-
},
|
|
647
|
-
async setDefaultAddress(token, addressId) {
|
|
648
|
-
const res = await graphql(
|
|
649
|
-
`mutation AddrDefault($t: String!, $id: ID!) {
|
|
650
|
-
customerDefaultAddressUpdate(customerAccessToken: $t, addressId: $id) {
|
|
651
|
-
customer { id }
|
|
652
|
-
customerUserErrors { message }
|
|
653
|
-
}
|
|
654
|
-
}`,
|
|
655
|
-
{ t: token, id: addressId }
|
|
656
|
-
);
|
|
657
|
-
const errors = errs(res?.customerDefaultAddressUpdate?.customerUserErrors);
|
|
658
|
-
return { ok: errors.length === 0, errors };
|
|
659
|
-
}
|
|
660
|
-
};
|
|
661
|
-
return {
|
|
662
|
-
fetchMenu,
|
|
663
|
-
search,
|
|
664
|
-
collectionProducts,
|
|
665
|
-
predictiveSearch,
|
|
666
|
-
productRecommendations,
|
|
667
|
-
blogByHandle,
|
|
668
|
-
articleByHandle,
|
|
669
|
-
metaobject,
|
|
670
|
-
metaobjects,
|
|
671
|
-
customer
|
|
672
|
-
};
|
|
673
|
-
}
|
|
674
|
-
const CUSTOMER_TOKEN_KEY = "tq-customer-token";
|
|
675
|
-
const customerTokenStore = {
|
|
676
|
-
get() {
|
|
677
|
-
if (typeof localStorage === "undefined") return null;
|
|
678
|
-
return localStorage.getItem(CUSTOMER_TOKEN_KEY);
|
|
679
|
-
},
|
|
680
|
-
set(token) {
|
|
681
|
-
if (typeof localStorage === "undefined") return;
|
|
682
|
-
localStorage.setItem(CUSTOMER_TOKEN_KEY, token);
|
|
683
|
-
},
|
|
684
|
-
clear() {
|
|
685
|
-
if (typeof localStorage === "undefined") return;
|
|
686
|
-
localStorage.removeItem(CUSTOMER_TOKEN_KEY);
|
|
687
|
-
}
|
|
688
|
-
};
|
|
689
|
-
const DataContext = createContext(null);
|
|
690
|
-
function DataProvider({ value, children }) {
|
|
691
|
-
return /* @__PURE__ */ jsx(DataContext.Provider, { value, children });
|
|
692
|
-
}
|
|
693
|
-
function useData() {
|
|
694
|
-
const ctx = useContext(DataContext);
|
|
695
|
-
if (!ctx) throw new Error("useData must be used within <DataProvider>");
|
|
696
|
-
return ctx;
|
|
697
|
-
}
|
|
698
|
-
function createMockData(collections) {
|
|
699
|
-
const productsByHandle = /* @__PURE__ */ new Map();
|
|
700
|
-
const allProducts = [];
|
|
701
|
-
for (const c of collections) {
|
|
702
|
-
for (const p of c.products) {
|
|
703
|
-
if (!productsByHandle.has(p.handle)) {
|
|
704
|
-
productsByHandle.set(p.handle, p);
|
|
705
|
-
allProducts.push(p);
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
const mkItem = (id, title, url, type = "HTTP") => ({ id, title, url, type, items: [] });
|
|
710
|
-
const mkMenu = (handle, title, items) => ({ id: `mock/menu/${handle}`, handle, title, items });
|
|
711
|
-
const mainMenu = mkMenu("main-menu", "Main menu", [
|
|
712
|
-
{
|
|
713
|
-
...mkItem("mm-shop", "Shop", "/collections/all", "COLLECTIONS"),
|
|
714
|
-
// submenu: the store's collections, so the header dropdown is real data
|
|
715
|
-
items: collections.slice(0, 6).map((c, i) => mkItem(`mm-shop-${i}`, c.title, `/collections/${c.handle}`, "COLLECTION"))
|
|
716
|
-
},
|
|
717
|
-
mkItem("mm-collections", "Collections", "/collections", "CATALOG"),
|
|
718
|
-
mkItem("mm-about", "About", "/pages/about", "PAGE"),
|
|
719
|
-
mkItem("mm-journal", "Journal", "/blogs/news", "BLOG")
|
|
720
|
-
]);
|
|
721
|
-
const menus = /* @__PURE__ */ new Map([
|
|
722
|
-
["main-menu", mainMenu],
|
|
723
|
-
["footer", mkMenu("footer", "Footer", [
|
|
724
|
-
mkItem("fm-privacy", "Privacy policy", "/policies/privacy-policy", "SHOP_POLICY"),
|
|
725
|
-
mkItem("fm-contact", "Contact", "/pages/contact", "PAGE")
|
|
726
|
-
])],
|
|
727
|
-
["footer-shop", mkMenu("footer-shop", "Shop", [
|
|
728
|
-
mkItem("fs-all", "All products", "/collections/all"),
|
|
729
|
-
mkItem("fs-new", "New arrivals", "/collections/new"),
|
|
730
|
-
mkItem("fs-sale", "Sale", "/collections/sale"),
|
|
731
|
-
mkItem("fs-gift", "Gift cards", "/products/gift-card")
|
|
732
|
-
])],
|
|
733
|
-
["footer-help", mkMenu("footer-help", "Help", [
|
|
734
|
-
mkItem("fh-contact", "Contact", "/pages/contact"),
|
|
735
|
-
mkItem("fh-ship", "Shipping", "/pages/shipping"),
|
|
736
|
-
mkItem("fh-returns", "Returns", "/pages/returns"),
|
|
737
|
-
mkItem("fh-faq", "FAQ", "/pages/faq")
|
|
738
|
-
])],
|
|
739
|
-
["footer-company", mkMenu("footer-company", "Company", [
|
|
740
|
-
mkItem("fc-about", "About", "/pages/about"),
|
|
741
|
-
mkItem("fc-journal", "Journal", "/blogs/news"),
|
|
742
|
-
mkItem("fc-sustain", "Sustainability", "/pages/sustainability"),
|
|
743
|
-
mkItem("fc-press", "Press", "/pages/press")
|
|
744
|
-
])]
|
|
745
|
-
]);
|
|
746
|
-
const articles = [
|
|
747
|
-
{
|
|
748
|
-
id: "mock/article/welcome",
|
|
749
|
-
handle: "welcome",
|
|
750
|
-
title: "Welcome to the shop",
|
|
751
|
-
excerpt: "A quick hello and what to expect.",
|
|
752
|
-
contentHtml: "<p>Welcome — this is mock article content for local development.</p>",
|
|
753
|
-
image: null,
|
|
754
|
-
author: "Studio",
|
|
755
|
-
publishedAt: "2024-01-01T00:00:00.000Z",
|
|
756
|
-
tags: ["news"],
|
|
757
|
-
blogHandle: "news"
|
|
758
|
-
},
|
|
759
|
-
{
|
|
760
|
-
id: "mock/article/behind-the-design",
|
|
761
|
-
handle: "behind-the-design",
|
|
762
|
-
title: "Behind the design",
|
|
763
|
-
excerpt: "How this starter theme is built.",
|
|
764
|
-
contentHtml: "<p>Built with Tanqory theme-kit sections.</p>",
|
|
765
|
-
image: null,
|
|
766
|
-
author: "Studio",
|
|
767
|
-
publishedAt: "2024-01-02T00:00:00.000Z",
|
|
768
|
-
tags: ["design"],
|
|
769
|
-
blogHandle: "news"
|
|
770
|
-
}
|
|
771
|
-
];
|
|
772
|
-
const blog = { id: "mock/blog/news", handle: "news", title: "News", articles };
|
|
773
|
-
const shop = {
|
|
774
|
-
name: "nova",
|
|
775
|
-
description: "A clean starter storefront powered by Tanqory.",
|
|
776
|
-
brand: { logo: null, slogan: "Modern essentials", shortDescription: "Starter theme" },
|
|
777
|
-
policies: {
|
|
778
|
-
privacy: { handle: "privacy-policy", title: "Privacy policy", url: "/policies/privacy-policy", body: "<p>Mock privacy policy.</p>" },
|
|
779
|
-
refund: { handle: "refund-policy", title: "Refund policy", url: "/policies/refund-policy", body: "<p>Mock refund policy.</p>" },
|
|
780
|
-
termsOfService: { handle: "terms-of-service", title: "Terms of service", url: "/policies/terms-of-service", body: "<p>Mock terms of service.</p>" },
|
|
781
|
-
shipping: { handle: "shipping-policy", title: "Shipping policy", url: "/policies/shipping-policy", body: "<p>Mock shipping policy.</p>" },
|
|
782
|
-
subscription: null
|
|
783
|
-
},
|
|
784
|
-
cookieBanner: { enabled: true, dataSharingTitle: "Do not sell or share my personal information", dataSharingVisible: true }
|
|
785
|
-
};
|
|
786
|
-
const EMPTY_PI = { hasNextPage: false, hasPreviousPage: false, startCursor: null, endCursor: null };
|
|
787
|
-
const match = (q) => {
|
|
788
|
-
const needle = q.trim().toLowerCase();
|
|
789
|
-
return needle ? allProducts.filter((p) => p.title.toLowerCase().includes(needle)) : [];
|
|
790
|
-
};
|
|
791
|
-
return {
|
|
792
|
-
collectionByHandle: (handle) => collections.find((c) => c.handle === handle) ?? null,
|
|
793
|
-
allCollections: () => collections,
|
|
794
|
-
productByHandle: (handle) => productsByHandle.get(handle) ?? null,
|
|
795
|
-
pageByHandle: () => null,
|
|
796
|
-
// Localization mirrors what the store DASHBOARD (Markets) would publish —
|
|
797
|
-
// NOT a theme config list. Countries carry their currency; languages are the
|
|
798
|
-
// store's published locales. The live source fills this from store-api.
|
|
799
|
-
localization: {
|
|
800
|
-
country: { isoCode: "TH", name: "Thailand", currency: { isoCode: "THB", symbol: "฿" }, market: null },
|
|
801
|
-
availableCountries: [
|
|
802
|
-
{ isoCode: "TH", name: "Thailand", currency: { isoCode: "THB", symbol: "฿" }, market: null },
|
|
803
|
-
{ isoCode: "US", name: "United States", currency: { isoCode: "USD", symbol: "$" }, market: null },
|
|
804
|
-
{ isoCode: "JP", name: "Japan", currency: { isoCode: "JPY", symbol: "¥" }, market: null },
|
|
805
|
-
{ isoCode: "SG", name: "Singapore", currency: { isoCode: "SGD", symbol: "S$" }, market: null },
|
|
806
|
-
{ isoCode: "GB", name: "United Kingdom", currency: { isoCode: "GBP", symbol: "£" }, market: null }
|
|
807
|
-
],
|
|
808
|
-
language: { isoCode: "en", name: "English" },
|
|
809
|
-
availableLanguages: [
|
|
810
|
-
{ isoCode: "en", name: "English" },
|
|
811
|
-
{ isoCode: "th", name: "ไทย" },
|
|
812
|
-
{ isoCode: "ja", name: "日本語" }
|
|
813
|
-
]
|
|
814
|
-
},
|
|
815
|
-
// ── Shopify-parity extensions, served offline from the fixtures above so
|
|
816
|
-
// themes built against `useData()` behave the same in mock + live. ──
|
|
817
|
-
shop,
|
|
818
|
-
menu: (handle) => menus.get(handle) ?? null,
|
|
819
|
-
fetchMenu: async (handle) => menus.get(handle) ?? null,
|
|
820
|
-
listMenus: async () => Array.from(menus.values()).map((m) => ({
|
|
821
|
-
handle: m.handle,
|
|
822
|
-
title: m.title,
|
|
823
|
-
itemsCount: m.items.length
|
|
824
|
-
})),
|
|
825
|
-
pixels: async () => [],
|
|
826
|
-
locations: async () => [],
|
|
827
|
-
search: async (query, opts) => {
|
|
828
|
-
const types = opts?.types ?? ["PRODUCT", "PAGE", "ARTICLE"];
|
|
829
|
-
const products = types.includes("PRODUCT") ? match(query) : [];
|
|
830
|
-
const arts = types.includes("ARTICLE") ? articles.filter((a) => a.title.toLowerCase().includes(query.trim().toLowerCase())) : [];
|
|
831
|
-
return { totalCount: products.length + arts.length, products, pages: [], articles: arts, filters: [], pageInfo: EMPTY_PI };
|
|
832
|
-
},
|
|
833
|
-
predictiveSearch: async (query, opts) => {
|
|
834
|
-
const products = match(query).slice(0, opts?.limit ?? 4);
|
|
835
|
-
return {
|
|
836
|
-
queries: products.slice(0, 3).map((p) => p.title),
|
|
837
|
-
products,
|
|
838
|
-
collections: collections.filter((c) => c.title.toLowerCase().includes(query.trim().toLowerCase())).slice(0, 3).map((c) => ({ handle: c.handle, title: c.title, image: null })),
|
|
839
|
-
pages: [],
|
|
840
|
-
articles: []
|
|
841
|
-
};
|
|
842
|
-
},
|
|
843
|
-
collectionProducts: async (handle, opts) => {
|
|
844
|
-
const c = collections.find((x) => x.handle === handle);
|
|
845
|
-
return { products: (c?.products ?? []).slice(0, opts?.first ?? 24), filters: [], pageInfo: EMPTY_PI };
|
|
846
|
-
},
|
|
847
|
-
productRecommendations: async (productId) => allProducts.filter((p) => p.id !== productId).slice(0, 4),
|
|
848
|
-
blogByHandle: async (handle) => handle === blog.handle ? blog : null,
|
|
849
|
-
articleByHandle: async (blogHandle, articleHandle) => blogHandle === blog.handle ? articles.find((a) => a.handle === articleHandle) ?? null : null,
|
|
850
|
-
metaobject: async () => null,
|
|
851
|
-
metaobjects: async () => [],
|
|
852
|
-
customer: {
|
|
853
|
-
login: async () => ({ token: null, expiresAt: null, errors: ["Customer accounts need live data (mock mode)."] }),
|
|
854
|
-
logout: async () => {
|
|
855
|
-
},
|
|
856
|
-
renew: async () => ({ token: null, expiresAt: null, errors: ["Customer accounts need live data (mock mode)."] }),
|
|
857
|
-
register: async () => ({ ok: false, errors: ["Customer accounts need live data (mock mode)."] }),
|
|
858
|
-
get: async () => null,
|
|
859
|
-
orders: async () => [],
|
|
860
|
-
orderByLookup: async () => null,
|
|
861
|
-
createAddress: async () => ({ ok: false, errors: ["mock mode"] }),
|
|
862
|
-
updateAddress: async () => ({ ok: false, errors: ["mock mode"] }),
|
|
863
|
-
deleteAddress: async () => ({ ok: false, errors: ["mock mode"] }),
|
|
864
|
-
setDefaultAddress: async () => ({ ok: false, errors: ["mock mode"] })
|
|
865
|
-
}
|
|
866
|
-
};
|
|
867
|
-
}
|
|
868
|
-
const PRODUCT_CARD_FIELDS = (
|
|
869
|
-
/* GraphQL */
|
|
870
|
-
`
|
|
871
|
-
fragment ProductCardBoot on Product {
|
|
872
|
-
id
|
|
873
|
-
handle
|
|
874
|
-
title
|
|
875
|
-
availableForSale
|
|
876
|
-
vendor
|
|
877
|
-
productType
|
|
878
|
-
tags
|
|
879
|
-
featuredImage { url altText }
|
|
880
|
-
priceRange { minVariantPrice { amount currencyCode } }
|
|
881
|
-
compareAtPriceRange { maxVariantPrice { amount currencyCode } }
|
|
882
|
-
firstAvailableVariant { id }
|
|
883
|
-
}
|
|
884
|
-
`
|
|
885
|
-
);
|
|
886
|
-
const COLLECTIONS_QUERY = (
|
|
887
|
-
/* GraphQL */
|
|
888
|
-
`
|
|
889
|
-
${BOOTSTRAP_SHOP_MENU_FRAGMENTS}
|
|
890
|
-
${PRODUCT_CARD_FIELDS}
|
|
891
|
-
query NovaBootstrap($first: Int!, $productFirst: Int!, $productsTop: Int!, $pageHandle: String) {
|
|
892
|
-
${BOOTSTRAP_SHOP_MENU}
|
|
893
|
-
page(handle: $pageHandle) {
|
|
894
|
-
handle
|
|
895
|
-
title
|
|
896
|
-
body
|
|
897
|
-
bodySummary
|
|
898
|
-
author
|
|
899
|
-
publishedAt
|
|
900
|
-
updatedAt
|
|
901
|
-
}
|
|
902
|
-
collections(first: $first) {
|
|
903
|
-
edges {
|
|
904
|
-
node {
|
|
905
|
-
id
|
|
906
|
-
handle
|
|
907
|
-
title
|
|
908
|
-
image { url altText }
|
|
909
|
-
products(first: $productFirst) {
|
|
910
|
-
edges {
|
|
911
|
-
node { ...ProductCardBoot }
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
products(first: $productsTop) {
|
|
918
|
-
edges {
|
|
919
|
-
node { ...ProductCardBoot }
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
localization {
|
|
923
|
-
country {
|
|
924
|
-
isoCode
|
|
925
|
-
name
|
|
926
|
-
currency { isoCode name symbol }
|
|
927
|
-
market { id handle name }
|
|
928
|
-
}
|
|
929
|
-
availableCountries {
|
|
930
|
-
isoCode
|
|
931
|
-
name
|
|
932
|
-
currency { isoCode name symbol }
|
|
933
|
-
market { id handle name }
|
|
934
|
-
}
|
|
935
|
-
language { isoCode name }
|
|
936
|
-
availableLanguages { isoCode name }
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
`
|
|
940
|
-
);
|
|
941
|
-
const SAFE_COLLECTIONS_QUERY = (
|
|
942
|
-
/* GraphQL */
|
|
943
|
-
`
|
|
944
|
-
${BOOTSTRAP_SHOP_MENU_FRAGMENTS}
|
|
945
|
-
${PRODUCT_CARD_FIELDS}
|
|
946
|
-
query NovaBootstrapSafe($first: Int!, $productsTop: Int!, $pageHandle: String) {
|
|
947
|
-
${BOOTSTRAP_SHOP_MENU}
|
|
948
|
-
page(handle: $pageHandle) {
|
|
949
|
-
handle
|
|
950
|
-
title
|
|
951
|
-
body
|
|
952
|
-
bodySummary
|
|
953
|
-
author
|
|
954
|
-
publishedAt
|
|
955
|
-
updatedAt
|
|
956
|
-
}
|
|
957
|
-
collections(first: $first) {
|
|
958
|
-
edges {
|
|
959
|
-
node {
|
|
960
|
-
id
|
|
961
|
-
handle
|
|
962
|
-
title
|
|
963
|
-
image { url altText }
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
products(first: $productsTop) {
|
|
968
|
-
edges {
|
|
969
|
-
node { ...ProductCardBoot }
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
`
|
|
974
|
-
);
|
|
975
|
-
function normalizeImage$1(img2) {
|
|
976
|
-
if (!img2 || !img2.url) return null;
|
|
977
|
-
return {
|
|
978
|
-
url: img2.url,
|
|
979
|
-
...img2.altText ? { altText: img2.altText } : {}
|
|
980
|
-
};
|
|
981
|
-
}
|
|
982
|
-
function normalizeProduct(p) {
|
|
983
|
-
const price = p.priceRange?.minVariantPrice ?? { amount: "0", currencyCode: "USD" };
|
|
984
|
-
const compareAt = p.compareAtPriceRange?.maxVariantPrice;
|
|
985
|
-
const onSale = compareAt && Number(compareAt.amount) > Number(price.amount);
|
|
986
|
-
return {
|
|
987
|
-
id: p.id,
|
|
988
|
-
handle: p.handle,
|
|
989
|
-
title: p.title,
|
|
990
|
-
price,
|
|
991
|
-
featuredImage: normalizeImage$1(p.featuredImage),
|
|
992
|
-
...onSale ? { compareAtPrice: compareAt } : {},
|
|
993
|
-
...p.vendor ? { vendor: p.vendor } : {},
|
|
994
|
-
...p.productType ? { productType: p.productType } : {},
|
|
995
|
-
...p.tags?.length ? { tags: p.tags } : {},
|
|
996
|
-
...p.firstAvailableVariant?.id ? { variantId: p.firstAvailableVariant.id } : {},
|
|
997
|
-
...typeof p.availableForSale === "boolean" ? { availableForSale: p.availableForSale } : {}
|
|
998
|
-
};
|
|
999
|
-
}
|
|
1000
|
-
function normalizeCollection(c) {
|
|
1001
|
-
return {
|
|
1002
|
-
handle: c.handle,
|
|
1003
|
-
title: c.title,
|
|
1004
|
-
image: normalizeImage$1(c.image),
|
|
1005
|
-
// Nested products are optional — the reduced bootstrap (used when a cell
|
|
1006
|
-
// errors on the nested products field) omits them.
|
|
1007
|
-
products: (c.products?.edges ?? []).map((e) => normalizeProduct(e.node))
|
|
1008
|
-
};
|
|
1009
|
-
}
|
|
1010
|
-
const PRODUCT_QUERY = (
|
|
1011
|
-
/* GraphQL */
|
|
1012
|
-
`
|
|
1013
|
-
query NovaProduct($handle: String!, $identifiers: [MetafieldIdentifier!]!) {
|
|
1014
|
-
product(handle: $handle) {
|
|
1015
|
-
id
|
|
1016
|
-
handle
|
|
1017
|
-
title
|
|
1018
|
-
description
|
|
1019
|
-
vendor
|
|
1020
|
-
productType
|
|
1021
|
-
tags
|
|
1022
|
-
availableForSale
|
|
1023
|
-
featuredImage { url altText }
|
|
1024
|
-
images(first: 20) { nodes { url altText } }
|
|
1025
|
-
priceRange { minVariantPrice { amount currencyCode } }
|
|
1026
|
-
compareAtPriceRange { maxVariantPrice { amount currencyCode } }
|
|
1027
|
-
options(first: 10) { name values }
|
|
1028
|
-
seo { title description }
|
|
1029
|
-
variants(first: 50) {
|
|
1030
|
-
nodes {
|
|
1031
|
-
id
|
|
1032
|
-
title
|
|
1033
|
-
availableForSale
|
|
1034
|
-
sku
|
|
1035
|
-
price { amount currencyCode }
|
|
1036
|
-
compareAtPrice { amount currencyCode }
|
|
1037
|
-
image { url altText }
|
|
1038
|
-
selectedOptions { name value }
|
|
1039
|
-
unitPrice { amount currencyCode }
|
|
1040
|
-
unitPriceMeasurement { measuredType quantityValue quantityUnit referenceValue referenceUnit }
|
|
1041
|
-
quantityRule { minimum maximum increment }
|
|
1042
|
-
storeAvailability(first: 5) {
|
|
1043
|
-
nodes { available quantityAvailable pickUpTime location { id name } }
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
sellingPlanGroups(first: 5) {
|
|
1048
|
-
nodes {
|
|
1049
|
-
name
|
|
1050
|
-
appName
|
|
1051
|
-
options { name values }
|
|
1052
|
-
sellingPlans(first: 10) {
|
|
1053
|
-
nodes { id name description recurringDeliveries options { name value } }
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
metafields(identifiers: $identifiers) { namespace key value }
|
|
1058
|
-
firstAvailableVariant { id }
|
|
1059
|
-
}
|
|
1060
|
-
}
|
|
1061
|
-
`
|
|
1062
|
-
);
|
|
1063
|
-
function normalizeProductDetail(p) {
|
|
1064
|
-
const base = normalizeProduct(p);
|
|
1065
|
-
const variants = (p.variants?.nodes ?? []).map((v) => ({
|
|
1066
|
-
id: v.id,
|
|
1067
|
-
title: v.title,
|
|
1068
|
-
price: v.price ?? base.price,
|
|
1069
|
-
...v.compareAtPrice && Number(v.compareAtPrice.amount) > Number((v.price ?? base.price).amount) ? { compareAtPrice: v.compareAtPrice } : {},
|
|
1070
|
-
availableForSale: v.availableForSale,
|
|
1071
|
-
...v.sku ? { sku: v.sku } : {},
|
|
1072
|
-
...v.selectedOptions?.length ? { selectedOptions: v.selectedOptions } : {},
|
|
1073
|
-
image: normalizeImage$1(v.image),
|
|
1074
|
-
...v.unitPrice ? { unitPrice: v.unitPrice } : {},
|
|
1075
|
-
...v.unitPriceMeasurement ? { unitPriceMeasurement: v.unitPriceMeasurement } : {},
|
|
1076
|
-
...v.quantityRule ? { quantityRule: v.quantityRule } : {},
|
|
1077
|
-
...v.storeAvailability?.nodes?.length ? {
|
|
1078
|
-
storeAvailability: v.storeAvailability.nodes.map((s) => ({
|
|
1079
|
-
available: s.available,
|
|
1080
|
-
quantityAvailable: s.quantityAvailable,
|
|
1081
|
-
pickUpTime: s.pickUpTime ?? null,
|
|
1082
|
-
location: s.location
|
|
1083
|
-
}))
|
|
1084
|
-
} : {}
|
|
1085
|
-
}));
|
|
1086
|
-
const images = (p.images?.nodes ?? []).map(normalizeImage$1).filter((i) => i !== null);
|
|
1087
|
-
const sellingPlanGroups = (p.sellingPlanGroups?.nodes ?? []).map((g) => ({
|
|
1088
|
-
name: g.name,
|
|
1089
|
-
appName: g.appName ?? null,
|
|
1090
|
-
options: g.options ?? [],
|
|
1091
|
-
sellingPlans: (g.sellingPlans?.nodes ?? []).map((sp) => ({
|
|
1092
|
-
id: sp.id,
|
|
1093
|
-
name: sp.name,
|
|
1094
|
-
description: sp.description ?? null,
|
|
1095
|
-
recurringDeliveries: sp.recurringDeliveries,
|
|
1096
|
-
options: sp.options ?? []
|
|
1097
|
-
}))
|
|
1098
|
-
}));
|
|
1099
|
-
const metafields = {};
|
|
1100
|
-
for (const m of p.metafields ?? []) {
|
|
1101
|
-
if (m) metafields[`${m.namespace}.${m.key}`] = m.value;
|
|
1102
|
-
}
|
|
1103
|
-
return {
|
|
1104
|
-
...base,
|
|
1105
|
-
...p.description ? { description: p.description } : {},
|
|
1106
|
-
...images.length ? { images } : {},
|
|
1107
|
-
...p.options?.length ? { options: p.options } : {},
|
|
1108
|
-
...p.seo ? { seo: { title: p.seo.title ?? null, description: p.seo.description ?? null } } : {},
|
|
1109
|
-
...variants.length ? { variants } : {},
|
|
1110
|
-
...sellingPlanGroups.length ? { sellingPlanGroups } : {},
|
|
1111
|
-
...Object.keys(metafields).length ? { metafields } : {}
|
|
1112
|
-
};
|
|
1113
|
-
}
|
|
1114
|
-
function makeGraphqlRequest(opts) {
|
|
1115
|
-
const maybeFetch = opts.fetcher ?? globalThis.fetch;
|
|
1116
|
-
if (!maybeFetch) {
|
|
1117
|
-
throw new Error("[theme-kit] createLiveData: fetch is not available in this environment");
|
|
1118
|
-
}
|
|
1119
|
-
const f = maybeFetch;
|
|
1120
|
-
const endpoint = opts.endpoint.replace(/\/$/, "");
|
|
1121
|
-
const url = `${endpoint}/api/v1/stores/${encodeURIComponent(opts.storeId)}/graphql`;
|
|
1122
|
-
return async function graphqlRequest(query, variables) {
|
|
1123
|
-
const res = await f(url, {
|
|
1124
|
-
method: "POST",
|
|
1125
|
-
headers: {
|
|
1126
|
-
"content-type": "application/json",
|
|
1127
|
-
...opts.token ? { "x-publishable-key": opts.token } : {},
|
|
1128
|
-
// X-Tanqory-Country triggers backend Market resolution; if the store
|
|
1129
|
-
// has an active Market with this country, Money.currencyCode + amount
|
|
1130
|
-
// come back already-converted in the target currency.
|
|
1131
|
-
...opts.country ? { "x-tanqory-country": opts.country.toUpperCase() } : {}
|
|
1132
|
-
},
|
|
1133
|
-
body: JSON.stringify({ query, variables })
|
|
1134
|
-
});
|
|
1135
|
-
if (!res.ok) {
|
|
1136
|
-
throw new Error(
|
|
1137
|
-
`[theme-kit] GraphQL HTTP ${res.status} from ${url} — ${await res.text().catch(() => "")}`
|
|
1138
|
-
);
|
|
1139
|
-
}
|
|
1140
|
-
const json = await res.json();
|
|
1141
|
-
if (json.errors?.length) {
|
|
1142
|
-
const msg = json.errors.map((e) => e.message).join("; ");
|
|
1143
|
-
if (json.data == null) {
|
|
1144
|
-
throw new Error(`[theme-kit] GraphQL errors: ${msg}`);
|
|
1145
|
-
}
|
|
1146
|
-
if (typeof console !== "undefined") {
|
|
1147
|
-
console.warn(`[theme-kit] GraphQL partial errors (using partial data): ${msg}`);
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
return json.data;
|
|
1151
|
-
};
|
|
1152
|
-
}
|
|
1153
|
-
async function createLiveData(opts) {
|
|
1154
|
-
const graphqlRequest = makeGraphqlRequest(opts);
|
|
1155
|
-
const collectionLimit = opts.prefetch?.collectionLimit ?? 10;
|
|
1156
|
-
const productLimitPerCollection = opts.prefetch?.productLimitPerCollection ?? 10;
|
|
1157
|
-
const topProductLimit = opts.prefetch?.topProductLimit ?? 24;
|
|
1158
|
-
let boot;
|
|
1159
|
-
try {
|
|
1160
|
-
boot = await graphqlRequest(COLLECTIONS_QUERY, {
|
|
1161
|
-
first: collectionLimit,
|
|
1162
|
-
productFirst: productLimitPerCollection,
|
|
1163
|
-
productsTop: topProductLimit,
|
|
1164
|
-
pageHandle: opts.pageHandle ?? null
|
|
1165
|
-
});
|
|
1166
|
-
} catch (err) {
|
|
1167
|
-
if (typeof console !== "undefined") {
|
|
1168
|
-
console.warn(
|
|
1169
|
-
`[theme-kit] full bootstrap failed, retrying without optional fields: ${err.message}`
|
|
1170
|
-
);
|
|
1171
|
-
}
|
|
1172
|
-
boot = await graphqlRequest(SAFE_COLLECTIONS_QUERY, {
|
|
1173
|
-
first: collectionLimit,
|
|
1174
|
-
productsTop: topProductLimit,
|
|
1175
|
-
pageHandle: opts.pageHandle ?? null
|
|
1176
|
-
});
|
|
1177
|
-
}
|
|
1178
|
-
return buildLiveData(boot, graphqlRequest, opts);
|
|
1179
|
-
}
|
|
1180
|
-
function createLiveDataFromSnapshot(snapshot, opts) {
|
|
1181
|
-
const graphqlRequest = makeGraphqlRequest(opts);
|
|
1182
|
-
return buildLiveData(snapshot ?? void 0, graphqlRequest, opts);
|
|
1183
|
-
}
|
|
1184
|
-
function buildLiveData(boot, graphqlRequest, opts) {
|
|
1185
|
-
const productLimitPerCollection = opts.prefetch?.productLimitPerCollection ?? 10;
|
|
1186
|
-
const collections = (boot?.collections.edges ?? []).map(
|
|
1187
|
-
(e) => normalizeCollection(e.node)
|
|
1188
|
-
);
|
|
1189
|
-
const topProducts = (boot?.products.edges ?? []).map(
|
|
1190
|
-
(e) => normalizeProduct(e.node)
|
|
1191
|
-
);
|
|
1192
|
-
if (topProducts.length > 0 && !collections.some((c) => c.handle === "featured")) {
|
|
1193
|
-
collections.unshift({
|
|
1194
|
-
handle: "featured",
|
|
1195
|
-
title: "Featured",
|
|
1196
|
-
image: topProducts[0].featuredImage ?? null,
|
|
1197
|
-
products: topProducts.slice(0, productLimitPerCollection)
|
|
1198
|
-
});
|
|
1199
|
-
}
|
|
1200
|
-
if (topProducts.length > 0 && !collections.some((c) => c.handle === "all")) {
|
|
1201
|
-
collections.push({
|
|
1202
|
-
handle: "all",
|
|
1203
|
-
title: "All products",
|
|
1204
|
-
image: topProducts[0].featuredImage ?? null,
|
|
1205
|
-
products: topProducts
|
|
1206
|
-
});
|
|
1207
|
-
}
|
|
1208
|
-
const data = createMockData(collections);
|
|
1209
|
-
const pageNode = boot?.page;
|
|
1210
|
-
if (pageNode) {
|
|
1211
|
-
const page = {
|
|
1212
|
-
handle: pageNode.handle,
|
|
1213
|
-
title: pageNode.title,
|
|
1214
|
-
body: pageNode.body,
|
|
1215
|
-
...pageNode.bodySummary ? { bodySummary: pageNode.bodySummary } : {},
|
|
1216
|
-
...pageNode.author ? { author: pageNode.author } : {},
|
|
1217
|
-
...pageNode.publishedAt ? { publishedAt: pageNode.publishedAt } : {},
|
|
1218
|
-
...pageNode.updatedAt ? { updatedAt: pageNode.updatedAt } : {}
|
|
1219
|
-
};
|
|
1220
|
-
data.pageByHandle = (handle) => handle === page.handle ? page : null;
|
|
1221
|
-
}
|
|
1222
|
-
const loc = boot?.localization;
|
|
1223
|
-
if (loc) {
|
|
1224
|
-
data.localization = loc.availableCountries.length > 0 ? {
|
|
1225
|
-
country: loc.country,
|
|
1226
|
-
availableCountries: loc.availableCountries,
|
|
1227
|
-
// Published languages come straight from the store (StoreLanguage →
|
|
1228
|
-
// SDL `availableLanguages`); the theme just renders the picker.
|
|
1229
|
-
language: loc.language,
|
|
1230
|
-
availableLanguages: loc.availableLanguages
|
|
1231
|
-
} : null;
|
|
1232
|
-
}
|
|
1233
|
-
data.graphql = graphqlRequest;
|
|
1234
|
-
data.fetchProduct = async (handle, fpOpts) => {
|
|
1235
|
-
try {
|
|
1236
|
-
const res = await graphqlRequest(PRODUCT_QUERY, {
|
|
1237
|
-
handle,
|
|
1238
|
-
identifiers: fpOpts?.metafields ?? []
|
|
1239
|
-
});
|
|
1240
|
-
return res?.product ? normalizeProductDetail(res.product) : null;
|
|
1241
|
-
} catch (err) {
|
|
1242
|
-
if (typeof console !== "undefined") {
|
|
1243
|
-
console.warn(`[theme-kit] fetchProduct(${handle}) failed: ${err.message}`);
|
|
1244
|
-
}
|
|
1245
|
-
return null;
|
|
1246
|
-
}
|
|
1247
|
-
};
|
|
1248
|
-
const toMfMap = (arr) => {
|
|
1249
|
-
const out = {};
|
|
1250
|
-
for (const m of arr ?? []) if (m) out[`${m.namespace}.${m.key}`] = m.value;
|
|
1251
|
-
return out;
|
|
1252
|
-
};
|
|
1253
|
-
data.fetchShopMetafields = async (identifiers) => {
|
|
1254
|
-
if (!identifiers.length) return {};
|
|
1255
|
-
const res = await graphqlRequest(
|
|
1256
|
-
`query NovaShopMetafields($identifiers: [MetafieldIdentifier!]!) {
|
|
1257
|
-
shop { metafields(identifiers: $identifiers) { namespace key value } }
|
|
1258
|
-
}`,
|
|
1259
|
-
{ identifiers }
|
|
1260
|
-
);
|
|
1261
|
-
return toMfMap(res?.shop?.metafields);
|
|
1262
|
-
};
|
|
1263
|
-
data.fetchCollectionMetafields = async (handle, identifiers) => {
|
|
1264
|
-
if (!identifiers.length || !handle) return {};
|
|
1265
|
-
const res = await graphqlRequest(
|
|
1266
|
-
`query NovaCollectionMetafields($handle: String!, $identifiers: [MetafieldIdentifier!]!) {
|
|
1267
|
-
collection(handle: $handle) { metafields(identifiers: $identifiers) { namespace key value } }
|
|
1268
|
-
}`,
|
|
1269
|
-
{ handle, identifiers }
|
|
1270
|
-
);
|
|
1271
|
-
return toMfMap(res?.collection?.metafields);
|
|
1272
|
-
};
|
|
1273
|
-
const { shop, menus } = readBootstrapShopMenu(boot);
|
|
1274
|
-
data.shop = shop;
|
|
1275
|
-
data.menu = (handle) => menus.get(handle) ?? null;
|
|
1276
|
-
Object.assign(
|
|
1277
|
-
data,
|
|
1278
|
-
createStorefrontMethods(graphqlRequest, { normalizeProduct }, menus)
|
|
1279
|
-
);
|
|
1280
|
-
data.listMenus = async () => {
|
|
1281
|
-
const res = await graphqlRequest(`query NovaMenus { menus { handle title itemsCount } }`);
|
|
1282
|
-
return res?.menus ?? [];
|
|
1283
|
-
};
|
|
1284
|
-
data.pixels = async () => {
|
|
1285
|
-
const res = await graphqlRequest(`query NovaPixels { customPixels { id name provider providerPixelId code } }`);
|
|
1286
|
-
return res?.customPixels ?? [];
|
|
1287
|
-
};
|
|
1288
|
-
data.locations = async () => {
|
|
1289
|
-
const res = await graphqlRequest(
|
|
1290
|
-
`query NovaLocations { locations { id name code address { country address city province postalCode phone } } }`
|
|
1291
|
-
);
|
|
1292
|
-
return res?.locations ?? [];
|
|
1293
|
-
};
|
|
1294
|
-
data.getSnapshot = () => boot ?? null;
|
|
1295
|
-
return data;
|
|
1296
|
-
}
|
|
1297
|
-
const CURRENCY_FORMAT = {
|
|
1298
|
-
THB: { symbol: "฿", decimals: 2 },
|
|
1299
|
-
USD: { symbol: "$", decimals: 2 },
|
|
1300
|
-
EUR: { symbol: "€", decimals: 2 },
|
|
1301
|
-
GBP: { symbol: "£", decimals: 2 },
|
|
1302
|
-
JPY: { symbol: "¥", decimals: 0 },
|
|
1303
|
-
KRW: { symbol: "₩", decimals: 0 },
|
|
1304
|
-
VND: { symbol: "₫", decimals: 0 },
|
|
1305
|
-
IDR: { symbol: "Rp", decimals: 0 },
|
|
1306
|
-
SGD: { symbol: "S$", decimals: 2 },
|
|
1307
|
-
HKD: { symbol: "HK$", decimals: 2 },
|
|
1308
|
-
TWD: { symbol: "NT$", decimals: 2 },
|
|
1309
|
-
AUD: { symbol: "A$", decimals: 2 },
|
|
1310
|
-
NZD: { symbol: "NZ$", decimals: 2 },
|
|
1311
|
-
CAD: { symbol: "CA$", decimals: 2 },
|
|
1312
|
-
CNY: { symbol: "CN¥", decimals: 2 },
|
|
1313
|
-
INR: { symbol: "₹", decimals: 2 },
|
|
1314
|
-
MYR: { symbol: "RM", decimals: 2 },
|
|
1315
|
-
PHP: { symbol: "₱", decimals: 2 }
|
|
1316
|
-
};
|
|
1317
|
-
function formatMoney(money2) {
|
|
1318
|
-
const n = Number(money2.amount);
|
|
1319
|
-
if (!Number.isFinite(n)) return `${money2.amount} ${money2.currencyCode}`;
|
|
1320
|
-
const info = CURRENCY_FORMAT[money2.currencyCode];
|
|
1321
|
-
const decimals = info?.decimals ?? 2;
|
|
1322
|
-
const [int, frac] = Math.abs(n).toFixed(decimals).split(".");
|
|
1323
|
-
const grouped = int.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
1324
|
-
const num = `${n < 0 ? "-" : ""}${grouped}${frac ? `.${frac}` : ""}`;
|
|
1325
|
-
return info ? `${info.symbol}${num}` : `${num} ${money2.currencyCode}`;
|
|
1326
|
-
}
|
|
1327
|
-
const STORAGE_KEY = "tq-cart-id";
|
|
1328
|
-
const DEFAULT_CURRENCY = "USD";
|
|
1329
|
-
const DEFAULT_VARIANT_TITLE = "Default Title";
|
|
1330
|
-
function money(amount, currencyCode) {
|
|
1331
|
-
return { amount: amount.toFixed(2), currencyCode };
|
|
1332
|
-
}
|
|
1333
|
-
function multiply(price, qty) {
|
|
1334
|
-
return money(Number(price.amount) * qty, price.currencyCode);
|
|
1335
|
-
}
|
|
1336
|
-
function sumLines(lines) {
|
|
1337
|
-
const currencyCode = lines[0]?.lineSubtotal.currencyCode ?? DEFAULT_CURRENCY;
|
|
1338
|
-
const total = lines.reduce((acc, l) => acc + Number(l.lineSubtotal.amount), 0);
|
|
1339
|
-
return money(total, currencyCode);
|
|
1340
|
-
}
|
|
1341
|
-
function countQty(lines) {
|
|
1342
|
-
return lines.reduce((acc, l) => acc + l.quantity, 0);
|
|
1343
|
-
}
|
|
1344
|
-
const CART_FRAGMENT = (
|
|
1345
|
-
/* GraphQL */
|
|
1346
|
-
`
|
|
1347
|
-
fragment CartFields on Cart {
|
|
1348
|
-
id
|
|
1349
|
-
checkoutUrl
|
|
1350
|
-
totalQuantity
|
|
1351
|
-
cost {
|
|
1352
|
-
subtotalAmount { amount currencyCode }
|
|
1353
|
-
totalAmount { amount currencyCode }
|
|
1354
|
-
}
|
|
1355
|
-
discountCodes { code applicable }
|
|
1356
|
-
discountAllocations {
|
|
1357
|
-
... on CartCodeDiscountAllocation { code discountedAmount { amount currencyCode } }
|
|
1358
|
-
... on CartAutomaticDiscountAllocation { title discountedAmount { amount currencyCode } }
|
|
1359
|
-
... on CartCustomDiscountAllocation { title discountedAmount { amount currencyCode } }
|
|
1360
|
-
}
|
|
1361
|
-
appliedGiftCards {
|
|
1362
|
-
id
|
|
1363
|
-
lastCharacters
|
|
1364
|
-
amountUsed { amount currencyCode }
|
|
1365
|
-
balance { amount currencyCode }
|
|
1366
|
-
}
|
|
1367
|
-
lines(first: 100) {
|
|
1368
|
-
nodes {
|
|
1369
|
-
id
|
|
1370
|
-
quantity
|
|
1371
|
-
cost {
|
|
1372
|
-
subtotalAmount { amount currencyCode }
|
|
1373
|
-
amountPerQuantity { amount currencyCode }
|
|
1374
|
-
}
|
|
1375
|
-
merchandise {
|
|
1376
|
-
... on Variant {
|
|
1377
|
-
id
|
|
1378
|
-
title
|
|
1379
|
-
image { url altText }
|
|
1380
|
-
price { amount currencyCode }
|
|
1381
|
-
product { handle title featuredImage { url altText } }
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
}
|
|
1385
|
-
}
|
|
1386
|
-
}
|
|
1387
|
-
`
|
|
1388
|
-
);
|
|
1389
|
-
const CART_QUERY = (
|
|
1390
|
-
/* GraphQL */
|
|
1391
|
-
`
|
|
1392
|
-
query Cart($id: ID!) { cart(id: $id) { ...CartFields } }
|
|
1393
|
-
${CART_FRAGMENT}
|
|
1394
|
-
`
|
|
1395
|
-
);
|
|
1396
|
-
const CART_CREATE = (
|
|
1397
|
-
/* GraphQL */
|
|
1398
|
-
`
|
|
1399
|
-
mutation CartCreate($lines: [CartLineInput!]!) {
|
|
1400
|
-
cartCreate(input: { lines: $lines }) { cart { ...CartFields } userErrors { message } }
|
|
1401
|
-
}
|
|
1402
|
-
${CART_FRAGMENT}
|
|
1403
|
-
`
|
|
1404
|
-
);
|
|
1405
|
-
const CART_LINES_ADD = (
|
|
1406
|
-
/* GraphQL */
|
|
1407
|
-
`
|
|
1408
|
-
mutation CartLinesAdd($cartId: ID!, $lines: [CartLineInput!]!) {
|
|
1409
|
-
cartLinesAdd(cartId: $cartId, lines: $lines) { cart { ...CartFields } userErrors { message } }
|
|
1410
|
-
}
|
|
1411
|
-
${CART_FRAGMENT}
|
|
1412
|
-
`
|
|
1413
|
-
);
|
|
1414
|
-
const CART_LINES_UPDATE = (
|
|
1415
|
-
/* GraphQL */
|
|
1416
|
-
`
|
|
1417
|
-
mutation CartLinesUpdate($cartId: ID!, $lines: [CartLineUpdateInput!]!) {
|
|
1418
|
-
cartLinesUpdate(cartId: $cartId, lines: $lines) { cart { ...CartFields } userErrors { message } }
|
|
1419
|
-
}
|
|
1420
|
-
${CART_FRAGMENT}
|
|
1421
|
-
`
|
|
1422
|
-
);
|
|
1423
|
-
const CART_LINES_REMOVE = (
|
|
1424
|
-
/* GraphQL */
|
|
1425
|
-
`
|
|
1426
|
-
mutation CartLinesRemove($cartId: ID!, $lineIds: [ID!]!) {
|
|
1427
|
-
cartLinesRemove(cartId: $cartId, lineIds: $lineIds) { cart { ...CartFields } userErrors { message } }
|
|
1428
|
-
}
|
|
1429
|
-
${CART_FRAGMENT}
|
|
1430
|
-
`
|
|
1431
|
-
);
|
|
1432
|
-
const CART_DISCOUNT_CODES = (
|
|
1433
|
-
/* GraphQL */
|
|
1434
|
-
`
|
|
1435
|
-
mutation CartDiscountCodesUpdate($cartId: ID!, $discountCodes: [String!]) {
|
|
1436
|
-
cartDiscountCodesUpdate(cartId: $cartId, discountCodes: $discountCodes) {
|
|
1437
|
-
cart { ...CartFields } userErrors { message }
|
|
1438
|
-
}
|
|
1439
|
-
}
|
|
1440
|
-
${CART_FRAGMENT}
|
|
1441
|
-
`
|
|
1442
|
-
);
|
|
1443
|
-
const CART_GIFTCARD_UPDATE = (
|
|
1444
|
-
/* GraphQL */
|
|
1445
|
-
`
|
|
1446
|
-
mutation CartGiftCardCodesUpdate($cartId: ID!, $giftCardCodes: [String!]!) {
|
|
1447
|
-
cartGiftCardCodesUpdate(cartId: $cartId, giftCardCodes: $giftCardCodes) {
|
|
1448
|
-
cart { ...CartFields } userErrors { message }
|
|
1449
|
-
}
|
|
1450
|
-
}
|
|
1451
|
-
${CART_FRAGMENT}
|
|
1452
|
-
`
|
|
1453
|
-
);
|
|
1454
|
-
const CART_GIFTCARD_REMOVE = (
|
|
1455
|
-
/* GraphQL */
|
|
1456
|
-
`
|
|
1457
|
-
mutation CartGiftCardCodesRemove($cartId: ID!, $appliedGiftCardIds: [ID!]!) {
|
|
1458
|
-
cartGiftCardCodesRemove(cartId: $cartId, appliedGiftCardIds: $appliedGiftCardIds) {
|
|
1459
|
-
cart { ...CartFields } userErrors { message }
|
|
1460
|
-
}
|
|
1461
|
-
}
|
|
1462
|
-
${CART_FRAGMENT}
|
|
1463
|
-
`
|
|
1464
|
-
);
|
|
1465
|
-
function normalizeImage(img2) {
|
|
1466
|
-
if (!img2 || !img2.url) return null;
|
|
1467
|
-
return { url: img2.url, ...img2.altText ? { altText: img2.altText } : {} };
|
|
1468
|
-
}
|
|
1469
|
-
function normalizeCart(c) {
|
|
1470
|
-
const lines = c.lines.nodes.map((n) => {
|
|
1471
|
-
const m = n.merchandise;
|
|
1472
|
-
const variantTitle = m.title && m.title !== DEFAULT_VARIANT_TITLE ? m.title : void 0;
|
|
1473
|
-
return {
|
|
1474
|
-
id: n.id,
|
|
1475
|
-
variantId: m.id,
|
|
1476
|
-
quantity: n.quantity,
|
|
1477
|
-
title: m.product?.title ?? m.title ?? "Item",
|
|
1478
|
-
...variantTitle ? { variantTitle } : {},
|
|
1479
|
-
image: normalizeImage(m.image) ?? normalizeImage(m.product?.featuredImage),
|
|
1480
|
-
price: n.cost.amountPerQuantity ?? m.price,
|
|
1481
|
-
lineSubtotal: n.cost.subtotalAmount ?? multiply(m.price, n.quantity),
|
|
1482
|
-
...m.product?.handle ? { productHandle: m.product.handle } : {}
|
|
1483
|
-
};
|
|
1484
|
-
});
|
|
1485
|
-
const subtotal = c.cost?.subtotalAmount ?? sumLines(lines);
|
|
1486
|
-
const allocations = c.discountAllocations ?? [];
|
|
1487
|
-
const discountAmount = allocations.length > 0 ? money(
|
|
1488
|
-
allocations.reduce((acc, a) => acc + Number(a.discountedAmount.amount), 0),
|
|
1489
|
-
allocations[0].discountedAmount.currencyCode
|
|
1490
|
-
) : null;
|
|
1491
|
-
return {
|
|
1492
|
-
id: c.id,
|
|
1493
|
-
lines,
|
|
1494
|
-
subtotal,
|
|
1495
|
-
total: c.cost?.totalAmount ?? subtotal,
|
|
1496
|
-
totalQuantity: c.totalQuantity ?? countQty(lines),
|
|
1497
|
-
checkoutUrl: c.checkoutUrl ?? null,
|
|
1498
|
-
discountCodes: (c.discountCodes ?? []).map((d) => ({ code: d.code, applicable: d.applicable })),
|
|
1499
|
-
discountAmount,
|
|
1500
|
-
appliedGiftCards: (c.appliedGiftCards ?? []).map((g) => ({
|
|
1501
|
-
id: g.id,
|
|
1502
|
-
lastCharacters: g.lastCharacters,
|
|
1503
|
-
amountUsed: g.amountUsed,
|
|
1504
|
-
balance: g.balance
|
|
1505
|
-
}))
|
|
1506
|
-
};
|
|
1507
|
-
}
|
|
1508
|
-
const EMPTY = {
|
|
1509
|
-
id: null,
|
|
1510
|
-
lines: [],
|
|
1511
|
-
subtotal: { amount: "0.00", currencyCode: DEFAULT_CURRENCY },
|
|
1512
|
-
total: { amount: "0.00", currencyCode: DEFAULT_CURRENCY },
|
|
1513
|
-
totalQuantity: 0,
|
|
1514
|
-
checkoutUrl: null,
|
|
1515
|
-
discountCodes: [],
|
|
1516
|
-
discountAmount: null,
|
|
1517
|
-
appliedGiftCards: [],
|
|
1518
|
-
loading: false,
|
|
1519
|
-
ready: false,
|
|
1520
|
-
error: null
|
|
1521
|
-
};
|
|
1522
|
-
const CartContext = createContext(null);
|
|
1523
|
-
function readStoredCartId() {
|
|
1524
|
-
if (typeof window === "undefined") return null;
|
|
1525
|
-
try {
|
|
1526
|
-
return window.localStorage.getItem(STORAGE_KEY);
|
|
1527
|
-
} catch {
|
|
1528
|
-
return null;
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
function writeStoredCartId(id) {
|
|
1532
|
-
if (typeof window === "undefined") return;
|
|
1533
|
-
try {
|
|
1534
|
-
if (id) window.localStorage.setItem(STORAGE_KEY, id);
|
|
1535
|
-
else window.localStorage.removeItem(STORAGE_KEY);
|
|
1536
|
-
} catch {
|
|
1537
|
-
}
|
|
1538
|
-
}
|
|
1539
|
-
function CartProvider({ children }) {
|
|
1540
|
-
const data = useData();
|
|
1541
|
-
const gql = data.graphql;
|
|
1542
|
-
const isLive = typeof gql === "function";
|
|
1543
|
-
const [state, setState] = useState(EMPTY);
|
|
1544
|
-
const ref = useRef(state);
|
|
1545
|
-
ref.current = state;
|
|
1546
|
-
const applyCart = useCallback((c) => {
|
|
1547
|
-
const next = normalizeCart(c);
|
|
1548
|
-
writeStoredCartId(next.id);
|
|
1549
|
-
setState((s) => ({ ...s, ...next, loading: false, error: null }));
|
|
1550
|
-
}, []);
|
|
1551
|
-
const setLocal = useCallback((lines) => {
|
|
1552
|
-
setState((s) => ({
|
|
1553
|
-
...s,
|
|
1554
|
-
lines,
|
|
1555
|
-
subtotal: sumLines(lines),
|
|
1556
|
-
total: sumLines(lines),
|
|
1557
|
-
totalQuantity: countQty(lines),
|
|
1558
|
-
loading: false,
|
|
1559
|
-
error: null
|
|
1560
|
-
}));
|
|
1561
|
-
}, []);
|
|
1562
|
-
useEffect(() => {
|
|
1563
|
-
let cancelled = false;
|
|
1564
|
-
async function hydrate() {
|
|
1565
|
-
const id = readStoredCartId();
|
|
1566
|
-
if (isLive && gql && id) {
|
|
1567
|
-
try {
|
|
1568
|
-
const res = await gql(CART_QUERY, { id });
|
|
1569
|
-
if (cancelled) return;
|
|
1570
|
-
if (res.cart) {
|
|
1571
|
-
const next = normalizeCart(res.cart);
|
|
1572
|
-
setState((s) => ({ ...s, ...next, ready: true }));
|
|
1573
|
-
return;
|
|
1574
|
-
}
|
|
1575
|
-
writeStoredCartId(null);
|
|
1576
|
-
} catch {
|
|
1577
|
-
}
|
|
1578
|
-
}
|
|
1579
|
-
if (!cancelled) setState((s) => ({ ...s, ready: true }));
|
|
1580
|
-
}
|
|
1581
|
-
void hydrate();
|
|
1582
|
-
return () => {
|
|
1583
|
-
cancelled = true;
|
|
1584
|
-
};
|
|
1585
|
-
}, [isLive, gql]);
|
|
1586
|
-
const add = useCallback(
|
|
1587
|
-
async (input) => {
|
|
1588
|
-
const quantity = input.quantity ?? 1;
|
|
1589
|
-
if (quantity < 1) return;
|
|
1590
|
-
if (isLive && gql) {
|
|
1591
|
-
setState((s) => ({ ...s, loading: true }));
|
|
1592
|
-
try {
|
|
1593
|
-
const lines2 = [{ merchandiseId: input.variantId, quantity }];
|
|
1594
|
-
if (!ref.current.id) {
|
|
1595
|
-
const res = await gql(CART_CREATE, { lines: lines2 });
|
|
1596
|
-
const payload = res.cartCreate;
|
|
1597
|
-
if (payload.cart) applyCart(payload.cart);
|
|
1598
|
-
else throw new Error(payload.userErrors[0]?.message ?? "Could not create cart");
|
|
1599
|
-
} else {
|
|
1600
|
-
const res = await gql(CART_LINES_ADD, {
|
|
1601
|
-
cartId: ref.current.id,
|
|
1602
|
-
lines: lines2
|
|
1603
|
-
});
|
|
1604
|
-
const payload = res.cartLinesAdd;
|
|
1605
|
-
if (payload.cart) applyCart(payload.cart);
|
|
1606
|
-
else throw new Error(payload.userErrors[0]?.message ?? "Could not add to cart");
|
|
1607
|
-
}
|
|
1608
|
-
} catch (e) {
|
|
1609
|
-
setState((s) => ({ ...s, loading: false, error: e.message }));
|
|
1610
|
-
}
|
|
1611
|
-
return;
|
|
1612
|
-
}
|
|
1613
|
-
const p = input.product;
|
|
1614
|
-
if (!p) return;
|
|
1615
|
-
const existing = ref.current.lines.find((l) => l.variantId === input.variantId);
|
|
1616
|
-
let lines;
|
|
1617
|
-
if (existing) {
|
|
1618
|
-
lines = ref.current.lines.map(
|
|
1619
|
-
(l) => l.variantId === input.variantId ? { ...l, quantity: l.quantity + quantity, lineSubtotal: multiply(l.price, l.quantity + quantity) } : l
|
|
1620
|
-
);
|
|
1621
|
-
} else {
|
|
1622
|
-
lines = [
|
|
1623
|
-
...ref.current.lines,
|
|
1624
|
-
{
|
|
1625
|
-
id: `mock-${input.variantId}`,
|
|
1626
|
-
variantId: input.variantId,
|
|
1627
|
-
quantity,
|
|
1628
|
-
title: p.title,
|
|
1629
|
-
...p.variantTitle ? { variantTitle: p.variantTitle } : {},
|
|
1630
|
-
image: p.image ?? null,
|
|
1631
|
-
price: p.price,
|
|
1632
|
-
lineSubtotal: multiply(p.price, quantity),
|
|
1633
|
-
...p.handle ? { productHandle: p.handle } : {}
|
|
1634
|
-
}
|
|
1635
|
-
];
|
|
1636
|
-
}
|
|
1637
|
-
setLocal(lines);
|
|
1638
|
-
},
|
|
1639
|
-
[isLive, gql, applyCart, setLocal]
|
|
1640
|
-
);
|
|
1641
|
-
const remove = useCallback(
|
|
1642
|
-
async (lineId) => {
|
|
1643
|
-
const optimistic = ref.current.lines.filter((l) => l.id !== lineId);
|
|
1644
|
-
setLocal(optimistic);
|
|
1645
|
-
if (isLive && gql && ref.current.id) {
|
|
1646
|
-
try {
|
|
1647
|
-
const res = await gql(CART_LINES_REMOVE, {
|
|
1648
|
-
cartId: ref.current.id,
|
|
1649
|
-
lineIds: [lineId]
|
|
1650
|
-
});
|
|
1651
|
-
if (res.cartLinesRemove.cart) applyCart(res.cartLinesRemove.cart);
|
|
1652
|
-
} catch (e) {
|
|
1653
|
-
setState((s) => ({ ...s, error: e.message }));
|
|
1654
|
-
}
|
|
1655
|
-
}
|
|
1656
|
-
},
|
|
1657
|
-
[isLive, gql, applyCart, setLocal]
|
|
1658
|
-
);
|
|
1659
|
-
const updateQuantity = useCallback(
|
|
1660
|
-
async (lineId, quantity) => {
|
|
1661
|
-
if (quantity <= 0) {
|
|
1662
|
-
await remove(lineId);
|
|
1663
|
-
return;
|
|
1664
|
-
}
|
|
1665
|
-
const optimistic = ref.current.lines.map(
|
|
1666
|
-
(l) => l.id === lineId ? { ...l, quantity, lineSubtotal: multiply(l.price, quantity) } : l
|
|
1667
|
-
);
|
|
1668
|
-
setLocal(optimistic);
|
|
1669
|
-
if (isLive && gql && ref.current.id) {
|
|
1670
|
-
try {
|
|
1671
|
-
const res = await gql(CART_LINES_UPDATE, {
|
|
1672
|
-
cartId: ref.current.id,
|
|
1673
|
-
lines: [{ id: lineId, quantity }]
|
|
1674
|
-
});
|
|
1675
|
-
if (res.cartLinesUpdate.cart) applyCart(res.cartLinesUpdate.cart);
|
|
1676
|
-
} catch (e) {
|
|
1677
|
-
setState((s) => ({ ...s, error: e.message }));
|
|
1678
|
-
}
|
|
1679
|
-
}
|
|
1680
|
-
},
|
|
1681
|
-
[isLive, gql, applyCart, setLocal, remove]
|
|
1682
|
-
);
|
|
1683
|
-
const clear = useCallback(async () => {
|
|
1684
|
-
const ids = ref.current.lines.map((l) => l.id);
|
|
1685
|
-
setLocal([]);
|
|
1686
|
-
if (isLive && gql && ref.current.id && ids.length > 0) {
|
|
1687
|
-
try {
|
|
1688
|
-
const res = await gql(CART_LINES_REMOVE, {
|
|
1689
|
-
cartId: ref.current.id,
|
|
1690
|
-
lineIds: ids
|
|
1691
|
-
});
|
|
1692
|
-
if (res.cartLinesRemove.cart) applyCart(res.cartLinesRemove.cart);
|
|
1693
|
-
} catch (e) {
|
|
1694
|
-
setState((s) => ({ ...s, error: e.message }));
|
|
1695
|
-
}
|
|
1696
|
-
}
|
|
1697
|
-
}, [isLive, gql, applyCart, setLocal]);
|
|
1698
|
-
const runCartMutation = useCallback(
|
|
1699
|
-
async (query, field, variables) => {
|
|
1700
|
-
if (!isLive || !gql || !ref.current.id) return;
|
|
1701
|
-
setState((s) => ({ ...s, loading: true }));
|
|
1702
|
-
try {
|
|
1703
|
-
const res = await gql(query, {
|
|
1704
|
-
cartId: ref.current.id,
|
|
1705
|
-
...variables
|
|
1706
|
-
});
|
|
1707
|
-
const payload = res[field];
|
|
1708
|
-
if (payload?.cart) applyCart(payload.cart);
|
|
1709
|
-
else throw new Error(payload?.userErrors[0]?.message ?? "Cart update failed");
|
|
1710
|
-
} catch (e) {
|
|
1711
|
-
setState((s) => ({ ...s, loading: false, error: e.message }));
|
|
1712
|
-
}
|
|
1713
|
-
},
|
|
1714
|
-
[isLive, gql, applyCart]
|
|
1715
|
-
);
|
|
1716
|
-
const applyDiscountCodes = useCallback(
|
|
1717
|
-
(codes) => runCartMutation(CART_DISCOUNT_CODES, "cartDiscountCodesUpdate", { discountCodes: codes }),
|
|
1718
|
-
[runCartMutation]
|
|
1719
|
-
);
|
|
1720
|
-
const applyGiftCardCodes = useCallback(
|
|
1721
|
-
(codes) => runCartMutation(CART_GIFTCARD_UPDATE, "cartGiftCardCodesUpdate", { giftCardCodes: codes }),
|
|
1722
|
-
[runCartMutation]
|
|
1723
|
-
);
|
|
1724
|
-
const removeGiftCards = useCallback(
|
|
1725
|
-
(ids) => runCartMutation(CART_GIFTCARD_REMOVE, "cartGiftCardCodesRemove", { appliedGiftCardIds: ids }),
|
|
1726
|
-
[runCartMutation]
|
|
1727
|
-
);
|
|
1728
|
-
const value = {
|
|
1729
|
-
...state,
|
|
1730
|
-
add,
|
|
1731
|
-
updateQuantity,
|
|
1732
|
-
remove,
|
|
1733
|
-
clear,
|
|
1734
|
-
applyDiscountCodes,
|
|
1735
|
-
applyGiftCardCodes,
|
|
1736
|
-
removeGiftCards
|
|
1737
|
-
};
|
|
1738
|
-
return /* @__PURE__ */ jsx(CartContext.Provider, { value, children });
|
|
1739
|
-
}
|
|
1740
|
-
function useCart() {
|
|
1741
|
-
const ctx = useContext(CartContext);
|
|
1742
|
-
if (!ctx) throw new Error("useCart must be used within <CartProvider>");
|
|
1743
|
-
return ctx;
|
|
1744
|
-
}
|
|
1745
|
-
function defaultsOf(map) {
|
|
1746
|
-
return Object.values(map).map((m) => m.default);
|
|
1747
|
-
}
|
|
1748
|
-
function pickByName(map, name) {
|
|
1749
|
-
const hit = Object.entries(map).find(
|
|
1750
|
-
([key]) => key.endsWith(`/${name}.json`) || key.endsWith(`/${name}.tsx`)
|
|
1751
|
-
);
|
|
1752
|
-
return hit ? hit[1].default : void 0;
|
|
1753
|
-
}
|
|
1754
|
-
function renderStorefrontHTML(opts) {
|
|
1755
|
-
registerSections(defaultsOf(opts.sections));
|
|
1756
|
-
const shells = opts.shell ? defaultsOf(opts.shell) : [];
|
|
1757
|
-
const Shell = shells[0] ?? (({ children }) => /* @__PURE__ */ jsx(Fragment, { children }));
|
|
1758
|
-
const pageDoc = pickByName(opts.pages, opts.page ?? "index") ?? { sections: [] };
|
|
1759
|
-
return renderToString(
|
|
1760
|
-
/* @__PURE__ */ jsx(DataProvider, { value: opts.data, children: /* @__PURE__ */ jsx(ThemeProvider, { settings: opts.settings, locale: opts.locale, children: /* @__PURE__ */ jsx(CartProvider, { children: /* @__PURE__ */ jsx(Shell, { children: /* @__PURE__ */ jsx(SectionTree, { tree: pageDoc.sections }) }) }) }) })
|
|
1761
|
-
);
|
|
1762
|
-
}
|
|
1763
|
-
function renderSectionPreviewHTML(opts, type, settingsOverride) {
|
|
1764
|
-
const defs = defaultsOf(opts.sections);
|
|
1765
|
-
registerSections(defs);
|
|
1766
|
-
const def = defs.find((d) => d.name === type);
|
|
1767
|
-
const presetBlocks = def?.presets?.[0]?.blocks ?? [];
|
|
1768
|
-
const node = {
|
|
1769
|
-
type,
|
|
1770
|
-
id: "preview",
|
|
1771
|
-
settings: settingsOverride ?? {},
|
|
1772
|
-
blocks: presetBlocks.map((b, i) => ({
|
|
1773
|
-
type: b.type,
|
|
1774
|
-
id: `preview-${i}`,
|
|
1775
|
-
settings: b.settings ?? {}
|
|
1776
|
-
}))
|
|
1777
|
-
};
|
|
1778
|
-
return renderToString(
|
|
1779
|
-
/* @__PURE__ */ jsx(DataProvider, { value: opts.data, children: /* @__PURE__ */ jsx(ThemeProvider, { settings: opts.settings, locale: opts.locale, children: /* @__PURE__ */ jsx(CartProvider, { children: /* @__PURE__ */ jsx(SectionTree, { tree: [node] }) }) }) })
|
|
1780
|
-
);
|
|
1781
|
-
}
|
|
1782
|
-
export {
|
|
1783
|
-
CartProvider as C,
|
|
1784
|
-
DataProvider as D,
|
|
1785
|
-
SectionTree as S,
|
|
1786
|
-
ThemeProvider as T,
|
|
1787
|
-
allSections as a,
|
|
1788
|
-
createLiveDataFromSnapshot as b,
|
|
1789
|
-
createLiveData as c,
|
|
1790
|
-
createMockData as d,
|
|
1791
|
-
customerTokenStore as e,
|
|
1792
|
-
formatMoney as f,
|
|
1793
|
-
getSection as g,
|
|
1794
|
-
renderSectionPreviewHTML as h,
|
|
1795
|
-
renderStorefrontHTML as i,
|
|
1796
|
-
useData as j,
|
|
1797
|
-
useSettings as k,
|
|
1798
|
-
useT as l,
|
|
1799
|
-
registerSections as r,
|
|
1800
|
-
useCart as u
|
|
1801
|
-
};
|
|
1802
|
-
//# sourceMappingURL=ssg-BZ5FYTw7.js.map
|