@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,504 @@
|
|
|
1
|
+
import { buildHeadlessFacade } from './render-adapter.js';
|
|
2
|
+
import { hasCachedJoyToken, revokeAndClearJoyToken } from './lib/token.js';
|
|
3
|
+
import { loadRecommendedProducts } from './lib/recommended-loader.js';
|
|
4
|
+
import { createOrchestratorContext } from './orchestrator/context.js';
|
|
5
|
+
import { registerAccountHandlers } from './orchestrator/handlers/account.js';
|
|
6
|
+
import { registerEarnHandlers } from './orchestrator/handlers/earn.js';
|
|
7
|
+
import { registerBirthdayHandlers } from './orchestrator/handlers/birthday.js';
|
|
8
|
+
import { registerMilestoneTierHandlers } from './orchestrator/handlers/milestone-tier.js';
|
|
9
|
+
import { registerReferralHandlers } from './orchestrator/handlers/referral.js';
|
|
10
|
+
import { registerProfileHandlers } from './orchestrator/handlers/profile.js';
|
|
11
|
+
import { registerRedeemHandlers } from './orchestrator/handlers/redeem.js';
|
|
12
|
+
import { GATED_EVENTS } from './orchestrator/gated.js';
|
|
13
|
+
let warnedStaleRecentlyViewedCore = false;
|
|
14
|
+
export function buildAppearanceProps(appearance) {
|
|
15
|
+
return {
|
|
16
|
+
variant: appearance?.variant ?? 'floating-drawer',
|
|
17
|
+
position: appearance?.position ?? 'right',
|
|
18
|
+
theme: appearance?.theme ?? 'light',
|
|
19
|
+
'primary-color': appearance?.primaryColor,
|
|
20
|
+
'fab-label': appearance?.fabLabel,
|
|
21
|
+
collapsed: appearance?.collapsed ?? true
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export function buildAppearanceOverrides(appearance) {
|
|
25
|
+
const overrides = {};
|
|
26
|
+
if (appearance?.variant !== undefined)
|
|
27
|
+
overrides.variant = appearance.variant;
|
|
28
|
+
if (appearance?.position !== undefined)
|
|
29
|
+
overrides.position = appearance.position;
|
|
30
|
+
if (appearance?.theme !== undefined)
|
|
31
|
+
overrides.theme = appearance.theme;
|
|
32
|
+
if (appearance?.primaryColor !== undefined)
|
|
33
|
+
overrides['primary-color'] = appearance.primaryColor;
|
|
34
|
+
if (appearance?.fabLabel !== undefined)
|
|
35
|
+
overrides['fab-label'] = appearance.fabLabel;
|
|
36
|
+
if (appearance?.collapsed !== undefined)
|
|
37
|
+
overrides.collapsed = appearance.collapsed;
|
|
38
|
+
return overrides;
|
|
39
|
+
}
|
|
40
|
+
function mapPrograms(programs) {
|
|
41
|
+
const earnPrograms = programs
|
|
42
|
+
.filter(program => program.programType === 'earning')
|
|
43
|
+
.map(program => ({
|
|
44
|
+
id: program.id,
|
|
45
|
+
icon: 'star',
|
|
46
|
+
title: program.title,
|
|
47
|
+
description: program.description,
|
|
48
|
+
points: program.points ?? undefined,
|
|
49
|
+
value: program.points != null ? `+${program.points} pts` : ''
|
|
50
|
+
}));
|
|
51
|
+
const redeemPrograms = programs
|
|
52
|
+
.filter(program => program.programType === 'spending')
|
|
53
|
+
.map(program => ({
|
|
54
|
+
id: program.id,
|
|
55
|
+
icon: 'gift',
|
|
56
|
+
title: program.title,
|
|
57
|
+
description: program.description,
|
|
58
|
+
points: program.pointsCost ?? 0,
|
|
59
|
+
value: program.pointsCost != null ? `${program.pointsCost} pts` : ''
|
|
60
|
+
}));
|
|
61
|
+
return { earnPrograms, redeemPrograms };
|
|
62
|
+
}
|
|
63
|
+
function buildTierBar(customer, tiers) {
|
|
64
|
+
const tierProgress = customer?.tierProgress;
|
|
65
|
+
if (!customer || !tierProgress || typeof customer.tierPoint !== 'number')
|
|
66
|
+
return {};
|
|
67
|
+
const tierPoint = customer.tierPoint;
|
|
68
|
+
if (typeof tierProgress.currentTarget === 'number') {
|
|
69
|
+
if (tierProgress.nextTarget == null) {
|
|
70
|
+
const full = Math.max(1, tierProgress.currentTarget);
|
|
71
|
+
return { progress: full, required: full };
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
progress: Math.max(0, tierPoint - tierProgress.currentTarget),
|
|
75
|
+
required: Math.max(1, tierProgress.nextTarget - tierProgress.currentTarget)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const nextTarget = tierPoint + tierProgress.pointsToNext;
|
|
79
|
+
const threshold = customer.tier ? tiers?.find(tier => tier.id === customer.tier.id)?.threshold : undefined;
|
|
80
|
+
if (typeof threshold === 'number') {
|
|
81
|
+
return {
|
|
82
|
+
progress: Math.max(0, tierPoint - threshold),
|
|
83
|
+
required: Math.max(1, nextTarget - threshold)
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const ratio = Math.max(0, Math.min(100, tierProgress.percentage));
|
|
87
|
+
return { progress: ratio, required: 100, progressType: 'points' };
|
|
88
|
+
}
|
|
89
|
+
export function buildProps(customer, programs, tiers) {
|
|
90
|
+
const { earnPrograms, redeemPrograms } = mapPrograms(programs);
|
|
91
|
+
const tierProgress = customer?.tierProgress ?? null;
|
|
92
|
+
const tierBar = buildTierBar(customer, tiers);
|
|
93
|
+
return {
|
|
94
|
+
'logged-in': customer != null,
|
|
95
|
+
'customer-id': customer?.id ?? '',
|
|
96
|
+
points: customer?.points ?? 0,
|
|
97
|
+
'current-tier': customer?.tier?.name ?? '',
|
|
98
|
+
'tier-id': customer?.tier?.id ?? null,
|
|
99
|
+
'next-tier': tierProgress?.nextTierName ?? '',
|
|
100
|
+
'tier-progress': tierBar.progress,
|
|
101
|
+
'tier-required': tierBar.required,
|
|
102
|
+
'progress-type': tierBar.progressType,
|
|
103
|
+
earnPrograms,
|
|
104
|
+
redeemPrograms
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export function writeProps(element, props) {
|
|
108
|
+
for (const [key, value] of Object.entries(props)) {
|
|
109
|
+
if (value === undefined || value === null)
|
|
110
|
+
continue;
|
|
111
|
+
const property = key.replace(/-([a-z])/g, (_, character) => character.toUpperCase());
|
|
112
|
+
if (/[A-Z]/.test(key)) {
|
|
113
|
+
element[property] = value;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (Array.isArray(value) || (typeof value === 'object' && value !== null)) {
|
|
117
|
+
element[property] = value;
|
|
118
|
+
}
|
|
119
|
+
else if (typeof value === 'boolean') {
|
|
120
|
+
element[property] = value;
|
|
121
|
+
element.toggleAttribute(key, value);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
element.setAttribute(key, String(value));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export function detail(event) {
|
|
129
|
+
return (event.detail ?? {});
|
|
130
|
+
}
|
|
131
|
+
const GUIDE_MODAL_STYLE_ID = 'Avada-Joy-modal-styles';
|
|
132
|
+
const GUIDE_MODAL_STYLE = `
|
|
133
|
+
#Avada-Joy-modal {
|
|
134
|
+
display: none;
|
|
135
|
+
position: fixed;
|
|
136
|
+
inset: 0;
|
|
137
|
+
z-index: 2147483647;
|
|
138
|
+
}
|
|
139
|
+
#Avada-Joy-modal.modal-show {
|
|
140
|
+
display: block;
|
|
141
|
+
}
|
|
142
|
+
#Avada-Joy-modal .Avada-modal-overlay {
|
|
143
|
+
position: fixed;
|
|
144
|
+
inset: 0;
|
|
145
|
+
background: rgba(0, 0, 0, 0.5);
|
|
146
|
+
opacity: 0.8;
|
|
147
|
+
}
|
|
148
|
+
#Avada-Joy-modal .Avada-modal-container {
|
|
149
|
+
position: fixed;
|
|
150
|
+
inset: 0;
|
|
151
|
+
display: flex;
|
|
152
|
+
align-items: center;
|
|
153
|
+
justify-content: center;
|
|
154
|
+
pointer-events: none;
|
|
155
|
+
}
|
|
156
|
+
#Avada-Joy-modal .modal-wrapper {
|
|
157
|
+
position: relative;
|
|
158
|
+
background: #fff;
|
|
159
|
+
border-radius: 12px;
|
|
160
|
+
max-width: 1000px;
|
|
161
|
+
max-height: 600px;
|
|
162
|
+
width: 90%;
|
|
163
|
+
overflow-y: auto;
|
|
164
|
+
padding: 24px;
|
|
165
|
+
pointer-events: auto;
|
|
166
|
+
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
|
167
|
+
}
|
|
168
|
+
#Avada-Joy-modal .modal-content {
|
|
169
|
+
line-height: 1.6;
|
|
170
|
+
font-size: 14px;
|
|
171
|
+
color: #333;
|
|
172
|
+
}
|
|
173
|
+
#Avada-Joy-modal .modal-content img {
|
|
174
|
+
max-width: 100%;
|
|
175
|
+
height: auto;
|
|
176
|
+
}
|
|
177
|
+
#Avada-Joy-modal .modal-close {
|
|
178
|
+
position: absolute;
|
|
179
|
+
top: 12px;
|
|
180
|
+
right: 12px;
|
|
181
|
+
width: 32px;
|
|
182
|
+
height: 32px;
|
|
183
|
+
background: rgba(0,0,0,0.05);
|
|
184
|
+
border: none;
|
|
185
|
+
border-radius: 50%;
|
|
186
|
+
font-size: 20px;
|
|
187
|
+
line-height: 1;
|
|
188
|
+
cursor: pointer;
|
|
189
|
+
display: flex;
|
|
190
|
+
align-items: center;
|
|
191
|
+
justify-content: center;
|
|
192
|
+
color: #666;
|
|
193
|
+
transition: background 0.2s;
|
|
194
|
+
}
|
|
195
|
+
#Avada-Joy-modal .modal-close:hover {
|
|
196
|
+
background: rgba(0,0,0,0.1);
|
|
197
|
+
}
|
|
198
|
+
`;
|
|
199
|
+
export async function openGuideModal(guide) {
|
|
200
|
+
if (!guide)
|
|
201
|
+
return;
|
|
202
|
+
const { default: DOMPurify } = await import('dompurify');
|
|
203
|
+
let modalEl = document.getElementById('Avada-Joy-modal');
|
|
204
|
+
if (!modalEl) {
|
|
205
|
+
modalEl = document.createElement('div');
|
|
206
|
+
modalEl.id = 'Avada-Joy-modal';
|
|
207
|
+
document.body.appendChild(modalEl);
|
|
208
|
+
}
|
|
209
|
+
const modal = modalEl;
|
|
210
|
+
const handleEsc = (ev) => {
|
|
211
|
+
if (ev.key === 'Escape')
|
|
212
|
+
closeModal();
|
|
213
|
+
};
|
|
214
|
+
const closeModal = () => {
|
|
215
|
+
modal.classList.remove('modal-show');
|
|
216
|
+
document.removeEventListener('keydown', handleEsc);
|
|
217
|
+
setTimeout(() => {
|
|
218
|
+
modal.textContent = '';
|
|
219
|
+
}, 300);
|
|
220
|
+
};
|
|
221
|
+
document.addEventListener('keydown', handleEsc);
|
|
222
|
+
const overlay = document.createElement('div');
|
|
223
|
+
overlay.className = 'Avada-modal-overlay';
|
|
224
|
+
overlay.addEventListener('click', closeModal);
|
|
225
|
+
const container = document.createElement('div');
|
|
226
|
+
container.className = 'Avada-modal-container';
|
|
227
|
+
const wrapper = document.createElement('div');
|
|
228
|
+
wrapper.className = 'modal-wrapper';
|
|
229
|
+
const content = document.createElement('div');
|
|
230
|
+
content.id = 'avada-modal-content';
|
|
231
|
+
content.className = 'modal-content';
|
|
232
|
+
const clean = DOMPurify.sanitize(guide, { USE_PROFILES: { html: true }, ADD_ATTR: ['target'] });
|
|
233
|
+
const contentTemplate = document.createElement('template');
|
|
234
|
+
contentTemplate.innerHTML = clean;
|
|
235
|
+
content.appendChild(contentTemplate.content);
|
|
236
|
+
const closeBtn = document.createElement('button');
|
|
237
|
+
closeBtn.title = 'Close (Esc)';
|
|
238
|
+
closeBtn.type = 'button';
|
|
239
|
+
closeBtn.className = 'modal-close';
|
|
240
|
+
closeBtn.textContent = '×';
|
|
241
|
+
closeBtn.addEventListener('click', closeModal);
|
|
242
|
+
wrapper.appendChild(content);
|
|
243
|
+
wrapper.appendChild(closeBtn);
|
|
244
|
+
container.appendChild(wrapper);
|
|
245
|
+
modal.textContent = '';
|
|
246
|
+
modal.appendChild(overlay);
|
|
247
|
+
modal.appendChild(container);
|
|
248
|
+
if (!document.getElementById(GUIDE_MODAL_STYLE_ID)) {
|
|
249
|
+
const style = document.createElement('style');
|
|
250
|
+
style.id = GUIDE_MODAL_STYLE_ID;
|
|
251
|
+
style.textContent = GUIDE_MODAL_STYLE;
|
|
252
|
+
document.head.appendChild(style);
|
|
253
|
+
}
|
|
254
|
+
requestAnimationFrame(() => {
|
|
255
|
+
modal.classList.add('modal-show');
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
export function createWidgetOrchestrator(container, hostAdapter, initialData, appearance, cart, coreExports, hostHooks = {}) {
|
|
259
|
+
const ctx = createOrchestratorContext({
|
|
260
|
+
container,
|
|
261
|
+
hostAdapter,
|
|
262
|
+
initialData,
|
|
263
|
+
appearance,
|
|
264
|
+
cart,
|
|
265
|
+
coreExports,
|
|
266
|
+
openCartDrawer: hostHooks.openCartDrawer
|
|
267
|
+
});
|
|
268
|
+
const { element, controller, renderContext, useBlobPath, shouldLoad, render, surface, tLabel, isAuthed, refresh } = ctx;
|
|
269
|
+
bindEvents();
|
|
270
|
+
const ready = refresh();
|
|
271
|
+
loadRecentlyViewed();
|
|
272
|
+
loadRecommended();
|
|
273
|
+
const isGuestAtMount = !initialData?.customer && !initialData?.shopifyCustomer && !hasCachedJoyToken();
|
|
274
|
+
if (isGuestAtMount)
|
|
275
|
+
ctx.loadMilestone();
|
|
276
|
+
if (shouldLoad)
|
|
277
|
+
void ready.finally(() => writeProps(element, { loading: false }));
|
|
278
|
+
const authUnsubscribe = hostAdapter.auth.subscribe(state => {
|
|
279
|
+
const wasGuest = ctx.previousStatus === 'guest';
|
|
280
|
+
const wasAuthenticated = ctx.previousStatus === 'authenticated';
|
|
281
|
+
ctx.previousStatus = state.status;
|
|
282
|
+
if (wasGuest && state.status === 'authenticated') {
|
|
283
|
+
void refresh();
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (wasAuthenticated && state.status === 'guest') {
|
|
287
|
+
revokeAndClearJoyToken({ apiBase: hostAdapter.context().apiUrl });
|
|
288
|
+
ctx.activeCustomer = undefined;
|
|
289
|
+
render(ctx.activeCustomer, initialData?.programs ?? []);
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
function onPointsUpdated(event) {
|
|
293
|
+
const points = event.detail?.points;
|
|
294
|
+
if (typeof points !== 'number' || !isAuthed())
|
|
295
|
+
return;
|
|
296
|
+
writeProps(element, { points });
|
|
297
|
+
ctx.patchCustomer({ point: points });
|
|
298
|
+
}
|
|
299
|
+
window.addEventListener(JOY_POINTS_UPDATED_EVENT, onPointsUpdated);
|
|
300
|
+
function bindEvents() {
|
|
301
|
+
const account = registerAccountHandlers(ctx);
|
|
302
|
+
const referral = registerReferralHandlers(ctx);
|
|
303
|
+
const profile = registerProfileHandlers(ctx);
|
|
304
|
+
const redeem = registerRedeemHandlers(ctx);
|
|
305
|
+
const { replaceEarnPrograms } = registerEarnHandlers(ctx);
|
|
306
|
+
registerBirthdayHandlers(ctx, { replaceEarnPrograms, buildLoaderFacade });
|
|
307
|
+
controller.on('joy-login-click', () => hostAdapter.navigate({ type: 'login' }));
|
|
308
|
+
registerMilestoneTierHandlers(ctx, { buildLoaderFacade });
|
|
309
|
+
controller.on('joy-signup-click', () => hostAdapter.navigate({ type: 'register' }));
|
|
310
|
+
controller.on('joy-host-navigate', event => {
|
|
311
|
+
const d = detail(event);
|
|
312
|
+
if (d.type === 'hash' && d.hash) {
|
|
313
|
+
hostAdapter.navigate({ type: 'hash', hash: d.hash });
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (d.type && d.type !== 'hash') {
|
|
317
|
+
hostAdapter.navigate({ type: d.type, url: d.url, view: d.view });
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (!d.url)
|
|
321
|
+
return;
|
|
322
|
+
if (isLogoutNavigation(d.url, hostAdapter.context().routesRoot)) {
|
|
323
|
+
revokeAndClearJoyToken({ apiBase: hostAdapter.context().apiUrl });
|
|
324
|
+
submitLogout(d.url);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (!isSafeNavigationUrl(d.url))
|
|
328
|
+
return;
|
|
329
|
+
if (d.sameTab)
|
|
330
|
+
hostAdapter.navigate({ type: 'external', url: d.url });
|
|
331
|
+
else
|
|
332
|
+
openInNewTab(d.url);
|
|
333
|
+
});
|
|
334
|
+
controller.on('joy-widget-toggle', event => {
|
|
335
|
+
const { isOpen } = detail(event);
|
|
336
|
+
if (typeof isOpen === 'boolean')
|
|
337
|
+
writeProps(element, { collapsed: !isOpen });
|
|
338
|
+
if (!isOpen)
|
|
339
|
+
return;
|
|
340
|
+
ctx.panelOpened = true;
|
|
341
|
+
if (ctx.preloadDeferred && isAuthed())
|
|
342
|
+
ctx.loadCustomerSections();
|
|
343
|
+
});
|
|
344
|
+
controller.on('joy-view-change', event => {
|
|
345
|
+
if (!isAuthed())
|
|
346
|
+
return;
|
|
347
|
+
const { view } = detail(event);
|
|
348
|
+
redeem.restoreAvailableCouponsOnLeave(view);
|
|
349
|
+
if (view === 'coupons')
|
|
350
|
+
redeem.openCouponsView();
|
|
351
|
+
else if (view === 'activities')
|
|
352
|
+
ctx.loadActivities();
|
|
353
|
+
else if (view === 'points-history')
|
|
354
|
+
ctx.loadActivities(undefined, undefined, 'pointHistory');
|
|
355
|
+
else if (view === 'profile')
|
|
356
|
+
profile.loadAddresses();
|
|
357
|
+
else if (view === 'referral') {
|
|
358
|
+
ctx.loadReferral();
|
|
359
|
+
referral.loadReferralHistory();
|
|
360
|
+
}
|
|
361
|
+
else if (view === 'milestone')
|
|
362
|
+
ctx.loadMilestone();
|
|
363
|
+
else if (view === 'perks')
|
|
364
|
+
ctx.loadTierPerks();
|
|
365
|
+
else if (view === 'orders')
|
|
366
|
+
account.loadOrders();
|
|
367
|
+
else if (view === 'store-credit-detail')
|
|
368
|
+
account.loadStoreCreditTransactions();
|
|
369
|
+
});
|
|
370
|
+
controller.on('joy-show-guide', event => {
|
|
371
|
+
const { guide } = detail(event);
|
|
372
|
+
if (guide)
|
|
373
|
+
void openGuideModal(guide);
|
|
374
|
+
});
|
|
375
|
+
for (const name of GATED_EVENTS) {
|
|
376
|
+
controller.on(name, event => {
|
|
377
|
+
event.preventDefault?.();
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
function buildLoaderFacade() {
|
|
382
|
+
if (!useBlobPath)
|
|
383
|
+
return null;
|
|
384
|
+
const built = buildHeadlessFacade({
|
|
385
|
+
blob: ctx.activeBlob,
|
|
386
|
+
customer: ctx.activeCustomer,
|
|
387
|
+
shopifyCustomer: initialData?.shopifyCustomer,
|
|
388
|
+
context: renderContext,
|
|
389
|
+
coreExports
|
|
390
|
+
});
|
|
391
|
+
if (!built)
|
|
392
|
+
return null;
|
|
393
|
+
const { facade } = built;
|
|
394
|
+
facade.hostAdapter.data = hostAdapter.data;
|
|
395
|
+
facade.widgetEl = element;
|
|
396
|
+
return facade;
|
|
397
|
+
}
|
|
398
|
+
function loadRecentlyViewed() {
|
|
399
|
+
const track = coreExports?.trackAndLoadRecentlyViewed;
|
|
400
|
+
const facade = typeof track === 'function' ? buildLoaderFacade() : null;
|
|
401
|
+
if (!track || !facade) {
|
|
402
|
+
element.recentlyViewedLoading = false;
|
|
403
|
+
if (!track && !warnedStaleRecentlyViewedCore) {
|
|
404
|
+
warnedStaleRecentlyViewedCore = true;
|
|
405
|
+
console.warn('[joy] recently viewed skipped: the loaded widget core has no `trackAndLoadRecentlyViewed` ' +
|
|
406
|
+
'export (CDN artifact predates it — rebuild/republish, or point `assetUrl` at a local build)');
|
|
407
|
+
}
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
surface(Promise.resolve(track(facade)));
|
|
411
|
+
}
|
|
412
|
+
function loadRecommended() {
|
|
413
|
+
const facade = buildLoaderFacade();
|
|
414
|
+
if (!facade)
|
|
415
|
+
return;
|
|
416
|
+
const settings = facade.data?.settings;
|
|
417
|
+
void loadRecommendedProducts({
|
|
418
|
+
widgetEl: element,
|
|
419
|
+
sections: Array.isArray(settings?.sections) ? settings.sections : [],
|
|
420
|
+
productCard: coreExports?.productCard,
|
|
421
|
+
currency: renderContext?.currency,
|
|
422
|
+
country: hostAdapter.context().country || undefined
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
return {
|
|
426
|
+
ready,
|
|
427
|
+
dispose: () => {
|
|
428
|
+
controller.dispose();
|
|
429
|
+
authUnsubscribe();
|
|
430
|
+
window.removeEventListener(JOY_POINTS_UPDATED_EVENT, onPointsUpdated);
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
export const JOY_POINTS_UPDATED_EVENT = 'joy:points-updated';
|
|
435
|
+
export function readEarningProgramByEvent(blob, event) {
|
|
436
|
+
const program = blob && typeof blob === 'object' ? blob.program : undefined;
|
|
437
|
+
const earning = program && typeof program === 'object' ? program.earning : undefined;
|
|
438
|
+
if (!Array.isArray(earning))
|
|
439
|
+
return undefined;
|
|
440
|
+
return earning.find(p => p?.event === event);
|
|
441
|
+
}
|
|
442
|
+
export function openInNewTab(url) {
|
|
443
|
+
if (typeof document === 'undefined')
|
|
444
|
+
return;
|
|
445
|
+
const a = document.createElement('a');
|
|
446
|
+
a.href = url;
|
|
447
|
+
a.target = '_blank';
|
|
448
|
+
a.rel = 'noopener noreferrer';
|
|
449
|
+
a.click();
|
|
450
|
+
}
|
|
451
|
+
function isSafeNavigationUrl(url) {
|
|
452
|
+
try {
|
|
453
|
+
const base = typeof location === 'undefined' ? 'https://localhost' : location.href;
|
|
454
|
+
return ['http:', 'https:', 'mailto:', 'tel:'].includes(new URL(url, base).protocol);
|
|
455
|
+
}
|
|
456
|
+
catch {
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
function isLogoutNavigation(url, routesRoot) {
|
|
461
|
+
const suffix = 'account/logout';
|
|
462
|
+
try {
|
|
463
|
+
const path = new URL(url, 'http://x/').pathname.replace(/^\/+|\/+$/g, '');
|
|
464
|
+
return path === suffix || path.endsWith(`/${suffix}`);
|
|
465
|
+
}
|
|
466
|
+
catch {
|
|
467
|
+
const expected = `${routesRoot || '/'}${suffix}`.replace(/\/+/g, '/');
|
|
468
|
+
return url === expected;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
function submitLogout(url) {
|
|
472
|
+
if (typeof document === 'undefined')
|
|
473
|
+
return;
|
|
474
|
+
const form = document.createElement('form');
|
|
475
|
+
form.method = 'POST';
|
|
476
|
+
form.action = url;
|
|
477
|
+
form.style.display = 'none';
|
|
478
|
+
document.body.appendChild(form);
|
|
479
|
+
form.submit();
|
|
480
|
+
}
|
|
481
|
+
const REFERRAL_SHOW_INVITE_STORAGE_KEY = 'joy:referral:showInvite';
|
|
482
|
+
export function readCachedReferralLink() {
|
|
483
|
+
try {
|
|
484
|
+
return typeof localStorage === 'undefined' ? false : localStorage.getItem(REFERRAL_SHOW_INVITE_STORAGE_KEY) === 'true';
|
|
485
|
+
}
|
|
486
|
+
catch {
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
export function writeCachedReferralLink(showInvite) {
|
|
491
|
+
try {
|
|
492
|
+
if (typeof localStorage === 'undefined')
|
|
493
|
+
return;
|
|
494
|
+
localStorage.setItem(REFERRAL_SHOW_INVITE_STORAGE_KEY, String(showInvite));
|
|
495
|
+
}
|
|
496
|
+
catch {
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
export function pickActiveMarketStoreCredit(accounts, activeCurrency = '') {
|
|
500
|
+
if (!accounts?.length)
|
|
501
|
+
return { balance: 0, currency: activeCurrency };
|
|
502
|
+
const matched = activeCurrency ? accounts.find(a => a.currencyCode === activeCurrency) : accounts[0];
|
|
503
|
+
return { balance: matched?.amount || 0, currency: matched?.currencyCode || activeCurrency };
|
|
504
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,123 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@joy-loyalty/hydrogen",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Joy Loyalty Widget V4 for any Hydrogen / headless Shopify storefront — host-neutral core embedded through HeadlessHostAdapter. No Weaverse dependency.",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./csp": {
|
|
17
|
+
"types": "./dist/csp.d.ts",
|
|
18
|
+
"import": "./dist/csp.js"
|
|
19
|
+
},
|
|
20
|
+
"./provider": {
|
|
21
|
+
"types": "./dist/lib/JoyProvider.d.ts",
|
|
22
|
+
"import": "./dist/lib/JoyProvider.js"
|
|
23
|
+
},
|
|
24
|
+
"./token": {
|
|
25
|
+
"types": "./dist/lib/token.d.ts",
|
|
26
|
+
"import": "./dist/lib/token.js"
|
|
27
|
+
},
|
|
28
|
+
"./points": {
|
|
29
|
+
"types": "./dist/lib/points.d.ts",
|
|
30
|
+
"import": "./dist/lib/points.js"
|
|
31
|
+
},
|
|
32
|
+
"./money": {
|
|
33
|
+
"types": "./dist/lib/money.d.ts",
|
|
34
|
+
"import": "./dist/lib/money.js"
|
|
35
|
+
},
|
|
36
|
+
"./image": {
|
|
37
|
+
"types": "./dist/lib/image.d.ts",
|
|
38
|
+
"import": "./dist/lib/image.js"
|
|
39
|
+
},
|
|
40
|
+
"./social-earn": {
|
|
41
|
+
"types": "./dist/lib/social-earn.d.ts",
|
|
42
|
+
"import": "./dist/lib/social-earn.js"
|
|
43
|
+
},
|
|
44
|
+
"./facebook-ref-link": {
|
|
45
|
+
"types": "./dist/lib/facebook-ref-link.d.ts",
|
|
46
|
+
"import": "./dist/lib/facebook-ref-link.js"
|
|
47
|
+
},
|
|
48
|
+
"./metafield-blob": {
|
|
49
|
+
"types": "./dist/lib/metafield-blob.d.ts",
|
|
50
|
+
"import": "./dist/lib/metafield-blob.js"
|
|
51
|
+
},
|
|
52
|
+
"./place-order-scope": {
|
|
53
|
+
"types": "./dist/lib/place-order-scope.d.ts",
|
|
54
|
+
"import": "./dist/lib/place-order-scope.js"
|
|
55
|
+
},
|
|
56
|
+
"./server": {
|
|
57
|
+
"types": "./dist/server/index.d.ts",
|
|
58
|
+
"import": "./dist/server/index.js"
|
|
59
|
+
},
|
|
60
|
+
"./routes/joy-token": {
|
|
61
|
+
"types": "./dist/routes/joy-token.d.ts",
|
|
62
|
+
"import": "./dist/routes/joy-token.js"
|
|
63
|
+
},
|
|
64
|
+
"./routes/joy-place-order-scope": {
|
|
65
|
+
"types": "./dist/routes/joy-place-order-scope.d.ts",
|
|
66
|
+
"import": "./dist/routes/joy-place-order-scope.js"
|
|
67
|
+
},
|
|
68
|
+
"./routes/joy-products": {
|
|
69
|
+
"types": "./dist/routes/joy-products.d.ts",
|
|
70
|
+
"import": "./dist/routes/joy-products.js"
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
"files": [
|
|
74
|
+
"dist",
|
|
75
|
+
"LICENSE",
|
|
76
|
+
"README.md"
|
|
77
|
+
],
|
|
78
|
+
"scripts": {
|
|
79
|
+
"prebuild": "npm --prefix ../joy-sdk run build",
|
|
80
|
+
"build": "tsc",
|
|
81
|
+
"prepack": "npm run build",
|
|
82
|
+
"pretypecheck": "npm --prefix ../joy-sdk run build",
|
|
83
|
+
"typecheck": "tsc --noEmit",
|
|
84
|
+
"test": "vitest run",
|
|
85
|
+
"preview:widget": "cd preview && node ../../../node_modules/vite/bin/vite.js --port 5199 --strictPort"
|
|
86
|
+
},
|
|
87
|
+
"peerDependencies": {
|
|
88
|
+
"@shopify/hydrogen": ">=2025.5",
|
|
89
|
+
"react": "^18 || ^19",
|
|
90
|
+
"react-dom": "^18 || ^19"
|
|
91
|
+
},
|
|
92
|
+
"peerDependenciesMeta": {
|
|
93
|
+
"@shopify/hydrogen": {
|
|
94
|
+
"optional": true
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
"dependencies": {
|
|
98
|
+
"@joy-loyalty/sdk": "^0.2.1",
|
|
99
|
+
"dompurify": "^3.1.7"
|
|
100
|
+
},
|
|
101
|
+
"devDependencies": {
|
|
102
|
+
"@shopify/hydrogen": "^2026.4.4",
|
|
103
|
+
"@types/react": "^19.0.0",
|
|
104
|
+
"react": "^19.0.0",
|
|
105
|
+
"react-dom": "^19.0.0",
|
|
106
|
+
"typescript": "^5.6.3",
|
|
107
|
+
"vitest": "^3.2.4"
|
|
108
|
+
},
|
|
109
|
+
"homepage": "https://joy.so",
|
|
110
|
+
"engines": {
|
|
111
|
+
"node": ">=18"
|
|
112
|
+
},
|
|
113
|
+
"keywords": [
|
|
114
|
+
"joy",
|
|
115
|
+
"loyalty",
|
|
116
|
+
"rewards",
|
|
117
|
+
"shopify",
|
|
118
|
+
"hydrogen",
|
|
119
|
+
"headless",
|
|
120
|
+
"widget",
|
|
121
|
+
"oxygen"
|
|
122
|
+
]
|
|
123
|
+
}
|