@joy-loyalty/hydrogen 0.0.0-stage → 0.2.1
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 +3 -0
- package/README.md +134 -2
- package/dist/cache-control.d.ts +8 -0
- package/dist/cache-control.js +6 -0
- package/dist/csp.d.ts +14 -0
- package/dist/csp.js +33 -0
- package/dist/headless-host-adapter.d.ts +5 -0
- package/dist/headless-host-adapter.js +198 -0
- package/dist/host-adapter/capabilities.d.ts +4 -0
- package/dist/host-adapter/capabilities.js +15 -0
- package/dist/host-adapter/data-source.d.ts +3 -0
- package/dist/host-adapter/data-source.js +49 -0
- package/dist/host-adapter/product-fetcher.d.ts +2 -0
- package/dist/host-adapter/product-fetcher.js +38 -0
- package/dist/host-adapter/promoted-collections.d.ts +2 -0
- package/dist/host-adapter/promoted-collections.js +44 -0
- package/dist/host-adapter/types.d.ts +212 -0
- package/dist/host-adapter/types.js +1 -0
- package/dist/hydrogen-cart-port.d.ts +134 -0
- package/dist/hydrogen-cart-port.js +245 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +15 -0
- package/dist/joy-widget.d.ts +29 -0
- package/dist/joy-widget.js +291 -0
- package/dist/lib/JoyProvider.d.ts +47 -0
- package/dist/lib/JoyProvider.js +95 -0
- package/dist/lib/account-links.d.ts +5 -0
- package/dist/lib/account-links.js +65 -0
- package/dist/lib/apply-behavior.d.ts +12 -0
- package/dist/lib/apply-behavior.js +26 -0
- package/dist/lib/core-loader.d.ts +63 -0
- package/dist/lib/core-loader.js +160 -0
- package/dist/lib/core-manifest.d.ts +7 -0
- package/dist/lib/core-manifest.js +54 -0
- package/dist/lib/core-preload-links.d.ts +8 -0
- package/dist/lib/core-preload-links.js +8 -0
- package/dist/lib/coupon-mapper.d.ts +95 -0
- package/dist/lib/coupon-mapper.js +167 -0
- package/dist/lib/custom-links.d.ts +22 -0
- package/dist/lib/custom-links.js +56 -0
- package/dist/lib/deeplink.d.ts +25 -0
- package/dist/lib/deeplink.js +92 -0
- package/dist/lib/earn-signup-gate.d.ts +3 -0
- package/dist/lib/earn-signup-gate.js +31 -0
- package/dist/lib/facebook-ref-link.d.ts +13 -0
- package/dist/lib/facebook-ref-link.js +31 -0
- package/dist/lib/headless-referral-origin.d.ts +2 -0
- package/dist/lib/headless-referral-origin.js +24 -0
- package/dist/lib/headless-source.d.ts +8 -0
- package/dist/lib/headless-source.js +71 -0
- package/dist/lib/image.d.ts +2 -0
- package/dist/lib/image.js +14 -0
- package/dist/lib/metafield-blob.d.ts +33 -0
- package/dist/lib/metafield-blob.js +76 -0
- package/dist/lib/money.d.ts +6 -0
- package/dist/lib/money.js +25 -0
- package/dist/lib/order-mapper.d.ts +44 -0
- package/dist/lib/order-mapper.js +141 -0
- package/dist/lib/place-order-scope.d.ts +6 -0
- package/dist/lib/place-order-scope.js +22 -0
- package/dist/lib/points.d.ts +5 -0
- package/dist/lib/points.js +10 -0
- package/dist/lib/recently-viewed-tracker.d.ts +1 -0
- package/dist/lib/recently-viewed-tracker.js +34 -0
- package/dist/lib/recommended-loader.d.ts +44 -0
- package/dist/lib/recommended-loader.js +113 -0
- package/dist/lib/referral-share.d.ts +4 -0
- package/dist/lib/referral-share.js +33 -0
- package/dist/lib/safe-navigation-url.d.ts +1 -0
- package/dist/lib/safe-navigation-url.js +9 -0
- package/dist/lib/server-token.d.ts +41 -0
- package/dist/lib/server-token.js +87 -0
- package/dist/lib/shop-locale.d.ts +1 -0
- package/dist/lib/shop-locale.js +21 -0
- package/dist/lib/social-earn.d.ts +11 -0
- package/dist/lib/social-earn.js +25 -0
- package/dist/lib/storefront-fonts.d.ts +5 -0
- package/dist/lib/storefront-fonts.js +52 -0
- package/dist/lib/token.d.ts +14 -0
- package/dist/lib/token.js +135 -0
- package/dist/lib/write-review.d.ts +2 -0
- package/dist/lib/write-review.js +44 -0
- package/dist/loyalty-page/JoyLoyaltyPage.d.ts +151 -0
- package/dist/loyalty-page/JoyLoyaltyPage.js +447 -0
- package/dist/loyalty-page/compose-home.d.ts +35 -0
- package/dist/loyalty-page/compose-home.js +177 -0
- package/dist/loyalty-page/skeleton.d.ts +11 -0
- package/dist/loyalty-page/skeleton.js +99 -0
- package/dist/orchestrator/context.d.ts +61 -0
- package/dist/orchestrator/context.js +64 -0
- package/dist/orchestrator/gated.d.ts +1 -0
- package/dist/orchestrator/gated.js +14 -0
- package/dist/orchestrator/handlers/account.d.ts +19 -0
- package/dist/orchestrator/handlers/account.js +266 -0
- package/dist/orchestrator/handlers/birthday.d.ts +6 -0
- package/dist/orchestrator/handlers/birthday.js +128 -0
- package/dist/orchestrator/handlers/earn.d.ts +5 -0
- package/dist/orchestrator/handlers/earn.js +267 -0
- package/dist/orchestrator/handlers/milestone-tier.d.ts +5 -0
- package/dist/orchestrator/handlers/milestone-tier.js +123 -0
- package/dist/orchestrator/handlers/profile.d.ts +5 -0
- package/dist/orchestrator/handlers/profile.js +262 -0
- package/dist/orchestrator/handlers/redeem.d.ts +6 -0
- package/dist/orchestrator/handlers/redeem.js +464 -0
- package/dist/orchestrator/handlers/referral.d.ts +5 -0
- package/dist/orchestrator/handlers/referral.js +218 -0
- package/dist/orchestrator/render.d.ts +2 -0
- package/dist/orchestrator/render.js +193 -0
- package/dist/render-adapter.d.ts +50 -0
- package/dist/render-adapter.js +422 -0
- package/dist/routes/joy-place-order-scope.d.ts +12 -0
- package/dist/routes/joy-place-order-scope.js +32 -0
- package/dist/routes/joy-products.d.ts +48 -0
- package/dist/routes/joy-products.js +338 -0
- package/dist/routes/joy-token.d.ts +16 -0
- package/dist/routes/joy-token.js +32 -0
- package/dist/server/get-joy-initial-data.d.ts +39 -0
- package/dist/server/get-joy-initial-data.js +65 -0
- package/dist/server/get-joy-loyalty-page-data.d.ts +28 -0
- package/dist/server/get-joy-loyalty-page-data.js +99 -0
- package/dist/server/index.d.ts +8 -0
- package/dist/server/index.js +4 -0
- package/dist/widget-controller.d.ts +2 -0
- package/dist/widget-controller.js +1 -0
- package/dist/widget-orchestrator.d.ts +89 -0
- package/dist/widget-orchestrator.js +504 -0
- package/package.json +121 -4
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { useJoyConfig, useJoyCustomer, useJoyProviderCartHandlers } from './lib/JoyProvider.js';
|
|
4
|
+
import { formatPointsNumber, resolvePointsLabel } from './lib/points.js';
|
|
5
|
+
import { loadWidgetCore, preconnectWidgetCore } from './lib/core-loader.js';
|
|
6
|
+
import { WidgetCorePreloadLinks } from './lib/core-preload-links.js';
|
|
7
|
+
import { createHeadlessSource } from './lib/headless-source.js';
|
|
8
|
+
import { getCustomLinkSettings, resolveAuthUrls } from './lib/custom-links.js';
|
|
9
|
+
import { installDeeplinkHandling, resolveDeeplinkCatalog } from './lib/deeplink.js';
|
|
10
|
+
import { installAccountLinkReplacement } from './lib/account-links.js';
|
|
11
|
+
import { createHeadlessHostAdapter } from './headless-host-adapter.js';
|
|
12
|
+
import { trackRecentlyViewedProduct } from './lib/recently-viewed-tracker.js';
|
|
13
|
+
import { resolveShopLocale } from './lib/shop-locale.js';
|
|
14
|
+
import { createWidgetOrchestrator } from './widget-orchestrator.js';
|
|
15
|
+
const ACCENT = 'var(--joy-accent, var(--color-primary, #054597))';
|
|
16
|
+
const fetchCustomer = (client) => client.getCustomer();
|
|
17
|
+
function resolveShopDomain(initialData) {
|
|
18
|
+
const shop = initialData?.blob?.shop;
|
|
19
|
+
return shop?.shopifyDomain || shop?.domain || undefined;
|
|
20
|
+
}
|
|
21
|
+
function resolveBlobSettings(initialData) {
|
|
22
|
+
return initialData?.blob?.settings;
|
|
23
|
+
}
|
|
24
|
+
function resolveCart(cart) {
|
|
25
|
+
return typeof cart === 'function' ? cart() : cart;
|
|
26
|
+
}
|
|
27
|
+
function scheduleIdle(cb) {
|
|
28
|
+
const w = window;
|
|
29
|
+
let cancelled = false;
|
|
30
|
+
let idleId;
|
|
31
|
+
let timerId;
|
|
32
|
+
const whenIdle = () => {
|
|
33
|
+
if (cancelled)
|
|
34
|
+
return;
|
|
35
|
+
if (typeof w.requestIdleCallback === 'function') {
|
|
36
|
+
idleId = w.requestIdleCallback(() => !cancelled && cb(), { timeout: 10_000 });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
timerId = window.setTimeout(() => !cancelled && cb(), 1200);
|
|
40
|
+
};
|
|
41
|
+
const onLoad = () => whenIdle();
|
|
42
|
+
if (document.readyState === 'complete')
|
|
43
|
+
whenIdle();
|
|
44
|
+
else
|
|
45
|
+
window.addEventListener('load', onLoad, { once: true });
|
|
46
|
+
return {
|
|
47
|
+
cancel: () => {
|
|
48
|
+
cancelled = true;
|
|
49
|
+
window.removeEventListener('load', onLoad);
|
|
50
|
+
if (idleId !== undefined)
|
|
51
|
+
w.cancelIdleCallback?.(idleId);
|
|
52
|
+
if (timerId !== undefined)
|
|
53
|
+
window.clearTimeout(timerId);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export function JoyWidget({ apiUrl = null, tokenRoute, loginUrl = '/account', loyaltyPageUrl = '/pages/rewards', locale, pathname: pathnameProp, shopDomain: shopDomainProp, position, accentColor, launcherLabel, panelHeading = 'Your rewards', pointsLabel = 'points', viewRewardsLabel = 'View rewards', loginLabel = 'Log in', initialData = null, assetUrl, preload = 'idle', cart, cartFetcher, cartRevalidate, cartRoute, onOpenCartDrawer, currency, country }) {
|
|
58
|
+
const resolvedPosition = position ?? 'bottom-right';
|
|
59
|
+
const resolvedLauncherLabel = launcherLabel ?? 'Rewards';
|
|
60
|
+
const shopDomain = resolveShopDomain(initialData) ?? shopDomainProp;
|
|
61
|
+
const [open, setOpen] = useState(false);
|
|
62
|
+
const [coreLoaded, setCoreLoaded] = useState(false);
|
|
63
|
+
const [coreFailed, setCoreFailed] = useState(false);
|
|
64
|
+
const hostConfig = useJoyConfig({ apiUrl, loginUrl, locale: locale ?? initialData?.locale, shopDomain });
|
|
65
|
+
const config = {
|
|
66
|
+
...hostConfig,
|
|
67
|
+
locale: resolveShopLocale(hostConfig.locale, initialData?.blob?.translation)
|
|
68
|
+
};
|
|
69
|
+
const resolvedTokenRoute = tokenRoute ?? config.tokenRoute;
|
|
70
|
+
const blobSettings = resolveBlobSettings(initialData);
|
|
71
|
+
const authUrls = resolveAuthUrls(blobSettings, { loginUrl: config.loginUrl, registerUrl: '/account/register' });
|
|
72
|
+
const seededPoints = initialData?.customer?.points ?? null;
|
|
73
|
+
const state = useJoyCustomer(fetchCustomer, {
|
|
74
|
+
apiUrl: config.apiUrl,
|
|
75
|
+
tokenRoute: resolvedTokenRoute,
|
|
76
|
+
locale: config.locale,
|
|
77
|
+
shopDomain: config.shopDomain,
|
|
78
|
+
enabled: open && !coreLoaded && seededPoints == null
|
|
79
|
+
});
|
|
80
|
+
const cartRef = useRef(cart);
|
|
81
|
+
cartRef.current = cart;
|
|
82
|
+
const cartFetcherRef = useRef(cartFetcher);
|
|
83
|
+
cartFetcherRef.current = cartFetcher;
|
|
84
|
+
const cartRevalidateRef = useRef(cartRevalidate);
|
|
85
|
+
cartRevalidateRef.current = cartRevalidate;
|
|
86
|
+
const cartPortRef = useRef(null);
|
|
87
|
+
const providerOpenCartDrawer = useJoyProviderCartHandlers().onOpenCartDrawer;
|
|
88
|
+
const openCartDrawerRef = useRef(onOpenCartDrawer ?? providerOpenCartDrawer);
|
|
89
|
+
openCartDrawerRef.current = onOpenCartDrawer ?? providerOpenCartDrawer;
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
cartPortRef.current?.settle();
|
|
92
|
+
});
|
|
93
|
+
const lastPathRef = useRef(null);
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (typeof window === 'undefined')
|
|
96
|
+
return;
|
|
97
|
+
const pathname = pathnameProp ?? window.location.pathname;
|
|
98
|
+
if (pathname === lastPathRef.current)
|
|
99
|
+
return;
|
|
100
|
+
lastPathRef.current = pathname;
|
|
101
|
+
trackRecentlyViewedProduct(pathname);
|
|
102
|
+
});
|
|
103
|
+
const startCoreRef = useRef(() => { });
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
if (typeof document === 'undefined')
|
|
106
|
+
return;
|
|
107
|
+
let cancelled = false;
|
|
108
|
+
let requested = false;
|
|
109
|
+
let handle;
|
|
110
|
+
let idle;
|
|
111
|
+
const detachers = [];
|
|
112
|
+
const container = document.createElement('div');
|
|
113
|
+
preconnectWidgetCore({ assetUrl });
|
|
114
|
+
const core = initialData?.core ?? null;
|
|
115
|
+
function startCore() {
|
|
116
|
+
if (requested || cancelled)
|
|
117
|
+
return;
|
|
118
|
+
requested = true;
|
|
119
|
+
void loadWidgetCore({ assetUrl, core }).then(async ({ loaded, coreExports }) => {
|
|
120
|
+
if (cancelled)
|
|
121
|
+
return;
|
|
122
|
+
if (!loaded) {
|
|
123
|
+
setCoreFailed(true);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
container.style.display = 'none';
|
|
127
|
+
document.body.appendChild(container);
|
|
128
|
+
if (cartFetcherRef.current && cartRevalidateRef.current && !cartPortRef.current) {
|
|
129
|
+
const { HydrogenCartPort } = await import('./hydrogen-cart-port.js');
|
|
130
|
+
if (cancelled)
|
|
131
|
+
return;
|
|
132
|
+
cartPortRef.current = new HydrogenCartPort({
|
|
133
|
+
fetcher: {
|
|
134
|
+
get state() {
|
|
135
|
+
return cartFetcherRef.current?.state ?? 'idle';
|
|
136
|
+
},
|
|
137
|
+
get data() {
|
|
138
|
+
return cartFetcherRef.current?.data;
|
|
139
|
+
},
|
|
140
|
+
submit: (data, options) => cartFetcherRef.current?.submit(data, options)
|
|
141
|
+
},
|
|
142
|
+
getCart: () => resolveCart(cartRef.current),
|
|
143
|
+
revalidate: () => cartRevalidateRef.current?.(),
|
|
144
|
+
...(cartRoute ? { cartRoute } : {})
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
const source = createHeadlessSource({
|
|
148
|
+
apiUrl: config.apiUrl,
|
|
149
|
+
tokenRoute: resolvedTokenRoute,
|
|
150
|
+
locale: config.locale,
|
|
151
|
+
shopDomain
|
|
152
|
+
});
|
|
153
|
+
const adapter = createHeadlessHostAdapter({
|
|
154
|
+
context: {
|
|
155
|
+
shopDomain: shopDomain ?? window.location.hostname,
|
|
156
|
+
apiUrl: config.apiUrl ?? '',
|
|
157
|
+
currency: currency ?? resolveCart(cartRef.current)?.cost?.totalAmount?.currencyCode ?? 'USD',
|
|
158
|
+
locale: config.locale,
|
|
159
|
+
page: { type: 'index', resourceId: '', handle: '' },
|
|
160
|
+
routesRoot: '/',
|
|
161
|
+
loginUrl: authUrls.loginUrl,
|
|
162
|
+
registerUrl: authUrls.registerUrl,
|
|
163
|
+
loyaltyPageUrl,
|
|
164
|
+
accountUrl: '/account',
|
|
165
|
+
country: country ?? initialData?.country ?? 'US',
|
|
166
|
+
viewport: { width: window.innerWidth }
|
|
167
|
+
},
|
|
168
|
+
source
|
|
169
|
+
});
|
|
170
|
+
const isBlobPath = !!(coreExports?.renderingProps && initialData?.blob);
|
|
171
|
+
const appearance = isBlobPath
|
|
172
|
+
? {
|
|
173
|
+
collapsed: true,
|
|
174
|
+
...(position !== undefined ? { position: position === 'bottom-left' ? 'left' : 'right' } : {}),
|
|
175
|
+
...(accentColor !== undefined ? { primaryColor: accentColor } : {}),
|
|
176
|
+
...(launcherLabel !== undefined ? { fabLabel: launcherLabel } : {})
|
|
177
|
+
}
|
|
178
|
+
: {
|
|
179
|
+
position: resolvedPosition === 'bottom-left' ? 'left' : 'right',
|
|
180
|
+
theme: 'light',
|
|
181
|
+
primaryColor: accentColor,
|
|
182
|
+
fabLabel: resolvedLauncherLabel,
|
|
183
|
+
collapsed: true
|
|
184
|
+
};
|
|
185
|
+
handle = createWidgetOrchestrator(container, adapter, initialData, appearance, cartPortRef.current ?? undefined, coreExports, {
|
|
186
|
+
openCartDrawer: () => {
|
|
187
|
+
if (!openCartDrawerRef.current)
|
|
188
|
+
return false;
|
|
189
|
+
openCartDrawerRef.current();
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
void handle.ready.finally(() => {
|
|
193
|
+
if (cancelled)
|
|
194
|
+
return;
|
|
195
|
+
container.style.display = '';
|
|
196
|
+
attachLinkHandlers(coreExports);
|
|
197
|
+
setCoreLoaded(true);
|
|
198
|
+
});
|
|
199
|
+
}).catch(err => {
|
|
200
|
+
console.warn('[joy] failed to mount the widget core', err);
|
|
201
|
+
if (!cancelled)
|
|
202
|
+
setCoreFailed(true);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
function attachLinkHandlers(coreExports) {
|
|
206
|
+
const el = container.querySelector('joy-loyalty-widget');
|
|
207
|
+
if (!el)
|
|
208
|
+
return;
|
|
209
|
+
detachers.push(installDeeplinkHandling(el, resolveDeeplinkCatalog(coreExports)));
|
|
210
|
+
const links = getCustomLinkSettings(blobSettings);
|
|
211
|
+
if (!links.replaceAccountLink)
|
|
212
|
+
return;
|
|
213
|
+
detachers.push(installAccountLinkReplacement({
|
|
214
|
+
customSelector: links.accountLinkSelector,
|
|
215
|
+
onOpen: () => {
|
|
216
|
+
if (!el._isOpen)
|
|
217
|
+
el.open();
|
|
218
|
+
}
|
|
219
|
+
}));
|
|
220
|
+
}
|
|
221
|
+
startCoreRef.current = startCore;
|
|
222
|
+
if (preload === 'eager')
|
|
223
|
+
startCore();
|
|
224
|
+
else if (preload === 'idle')
|
|
225
|
+
idle = scheduleIdle(startCore);
|
|
226
|
+
return () => {
|
|
227
|
+
cancelled = true;
|
|
228
|
+
idle?.cancel();
|
|
229
|
+
detachers.forEach(detach => detach());
|
|
230
|
+
handle?.dispose();
|
|
231
|
+
container.remove();
|
|
232
|
+
};
|
|
233
|
+
}, [config.apiUrl, resolvedTokenRoute, config.locale, shopDomain]);
|
|
234
|
+
const points = state.status === 'ready' ? state.data.points : state.status === 'loading' ? seededPoints : null;
|
|
235
|
+
const side = resolvedPosition === 'bottom-left' ? { left: '20px' } : { right: '20px' };
|
|
236
|
+
const rootStyle = {
|
|
237
|
+
position: 'fixed',
|
|
238
|
+
bottom: '20px',
|
|
239
|
+
zIndex: 2147483000,
|
|
240
|
+
...side,
|
|
241
|
+
...(accentColor ? { '--joy-accent': accentColor } : null)
|
|
242
|
+
};
|
|
243
|
+
const showFallbackFab = preload === 'interaction' && !coreLoaded;
|
|
244
|
+
const core = initialData?.core ?? null;
|
|
245
|
+
return (_jsxs("div", { style: rootStyle, children: [preload !== 'interaction' ? _jsx(WidgetCorePreloadLinks, { core: core, assetUrl: assetUrl }) : null, showFallbackFab ? (_jsxs(_Fragment, { children: [open ? (_jsxs("div", { role: "dialog", "aria-label": panelHeading, style: {
|
|
246
|
+
width: '240px',
|
|
247
|
+
marginBottom: '12px',
|
|
248
|
+
padding: '18px',
|
|
249
|
+
borderRadius: '14px',
|
|
250
|
+
background: 'var(--joy-card-bg, var(--color-background, #ffffff))',
|
|
251
|
+
boxShadow: '0 12px 32px rgba(0,0,0,0.18)',
|
|
252
|
+
border: '1px solid rgba(0,0,0,0.08)'
|
|
253
|
+
}, children: [_jsx("strong", { style: { display: 'block', fontSize: '16px', marginBottom: '12px' }, children: panelHeading }), renderBody()] })) : null, _jsx("button", { type: "button", "aria-expanded": open, onPointerEnter: () => startCoreRef.current(), onPointerDown: () => startCoreRef.current(), onFocus: () => startCoreRef.current(), onClick: () => {
|
|
254
|
+
startCoreRef.current();
|
|
255
|
+
setOpen(value => !value);
|
|
256
|
+
}, style: {
|
|
257
|
+
display: 'block',
|
|
258
|
+
marginLeft: resolvedPosition === 'bottom-left' ? 0 : 'auto',
|
|
259
|
+
padding: '12px 20px',
|
|
260
|
+
borderRadius: '999px',
|
|
261
|
+
border: 'none',
|
|
262
|
+
background: ACCENT,
|
|
263
|
+
color: '#fff',
|
|
264
|
+
fontSize: '14px',
|
|
265
|
+
fontWeight: 600,
|
|
266
|
+
cursor: 'pointer',
|
|
267
|
+
boxShadow: '0 6px 18px rgba(0,0,0,0.22)'
|
|
268
|
+
}, children: resolvedLauncherLabel })] })) : null] }));
|
|
269
|
+
function renderBody() {
|
|
270
|
+
if (state.status === 'error') {
|
|
271
|
+
return (_jsx("p", { style: { margin: 0, fontSize: '13px', opacity: 0.7 }, children: state.message === 'missing_env'
|
|
272
|
+
? 'Set JOY_API_URL / JOY_SHOP_DOMAIN and pass apiUrl to <JoyWidget/>.'
|
|
273
|
+
: 'Unable to load your points right now.' }));
|
|
274
|
+
}
|
|
275
|
+
if (state.status === 'guest') {
|
|
276
|
+
return (_jsx("a", { href: authUrls.loginUrl, style: ctaStyle, children: loginLabel }));
|
|
277
|
+
}
|
|
278
|
+
const loading = state.status === 'loading' && points == null;
|
|
279
|
+
return (_jsxs("div", { children: [_jsxs("div", { "aria-busy": loading || undefined, style: loading ? { opacity: 0.35 } : undefined, children: [_jsx("strong", { style: { fontSize: '28px', color: ACCENT }, children: points != null ? formatPointsNumber(points, config.locale) : '—' }), ' ', _jsx("span", { style: { fontSize: '14px', opacity: 0.8 }, children: resolvePointsLabel(points ?? 0, pointsLabel) })] }), _jsx("a", { href: loyaltyPageUrl, style: { ...ctaStyle, marginTop: '12px' }, children: viewRewardsLabel })] }));
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const ctaStyle = {
|
|
283
|
+
display: 'inline-block',
|
|
284
|
+
padding: '9px 16px',
|
|
285
|
+
borderRadius: '8px',
|
|
286
|
+
border: `1px solid ${ACCENT}`,
|
|
287
|
+
color: ACCENT,
|
|
288
|
+
textDecoration: 'none',
|
|
289
|
+
fontSize: '13px',
|
|
290
|
+
fontWeight: 600
|
|
291
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { JoyClient } from '@joy-loyalty/sdk/client';
|
|
3
|
+
export interface JoyConfig {
|
|
4
|
+
apiUrl: string | null;
|
|
5
|
+
tokenRoute: string;
|
|
6
|
+
loginUrl: string;
|
|
7
|
+
locale?: string;
|
|
8
|
+
shopDomain?: string;
|
|
9
|
+
}
|
|
10
|
+
export type JoyProviderCart = unknown;
|
|
11
|
+
export interface JoyProviderCartHandlers {
|
|
12
|
+
onCartUpdated?: (cart: JoyProviderCart) => void;
|
|
13
|
+
onOpenCartDrawer?: () => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function JoyProvider({ apiUrl, tokenRoute, loginUrl, locale, shopDomain, cart, onCartUpdated, onOpenCartDrawer, children }: Partial<Omit<JoyConfig, 'apiUrl'>> & JoyProviderCartHandlers & {
|
|
16
|
+
apiUrl?: string | null;
|
|
17
|
+
cart?: JoyProviderCart;
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
}): import("react").JSX.Element;
|
|
20
|
+
export declare function useJoyProviderCart(): JoyProviderCart | undefined;
|
|
21
|
+
export declare function useJoyProviderCartHandlers(): JoyProviderCartHandlers;
|
|
22
|
+
export declare function useJoyConfig(fallback: {
|
|
23
|
+
apiUrl: string | null;
|
|
24
|
+
loginUrl: string;
|
|
25
|
+
locale?: string;
|
|
26
|
+
shopDomain?: string;
|
|
27
|
+
}): JoyConfig;
|
|
28
|
+
export type JoyCustomerState<T> = {
|
|
29
|
+
status: 'loading';
|
|
30
|
+
} | {
|
|
31
|
+
status: 'guest';
|
|
32
|
+
} | {
|
|
33
|
+
status: 'ready';
|
|
34
|
+
data: T;
|
|
35
|
+
} | {
|
|
36
|
+
status: 'error';
|
|
37
|
+
message: string;
|
|
38
|
+
};
|
|
39
|
+
export declare function ensureClient(apiUrl: string, tokenRoute: string, locale?: string, shopDomain?: string): JoyClient;
|
|
40
|
+
export declare function useJoyCustomer<T>(fetcher: (client: JoyClient) => Promise<T>, options: {
|
|
41
|
+
apiUrl: string | null;
|
|
42
|
+
tokenRoute: string;
|
|
43
|
+
locale?: string;
|
|
44
|
+
shopDomain?: string;
|
|
45
|
+
deps?: readonly unknown[];
|
|
46
|
+
enabled?: boolean;
|
|
47
|
+
}): JoyCustomerState<T>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { Joy } from '@joy-loyalty/sdk/client';
|
|
4
|
+
import { DEFAULT_TOKEN_ROUTE, getJoyToken } from './token.js';
|
|
5
|
+
const JoyContext = createContext(null);
|
|
6
|
+
export function JoyProvider({ apiUrl, tokenRoute, loginUrl, locale, shopDomain, cart, onCartUpdated, onOpenCartDrawer, children }) {
|
|
7
|
+
const value = useMemo(() => ({ apiUrl: apiUrl ?? undefined, tokenRoute, loginUrl, locale, shopDomain, cart, onCartUpdated, onOpenCartDrawer }), [apiUrl, tokenRoute, loginUrl, locale, shopDomain, cart, onCartUpdated, onOpenCartDrawer]);
|
|
8
|
+
return _jsx(JoyContext.Provider, { value: value, children: children });
|
|
9
|
+
}
|
|
10
|
+
export function useJoyProviderCart() {
|
|
11
|
+
return useContext(JoyContext)?.cart;
|
|
12
|
+
}
|
|
13
|
+
export function useJoyProviderCartHandlers() {
|
|
14
|
+
const ctx = useContext(JoyContext);
|
|
15
|
+
return { onCartUpdated: ctx?.onCartUpdated, onOpenCartDrawer: ctx?.onOpenCartDrawer };
|
|
16
|
+
}
|
|
17
|
+
export function useJoyConfig(fallback) {
|
|
18
|
+
const ctx = useContext(JoyContext);
|
|
19
|
+
return {
|
|
20
|
+
apiUrl: ctx?.apiUrl ?? fallback.apiUrl,
|
|
21
|
+
tokenRoute: ctx?.tokenRoute ?? DEFAULT_TOKEN_ROUTE,
|
|
22
|
+
loginUrl: ctx?.loginUrl ?? fallback.loginUrl,
|
|
23
|
+
locale: ctx?.locale ?? fallback.locale,
|
|
24
|
+
shopDomain: ctx?.shopDomain ?? fallback.shopDomain
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
let initializedLocale;
|
|
28
|
+
let hasLocale = false;
|
|
29
|
+
let initializedShopDomain;
|
|
30
|
+
export function ensureClient(apiUrl, tokenRoute, locale, shopDomain) {
|
|
31
|
+
const sameShop = !shopDomain || shopDomain === initializedShopDomain;
|
|
32
|
+
if (hasLocale && initializedLocale === locale && sameShop) {
|
|
33
|
+
try {
|
|
34
|
+
return Joy.getClient();
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const client = Joy.init({
|
|
40
|
+
apiUrl,
|
|
41
|
+
locale,
|
|
42
|
+
shopDomain: shopDomain ?? initializedShopDomain,
|
|
43
|
+
onTokenExpired: () => getJoyToken({ force: true, tokenRoute })
|
|
44
|
+
});
|
|
45
|
+
initializedLocale = locale;
|
|
46
|
+
initializedShopDomain = shopDomain ?? initializedShopDomain;
|
|
47
|
+
hasLocale = true;
|
|
48
|
+
return client;
|
|
49
|
+
}
|
|
50
|
+
export function useJoyCustomer(fetcher, options) {
|
|
51
|
+
const { apiUrl, tokenRoute, locale, shopDomain, deps = [], enabled = true } = options;
|
|
52
|
+
const [state, setState] = useState({ status: 'loading' });
|
|
53
|
+
const fetcherRef = useRef(fetcher);
|
|
54
|
+
fetcherRef.current = fetcher;
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (!enabled)
|
|
57
|
+
return;
|
|
58
|
+
if (!apiUrl) {
|
|
59
|
+
setState({ status: 'error', message: 'missing_env' });
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
let cancelled = false;
|
|
63
|
+
setState({ status: 'loading' });
|
|
64
|
+
(async () => {
|
|
65
|
+
try {
|
|
66
|
+
const client = ensureClient(apiUrl, tokenRoute, locale, shopDomain);
|
|
67
|
+
const token = await getJoyToken({ tokenRoute });
|
|
68
|
+
if (cancelled)
|
|
69
|
+
return;
|
|
70
|
+
if (!token) {
|
|
71
|
+
setState({ status: 'guest' });
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
client.setToken(token);
|
|
75
|
+
const data = await fetcherRef.current(client);
|
|
76
|
+
if (cancelled)
|
|
77
|
+
return;
|
|
78
|
+
setState({ status: 'ready', data });
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (cancelled)
|
|
82
|
+
return;
|
|
83
|
+
if (error?.statusCode === 401) {
|
|
84
|
+
setState({ status: 'guest' });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
setState({ status: 'error', message: error instanceof Error ? error.message : 'unknown_error' });
|
|
88
|
+
}
|
|
89
|
+
})();
|
|
90
|
+
return () => {
|
|
91
|
+
cancelled = true;
|
|
92
|
+
};
|
|
93
|
+
}, [enabled, apiUrl, tokenRoute, locale, shopDomain, ...deps]);
|
|
94
|
+
return state;
|
|
95
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const BUILT_IN_SELECTOR = [
|
|
2
|
+
'a[href$="/account"]',
|
|
3
|
+
'a[href$="/account/"]',
|
|
4
|
+
'a[href$="/account/login"]',
|
|
5
|
+
'a[href*="/account?"]',
|
|
6
|
+
'a[href*="/account/login?"]',
|
|
7
|
+
'a[href*="/customer_identity/login"]',
|
|
8
|
+
'a[href*="/customer_identity/redirect"]',
|
|
9
|
+
'a[href*="/customer_authentication/login"]',
|
|
10
|
+
'a[href*="/customer_authentication/redirect"]',
|
|
11
|
+
'.account-button',
|
|
12
|
+
'shopify-account',
|
|
13
|
+
'button[aria-label*="account" i]',
|
|
14
|
+
'[data-joy-account-replaced="true"]'
|
|
15
|
+
].join(',');
|
|
16
|
+
export function buildAccountLinkSelector(customSelector) {
|
|
17
|
+
const custom = customSelector?.trim();
|
|
18
|
+
if (!custom)
|
|
19
|
+
return BUILT_IN_SELECTOR;
|
|
20
|
+
try {
|
|
21
|
+
document.createElement('div').matches(custom);
|
|
22
|
+
return `${BUILT_IN_SELECTOR},${custom}`;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
console.warn('Invalid Joy account link selector:', custom);
|
|
26
|
+
return BUILT_IN_SELECTOR;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function isIgnored(node) {
|
|
30
|
+
const ignored = node.closest('[data-joy-account-ignore]');
|
|
31
|
+
return !!ignored && ignored.dataset.joyAccountIgnore !== 'false';
|
|
32
|
+
}
|
|
33
|
+
function markAccountLinks(selector) {
|
|
34
|
+
document.querySelectorAll(selector).forEach(node => {
|
|
35
|
+
if (isIgnored(node))
|
|
36
|
+
return;
|
|
37
|
+
if (node instanceof HTMLAnchorElement)
|
|
38
|
+
node.href = '#joy-open';
|
|
39
|
+
node.dataset.joyAccountReplaced = 'true';
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
export function installAccountLinkReplacement({ customSelector, onOpen }) {
|
|
43
|
+
const selector = buildAccountLinkSelector(customSelector);
|
|
44
|
+
const onClick = (event) => {
|
|
45
|
+
if (event.defaultPrevented || event.button !== 0)
|
|
46
|
+
return;
|
|
47
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
|
|
48
|
+
return;
|
|
49
|
+
const trigger = event.target?.closest?.(selector);
|
|
50
|
+
if (!trigger || isIgnored(trigger))
|
|
51
|
+
return;
|
|
52
|
+
if (trigger.hasAttribute('download'))
|
|
53
|
+
return;
|
|
54
|
+
const target = trigger.target;
|
|
55
|
+
if (target && target.toLowerCase() !== '_self')
|
|
56
|
+
return;
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
event.stopImmediatePropagation();
|
|
59
|
+
window.location.hash = 'joy-open';
|
|
60
|
+
onOpen();
|
|
61
|
+
};
|
|
62
|
+
markAccountLinks(selector);
|
|
63
|
+
document.addEventListener('click', onClick, true);
|
|
64
|
+
return () => document.removeEventListener('click', onClick, true);
|
|
65
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface ApplyBehaviorSettings {
|
|
2
|
+
customerApplyDiscountBehavior?: string;
|
|
3
|
+
customerFreeGiftBehavior?: string;
|
|
4
|
+
cartDrawerButtonSelector?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface ApplyBehaviorHost {
|
|
7
|
+
navigate: (url: string) => void;
|
|
8
|
+
cartRoute: string;
|
|
9
|
+
openCartDrawer?: () => boolean | void;
|
|
10
|
+
onCartDrawerOpen?: () => void;
|
|
11
|
+
}
|
|
12
|
+
export declare function followApplyBehavior(settings: ApplyBehaviorSettings | undefined, host: ApplyBehaviorHost): void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export function followApplyBehavior(settings = {}, host) {
|
|
2
|
+
const behavior = settings.customerFreeGiftBehavior === 'auto_add_to_cart_drawer_hide_modal'
|
|
3
|
+
? 'open_cart_drawer'
|
|
4
|
+
: settings.customerApplyDiscountBehavior || 'go_to_cart';
|
|
5
|
+
if (behavior === 'go_to_cart')
|
|
6
|
+
return host.navigate(host.cartRoute);
|
|
7
|
+
if (behavior !== 'open_cart_drawer')
|
|
8
|
+
return;
|
|
9
|
+
if (host.openCartDrawer && host.openCartDrawer() !== false) {
|
|
10
|
+
host.onCartDrawerOpen?.();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (!settings.cartDrawerButtonSelector || typeof document === 'undefined')
|
|
14
|
+
return;
|
|
15
|
+
let button = null;
|
|
16
|
+
try {
|
|
17
|
+
button = document.querySelector(settings.cartDrawerButtonSelector);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!button)
|
|
23
|
+
return;
|
|
24
|
+
host.onCartDrawerOpen?.();
|
|
25
|
+
button.click();
|
|
26
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export declare const SUPPORTED_CORE_API_VERSION = 1;
|
|
2
|
+
export declare const CORE_PRELOAD_ATTR = "data-joy-widget-core-preload";
|
|
3
|
+
export declare const CORE_CSS_ATTR = "data-joy-widget-core-css";
|
|
4
|
+
export interface WidgetCoreManifest {
|
|
5
|
+
buildId: string;
|
|
6
|
+
coreApiVersion?: number;
|
|
7
|
+
entry?: string;
|
|
8
|
+
imports?: string[];
|
|
9
|
+
css?: string[];
|
|
10
|
+
lazy?: Record<string, string[]>;
|
|
11
|
+
}
|
|
12
|
+
export interface WidgetCoreRenderingProps {
|
|
13
|
+
getMenuVisibility(adapter: unknown, settings: unknown, input?: unknown): Record<string, unknown>;
|
|
14
|
+
buildLabels(adapter: unknown, input?: unknown): Record<string, string>;
|
|
15
|
+
buildCoreProps(adapter: unknown, input?: unknown): Record<string, unknown>;
|
|
16
|
+
buildTierProps(adapter: unknown, input?: unknown): Record<string, unknown>;
|
|
17
|
+
buildCustomerProps(adapter: unknown, input?: unknown): Record<string, unknown>;
|
|
18
|
+
buildExpirationProps(adapter: unknown, input?: unknown): Record<string, unknown>;
|
|
19
|
+
buildAppearanceProps(adapter: unknown, input?: unknown): Record<string, unknown>;
|
|
20
|
+
buildVisibilityProps(adapter: unknown, visibility: unknown, input?: unknown): Record<string, unknown>;
|
|
21
|
+
buildQrCodeProps(adapter: unknown, input?: unknown): Record<string, unknown>;
|
|
22
|
+
buildWalletPassProps(adapter: unknown, input?: unknown): Record<string, unknown>;
|
|
23
|
+
buildSectionConfigs(adapter: unknown, settings: unknown, input?: unknown): Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
export interface WidgetCoreExports {
|
|
26
|
+
renderingProps?: WidgetCoreRenderingProps;
|
|
27
|
+
resolveWidgetSettings?: (settings: unknown) => unknown;
|
|
28
|
+
checkProgramRedeemAvailabilitySegmentFE?: (args: {
|
|
29
|
+
shopId: string;
|
|
30
|
+
programsSpending: unknown[];
|
|
31
|
+
shopifyCustomerId?: string;
|
|
32
|
+
post: (path: string, body: Record<string, unknown>) => Promise<unknown>;
|
|
33
|
+
}) => Promise<unknown[]>;
|
|
34
|
+
getDeeplinkByHash?: (hash: string) => unknown;
|
|
35
|
+
resolveDeeplinkHash?: (param: string | null) => string | null;
|
|
36
|
+
[key: string]: unknown;
|
|
37
|
+
}
|
|
38
|
+
export interface LoadWidgetCoreOptions {
|
|
39
|
+
assetUrl?: string;
|
|
40
|
+
core?: WidgetCoreManifest | null;
|
|
41
|
+
preloadLazy?: string[];
|
|
42
|
+
}
|
|
43
|
+
export interface WidgetCoreLoadResult {
|
|
44
|
+
loaded: boolean;
|
|
45
|
+
coreExports?: WidgetCoreExports;
|
|
46
|
+
}
|
|
47
|
+
export declare function loadWidgetCore(options?: LoadWidgetCoreOptions): Promise<WidgetCoreLoadResult>;
|
|
48
|
+
export declare function preconnectWidgetCore(options?: LoadWidgetCoreOptions): void;
|
|
49
|
+
export declare function resolveWidgetCoreAssetUrls(manifest: WidgetCoreManifest, options?: {
|
|
50
|
+
assetUrl?: string;
|
|
51
|
+
lazy?: string[];
|
|
52
|
+
}): {
|
|
53
|
+
modules: string[];
|
|
54
|
+
css: Array<{
|
|
55
|
+
href: string;
|
|
56
|
+
name: string;
|
|
57
|
+
}>;
|
|
58
|
+
};
|
|
59
|
+
export declare function preloadWidgetCore(manifest: WidgetCoreManifest, options?: {
|
|
60
|
+
assetUrl?: string;
|
|
61
|
+
lazy?: string[];
|
|
62
|
+
}): void;
|
|
63
|
+
export declare function isCoreApiCompatible(manifest?: WidgetCoreManifest | null): boolean;
|