@joy-loyalty/hydrogen 0.2.1 → 0.4.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 +282 -1
- package/dist/hydrogen-cart-port.d.ts +49 -1
- package/dist/hydrogen-cart-port.js +23 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +4 -0
- package/dist/lib/core-loader.d.ts +55 -0
- package/dist/lib/points-calculator-snapshot.d.ts +155 -0
- package/dist/lib/points-calculator-snapshot.js +139 -0
- package/dist/lib/redeem-cart-snapshot.d.ts +51 -0
- package/dist/lib/redeem-cart-snapshot.js +173 -0
- package/dist/lib/submit-form-visibility.d.ts +2 -0
- package/dist/lib/submit-form-visibility.js +15 -0
- package/dist/loyalty-page/compose-home.js +2 -1
- package/dist/orchestrator/handlers/milestone-tier.js +1 -1
- package/dist/points-calculator/JoyPointsCalculator.d.ts +29 -0
- package/dist/points-calculator/JoyPointsCalculator.js +377 -0
- package/dist/points-calculator/fragments.d.ts +7 -0
- package/dist/points-calculator/fragments.js +150 -0
- package/dist/redeem-cart/JoyRedeemCart.d.ts +19 -0
- package/dist/redeem-cart/JoyRedeemCart.js +321 -0
- package/dist/render-adapter.js +4 -0
- package/dist/widget-orchestrator.js +2 -1
- package/package.json +2 -2
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createElement, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { loadWidgetCore } from '../lib/core-loader.js';
|
|
4
|
+
import { WidgetCorePreloadLinks } from '../lib/core-preload-links.js';
|
|
5
|
+
import { ensureClient, useJoyProviderCart, useJoyProviderCartHandlers } from '../lib/JoyProvider.js';
|
|
6
|
+
import { normalizeRedeemCartSnapshot } from '../lib/redeem-cart-snapshot.js';
|
|
7
|
+
import { safeNavigationUrl } from '../lib/safe-navigation-url.js';
|
|
8
|
+
import { DEFAULT_TOKEN_ROUTE, getJoyToken } from '../lib/token.js';
|
|
9
|
+
import { resolveActiveTranslation, resolveContentLocale } from '../render-adapter.js';
|
|
10
|
+
const PLACEHOLDER_MIN_HEIGHT = 96;
|
|
11
|
+
const CART_UPDATE_DEBOUNCE_MS = 50;
|
|
12
|
+
const PLACEHOLDER_RELEASE_FALLBACK_MS = 1500;
|
|
13
|
+
const COPY_MODE_PORT = {
|
|
14
|
+
capabilities: { discountCodes: false, freeGift: false, mergeCodes: false, revalidate: false },
|
|
15
|
+
applyDiscount: () => Promise.reject(new Error('Cart discounts are not supported by this host.')),
|
|
16
|
+
removeDiscount: async () => { },
|
|
17
|
+
addLines: () => Promise.reject(new Error('Adding cart lines is not supported by this host.')),
|
|
18
|
+
redeemGift: () => Promise.reject(new Error('Free gifts are not supported by this host.')),
|
|
19
|
+
getCart: async () => null,
|
|
20
|
+
revalidate: () => { }
|
|
21
|
+
};
|
|
22
|
+
class StageError extends Error {
|
|
23
|
+
stage;
|
|
24
|
+
detail;
|
|
25
|
+
constructor(stage, detail) {
|
|
26
|
+
super(stage);
|
|
27
|
+
this.stage = stage;
|
|
28
|
+
this.detail = detail;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function guarded(stage, run) {
|
|
32
|
+
try {
|
|
33
|
+
return await run();
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
throw new StageError(stage, error);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function resolveCart(cart) {
|
|
40
|
+
try {
|
|
41
|
+
return typeof cart === 'function' ? cart() : cart;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function resolveCustomerKey(initialData) {
|
|
48
|
+
const shopifyId = initialData?.shopifyCustomer?.id;
|
|
49
|
+
if (shopifyId)
|
|
50
|
+
return `s:${shopifyId}`;
|
|
51
|
+
const joyId = initialData?.customer?.id;
|
|
52
|
+
return joyId ? `j:${joyId}` : null;
|
|
53
|
+
}
|
|
54
|
+
function toRedeemCartPort(port) {
|
|
55
|
+
return {
|
|
56
|
+
capabilities: port.capabilities,
|
|
57
|
+
applyDiscount: code => port.applyDiscount(code),
|
|
58
|
+
removeDiscount: code => port.removeDiscount(code),
|
|
59
|
+
addLines: async (lines) => port.addLines(lines),
|
|
60
|
+
redeemGift: input => port.redeemGift(input),
|
|
61
|
+
getCart: async () => port.getCart(),
|
|
62
|
+
revalidate: () => port.revalidate(),
|
|
63
|
+
setDiscountCodes: codes => port.setDiscountCodes(codes)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function createMemberClient(options) {
|
|
67
|
+
const { apiUrl, tokenRoute, locale, shopDomain } = options;
|
|
68
|
+
const client = ensureClient(apiUrl, tokenRoute ?? DEFAULT_TOKEN_ROUTE, locale, shopDomain);
|
|
69
|
+
const withToken = async (run) => {
|
|
70
|
+
const token = await getJoyToken(tokenRoute ? { tokenRoute } : {});
|
|
71
|
+
if (token)
|
|
72
|
+
client.setToken(token);
|
|
73
|
+
return run();
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
redeem: (programId, redeemOptions) => withToken(() => client.redeem(programId, redeemOptions)),
|
|
77
|
+
revokeReward: (rewardId) => withToken(() => client.cancelRedeem(rewardId)),
|
|
78
|
+
getRewards: (params) => withToken(() => client.getRewards(params)),
|
|
79
|
+
checkSegmentPrograms: (queries) => withToken(() => client.checkSegmentPrograms(queries))
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function flattenTranslation(blobTranslation, locale) {
|
|
83
|
+
return { ...resolveActiveTranslation(blobTranslation), ...resolveActiveTranslation(blobTranslation, locale) };
|
|
84
|
+
}
|
|
85
|
+
export function JoyRedeemCart({ apiUrl, shopDomain, tokenRoute, assetUrl, initialData, loginUrl, onNavigate, className, cart, cartFetcher, cartRevalidate, cartRoute, onCartUpdated }) {
|
|
86
|
+
const providerCart = useJoyProviderCart();
|
|
87
|
+
const providerHandlers = useJoyProviderCartHandlers();
|
|
88
|
+
const effectiveCart = cart ?? providerCart;
|
|
89
|
+
const effectiveOnCartUpdated = onCartUpdated ?? providerHandlers.onCartUpdated;
|
|
90
|
+
const cartRef = useRef(effectiveCart);
|
|
91
|
+
cartRef.current = effectiveCart;
|
|
92
|
+
const cartFetcherRef = useRef(cartFetcher);
|
|
93
|
+
cartFetcherRef.current = cartFetcher;
|
|
94
|
+
const cartRevalidateRef = useRef(cartRevalidate);
|
|
95
|
+
cartRevalidateRef.current = cartRevalidate;
|
|
96
|
+
const onCartUpdatedRef = useRef(effectiveOnCartUpdated);
|
|
97
|
+
onCartUpdatedRef.current = effectiveOnCartUpdated;
|
|
98
|
+
const onNavigateRef = useRef(onNavigate);
|
|
99
|
+
onNavigateRef.current = onNavigate;
|
|
100
|
+
const initialDataRef = useRef(initialData);
|
|
101
|
+
initialDataRef.current = initialData;
|
|
102
|
+
const loginUrlRef = useRef(loginUrl);
|
|
103
|
+
loginUrlRef.current = loginUrl;
|
|
104
|
+
const cartRouteRef = useRef(cartRoute);
|
|
105
|
+
cartRouteRef.current = cartRoute;
|
|
106
|
+
const cartPortRef = useRef(null);
|
|
107
|
+
const handleRef = useRef(null);
|
|
108
|
+
const hostRef = useRef(null);
|
|
109
|
+
const updateWarnedRef = useRef(false);
|
|
110
|
+
const [reserved, setReserved] = useState(true);
|
|
111
|
+
const [collapsed, setCollapsed] = useState(false);
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
try {
|
|
114
|
+
cartPortRef.current?.settle();
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
const customerKey = resolveCustomerKey(initialData);
|
|
120
|
+
const locale = initialData?.locale;
|
|
121
|
+
const hasInitialData = Boolean(initialData);
|
|
122
|
+
const isCollapsed = collapsed || !hasInitialData;
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
if (!hasInitialData)
|
|
125
|
+
return;
|
|
126
|
+
setCollapsed(false);
|
|
127
|
+
setReserved(true);
|
|
128
|
+
let disposed = false;
|
|
129
|
+
let handle = null;
|
|
130
|
+
let ownPort = null;
|
|
131
|
+
let releaseObserver = null;
|
|
132
|
+
let releaseTimer;
|
|
133
|
+
let warned = false;
|
|
134
|
+
const warnOnce = (message, detail) => {
|
|
135
|
+
if (warned)
|
|
136
|
+
return;
|
|
137
|
+
warned = true;
|
|
138
|
+
console.warn(`[joy] JoyRedeemCart: ${message}`, ...(detail === undefined ? [] : [detail]));
|
|
139
|
+
};
|
|
140
|
+
const disposeHandle = () => {
|
|
141
|
+
const current = handle;
|
|
142
|
+
handle = null;
|
|
143
|
+
if (handleRef.current === current)
|
|
144
|
+
handleRef.current = null;
|
|
145
|
+
if (!current)
|
|
146
|
+
return;
|
|
147
|
+
try {
|
|
148
|
+
current.dispose();
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
warnOnce('dispose failed', error);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const fail = (message, detail) => {
|
|
155
|
+
if (disposed)
|
|
156
|
+
return;
|
|
157
|
+
disposeHandle();
|
|
158
|
+
warnOnce(message, detail);
|
|
159
|
+
setCollapsed(true);
|
|
160
|
+
};
|
|
161
|
+
const getSnapshot = () => {
|
|
162
|
+
try {
|
|
163
|
+
return normalizeRedeemCartSnapshot(resolveCart(cartRef.current));
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
warnOnce('reading the cart snapshot failed', error);
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const run = async () => {
|
|
171
|
+
const data = initialDataRef.current;
|
|
172
|
+
if (!data)
|
|
173
|
+
return;
|
|
174
|
+
const loadOptions = {
|
|
175
|
+
core: data.core,
|
|
176
|
+
preloadLazy: ['redeemCart'],
|
|
177
|
+
...(assetUrl ? { assetUrl } : {})
|
|
178
|
+
};
|
|
179
|
+
const loaded = await guarded('widget core load failed', () => loadWidgetCore(loadOptions));
|
|
180
|
+
if (disposed)
|
|
181
|
+
return;
|
|
182
|
+
if (!loaded.loaded)
|
|
183
|
+
throw new StageError('widget core load failed', loaded);
|
|
184
|
+
const loadRedeemCartCore = loaded.coreExports?.loadRedeemCartCore;
|
|
185
|
+
if (typeof loadRedeemCartCore !== 'function') {
|
|
186
|
+
throw new StageError('the widget core has no redeem-cart surface (older CDN core), rendering nothing', undefined);
|
|
187
|
+
}
|
|
188
|
+
const surface = await guarded('loading the redeem-cart core failed', () => loadRedeemCartCore());
|
|
189
|
+
if (disposed)
|
|
190
|
+
return;
|
|
191
|
+
const mount = surface?.mountRedeemCartCore;
|
|
192
|
+
if (typeof mount !== 'function') {
|
|
193
|
+
throw new StageError('the redeem-cart core has no mountRedeemCartCore, rendering nothing', undefined);
|
|
194
|
+
}
|
|
195
|
+
let builtPort = null;
|
|
196
|
+
if (cartFetcherRef.current && cartRevalidateRef.current) {
|
|
197
|
+
try {
|
|
198
|
+
const { HydrogenCartPort: Port } = await import('../hydrogen-cart-port.js');
|
|
199
|
+
if (!disposed) {
|
|
200
|
+
builtPort = new Port({
|
|
201
|
+
fetcher: {
|
|
202
|
+
get state() {
|
|
203
|
+
return cartFetcherRef.current?.state ?? 'idle';
|
|
204
|
+
},
|
|
205
|
+
get data() {
|
|
206
|
+
return cartFetcherRef.current?.data;
|
|
207
|
+
},
|
|
208
|
+
submit: (payload, submitOptions) => cartFetcherRef.current?.submit(payload, submitOptions)
|
|
209
|
+
},
|
|
210
|
+
getCart: () => resolveCart(cartRef.current),
|
|
211
|
+
revalidate: () => cartRevalidateRef.current?.(),
|
|
212
|
+
onCartUpdated: updated => onCartUpdatedRef.current?.(updated),
|
|
213
|
+
...(cartRouteRef.current ? { cartRoute: cartRouteRef.current } : {})
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
console.warn('[joy] JoyRedeemCart: cart-port failed, using copy mode', error);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (disposed)
|
|
222
|
+
return;
|
|
223
|
+
let cartPort = COPY_MODE_PORT;
|
|
224
|
+
if (builtPort) {
|
|
225
|
+
ownPort = builtPort;
|
|
226
|
+
cartPortRef.current = builtPort;
|
|
227
|
+
cartPort = toRedeemCartPort(builtPort);
|
|
228
|
+
}
|
|
229
|
+
const options = await guarded('setting up the mount failed', () => {
|
|
230
|
+
const blob = data.blob;
|
|
231
|
+
const blobTranslation = blob?.translation;
|
|
232
|
+
return {
|
|
233
|
+
source: {
|
|
234
|
+
blob,
|
|
235
|
+
customer: data.customer,
|
|
236
|
+
customerKey: resolveCustomerKey(data),
|
|
237
|
+
translation: flattenTranslation(blobTranslation, data.locale),
|
|
238
|
+
locale: resolveContentLocale(data.locale, blobTranslation),
|
|
239
|
+
loginUrl: loginUrlRef.current ?? data.login_url
|
|
240
|
+
},
|
|
241
|
+
client: data.customer ? createMemberClient({ apiUrl, tokenRoute, locale: data.locale, shopDomain }) : null,
|
|
242
|
+
cartPort,
|
|
243
|
+
getSnapshot,
|
|
244
|
+
hooks: {
|
|
245
|
+
navigate: (url) => onNavigateRef.current
|
|
246
|
+
? onNavigateRef.current(url)
|
|
247
|
+
: window.location.assign(safeNavigationUrl(url)?.href ?? '/')
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
});
|
|
251
|
+
if (disposed)
|
|
252
|
+
return;
|
|
253
|
+
const host = hostRef.current;
|
|
254
|
+
if (!host)
|
|
255
|
+
return;
|
|
256
|
+
const created = await guarded('mount failed', () => mount(host, options));
|
|
257
|
+
handle = created ?? null;
|
|
258
|
+
if (disposed) {
|
|
259
|
+
disposeHandle();
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
handleRef.current = handle;
|
|
263
|
+
await guarded('reading the first cart snapshot failed', () => normalizeRedeemCartSnapshot(resolveCart(cartRef.current)));
|
|
264
|
+
if (disposed)
|
|
265
|
+
return;
|
|
266
|
+
if (host.childNodes.length > 0)
|
|
267
|
+
return setReserved(false);
|
|
268
|
+
releaseObserver = new MutationObserver(() => {
|
|
269
|
+
if (host.childNodes.length === 0)
|
|
270
|
+
return;
|
|
271
|
+
releaseObserver?.disconnect();
|
|
272
|
+
setReserved(false);
|
|
273
|
+
});
|
|
274
|
+
releaseObserver.observe(host, { childList: true });
|
|
275
|
+
releaseTimer = setTimeout(() => {
|
|
276
|
+
releaseObserver?.disconnect();
|
|
277
|
+
setReserved(false);
|
|
278
|
+
}, PLACEHOLDER_RELEASE_FALLBACK_MS);
|
|
279
|
+
};
|
|
280
|
+
void run()
|
|
281
|
+
.catch(error => {
|
|
282
|
+
if (error instanceof StageError)
|
|
283
|
+
fail(error.stage, error.detail);
|
|
284
|
+
else
|
|
285
|
+
fail('unexpected failure', error);
|
|
286
|
+
})
|
|
287
|
+
.catch(() => { });
|
|
288
|
+
return () => {
|
|
289
|
+
disposed = true;
|
|
290
|
+
releaseObserver?.disconnect();
|
|
291
|
+
clearTimeout(releaseTimer);
|
|
292
|
+
if (cartPortRef.current === ownPort)
|
|
293
|
+
cartPortRef.current = null;
|
|
294
|
+
disposeHandle();
|
|
295
|
+
};
|
|
296
|
+
}, [apiUrl, shopDomain, tokenRoute, assetUrl, customerKey, locale, hasInitialData]);
|
|
297
|
+
const resolvedCart = resolveCart(effectiveCart);
|
|
298
|
+
useEffect(() => {
|
|
299
|
+
if (!handleRef.current)
|
|
300
|
+
return;
|
|
301
|
+
const timer = setTimeout(() => {
|
|
302
|
+
const handle = handleRef.current;
|
|
303
|
+
if (!handle)
|
|
304
|
+
return;
|
|
305
|
+
Promise.resolve()
|
|
306
|
+
.then(() => handle.update())
|
|
307
|
+
.catch(error => {
|
|
308
|
+
if (updateWarnedRef.current)
|
|
309
|
+
return;
|
|
310
|
+
updateWarnedRef.current = true;
|
|
311
|
+
console.warn('[joy] JoyRedeemCart: update failed', error);
|
|
312
|
+
});
|
|
313
|
+
}, CART_UPDATE_DEBOUNCE_MS);
|
|
314
|
+
return () => clearTimeout(timer);
|
|
315
|
+
}, [resolvedCart]);
|
|
316
|
+
return (_jsxs("div", { className: className, hidden: isCollapsed, style: isCollapsed ? { display: 'none' } : undefined, children: [_jsx(WidgetCorePreloadLinks, { core: initialData?.core, assetUrl: assetUrl, lazy: ['redeemCart'] }), createElement('div', {
|
|
317
|
+
ref: hostRef,
|
|
318
|
+
'data-joy-redeem-cart': '',
|
|
319
|
+
style: reserved && !isCollapsed ? { minHeight: PLACEHOLDER_MIN_HEIGHT } : undefined
|
|
320
|
+
})] }));
|
|
321
|
+
}
|
package/dist/render-adapter.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { buildWriteReviewProps } from './lib/write-review.js';
|
|
2
2
|
import { formatMoney } from './lib/money.js';
|
|
3
3
|
import { formatPointsNumber } from './lib/points.js';
|
|
4
|
+
import { withoutHeadlessHiddenPrograms } from './lib/submit-form-visibility.js';
|
|
4
5
|
const TYPE_BRANDING_DEFAULT = 'default';
|
|
5
6
|
const TYPE_BRANDING_IMAGE = 'image';
|
|
6
7
|
const TYPE_BRANDING_BLANK = 'blank';
|
|
@@ -82,6 +83,9 @@ function assembleHeadlessFacade(params) {
|
|
|
82
83
|
const rawSettings = applyTranslatedPointLabel({ ...rawSettings0 }, activeTranslation);
|
|
83
84
|
const resolvedSettings = asRecord(resolveWidgetSettings(rawSettings0));
|
|
84
85
|
const clonedProgram = blob.program != null ? structuredClone(blob.program) : blob.program;
|
|
86
|
+
const clonedEarning = asRecord(clonedProgram).earning;
|
|
87
|
+
if (Array.isArray(clonedEarning))
|
|
88
|
+
asRecord(clonedProgram).earning = withoutHeadlessHiddenPrograms(clonedEarning);
|
|
85
89
|
const data = {
|
|
86
90
|
...blob,
|
|
87
91
|
program: clonedProgram,
|
|
@@ -390,9 +390,10 @@ export function createWidgetOrchestrator(container, hostAdapter, initialData, ap
|
|
|
390
390
|
});
|
|
391
391
|
if (!built)
|
|
392
392
|
return null;
|
|
393
|
-
const { facade } = built;
|
|
393
|
+
const { facade, renderInput } = built;
|
|
394
394
|
facade.hostAdapter.data = hostAdapter.data;
|
|
395
395
|
facade.widgetEl = element;
|
|
396
|
+
facade._renderInput = renderInput;
|
|
396
397
|
return facade;
|
|
397
398
|
}
|
|
398
399
|
function loadRecentlyViewed() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@joy-loyalty/hydrogen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Joy Loyalty Widget V4 for any Hydrogen / headless Shopify storefront — host-neutral core embedded through HeadlessHostAdapter. No Weaverse dependency.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"type": "module",
|
|
@@ -95,7 +95,7 @@
|
|
|
95
95
|
}
|
|
96
96
|
},
|
|
97
97
|
"dependencies": {
|
|
98
|
-
"@joy-loyalty/sdk": "^0.
|
|
98
|
+
"@joy-loyalty/sdk": "^0.3.0",
|
|
99
99
|
"dompurify": "^3.1.7"
|
|
100
100
|
},
|
|
101
101
|
"devDependencies": {
|