@viu/emporix-sdk-react 2.13.1 → 2.15.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/CHANGELOG.md +26 -0
- package/dist/{chunk-VMDBYVWG.js → chunk-62GKMVS3.js} +30 -4
- package/dist/chunk-62GKMVS3.js.map +1 -0
- package/dist/{chunk-SXZCM2LE.js → chunk-IREROKYI.js} +658 -311
- package/dist/chunk-IREROKYI.js.map +1 -0
- package/dist/{chunk-4YDWCA7A.js → chunk-ZNE3J25W.js} +165 -107
- package/dist/chunk-ZNE3J25W.js.map +1 -0
- package/dist/hooks.cjs +751 -430
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.cts +2 -2
- package/dist/hooks.d.ts +2 -2
- package/dist/hooks.js +185 -4
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +950 -551
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +40 -6
- package/dist/index.d.ts +40 -6
- package/dist/index.js +240 -8
- package/dist/index.js.map +1 -1
- package/dist/{provider-Bcb-tOpS.d.ts → provider-Cgm9QVoo.d.ts} +1 -1
- package/dist/{provider-Ddo7aSOI.d.cts → provider-DGjmBPMr.d.cts} +1 -1
- package/dist/provider.cjs +193 -123
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.d.cts +1 -1
- package/dist/provider.d.ts +1 -1
- package/dist/provider.js +12 -3
- package/dist/provider.js.map +1 -1
- package/dist/ssr.cjs +36 -5
- package/dist/ssr.cjs.map +1 -1
- package/dist/ssr.d.cts +22 -6
- package/dist/ssr.d.ts +22 -6
- package/dist/ssr.js +54 -1
- package/dist/ssr.js.map +1 -1
- package/dist/storage.cjs +58 -9
- package/dist/storage.cjs.map +1 -1
- package/dist/storage.d.cts +8 -2
- package/dist/storage.d.ts +8 -2
- package/dist/storage.js +15 -2
- package/dist/storage.js.map +1 -1
- package/dist/{use-returns-uZJiO46w.d.ts → use-returns-DrqdwizU.d.ts} +1 -1
- package/dist/{use-returns-DT98Ptod.d.cts → use-returns-fKPIgjmU.d.cts} +1 -1
- package/package.json +5 -4
- package/dist/chunk-4YDWCA7A.js.map +0 -1
- package/dist/chunk-SXZCM2LE.js.map +0 -1
- package/dist/chunk-TIS4BKHK.js +0 -25
- package/dist/chunk-TIS4BKHK.js.map +0 -1
- package/dist/chunk-VMDBYVWG.js.map +0 -1
package/dist/provider.cjs
CHANGED
|
@@ -1,11 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
7
20
|
|
|
8
21
|
// src/provider.tsx
|
|
22
|
+
var provider_exports = {};
|
|
23
|
+
__export(provider_exports, {
|
|
24
|
+
EmporixProvider: () => EmporixProvider,
|
|
25
|
+
EmporixSiteContext: () => EmporixSiteContext,
|
|
26
|
+
useEmporix: () => useEmporix
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(provider_exports);
|
|
29
|
+
var import_react3 = require("react");
|
|
30
|
+
var import_react_query2 = require("@tanstack/react-query");
|
|
31
|
+
var import_emporix_sdk2 = require("@viu/emporix-sdk");
|
|
9
32
|
|
|
10
33
|
// src/storage/index.ts
|
|
11
34
|
function createListenerSet() {
|
|
@@ -35,6 +58,7 @@ function createMemoryStorage(opts = {}) {
|
|
|
35
58
|
let language = null;
|
|
36
59
|
let activeLegalEntityId = null;
|
|
37
60
|
let refreshToken = null;
|
|
61
|
+
let saasToken = null;
|
|
38
62
|
const tokenListeners = /* @__PURE__ */ new Set();
|
|
39
63
|
const all = createListenerSet();
|
|
40
64
|
return {
|
|
@@ -78,17 +102,31 @@ function createMemoryStorage(opts = {}) {
|
|
|
78
102
|
refreshToken = t;
|
|
79
103
|
all.notify("refreshToken");
|
|
80
104
|
},
|
|
105
|
+
getSaasToken: () => saasToken,
|
|
106
|
+
setSaasToken: (t) => {
|
|
107
|
+
saasToken = t;
|
|
108
|
+
all.notify("saasToken");
|
|
109
|
+
},
|
|
81
110
|
subscribeAll: (l) => all.add(l)
|
|
82
111
|
};
|
|
83
112
|
}
|
|
84
|
-
|
|
113
|
+
|
|
114
|
+
// src/telemetry.ts
|
|
115
|
+
var import_react = require("react");
|
|
116
|
+
var EmporixTelemetryContext = (0, import_react.createContext)(null);
|
|
85
117
|
function useEmporixTelemetry() {
|
|
86
|
-
const ctx =
|
|
118
|
+
const ctx = (0, import_react.useContext)(EmporixTelemetryContext);
|
|
87
119
|
if (!ctx) {
|
|
88
120
|
throw new Error("useEmporixTelemetry must be used within an EmporixProvider");
|
|
89
121
|
}
|
|
90
122
|
return ctx;
|
|
91
123
|
}
|
|
124
|
+
|
|
125
|
+
// src/company-context.tsx
|
|
126
|
+
var import_react2 = require("react");
|
|
127
|
+
var import_react_query = require("@tanstack/react-query");
|
|
128
|
+
var import_emporix_sdk = require("@viu/emporix-sdk");
|
|
129
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
92
130
|
var NULL_CTX = {
|
|
93
131
|
activeCompany: null,
|
|
94
132
|
myCompanies: [],
|
|
@@ -101,90 +139,110 @@ var NULL_CTX = {
|
|
|
101
139
|
refetchMyCompanies: async () => {
|
|
102
140
|
}
|
|
103
141
|
};
|
|
104
|
-
var EmporixCompanyContext =
|
|
142
|
+
var EmporixCompanyContext = (0, import_react2.createContext)(NULL_CTX);
|
|
105
143
|
function CompanyContextProvider({
|
|
106
144
|
client,
|
|
107
145
|
storage,
|
|
108
146
|
initialActiveLegalEntityId,
|
|
109
147
|
children
|
|
110
148
|
}) {
|
|
111
|
-
const qc =
|
|
149
|
+
const qc = (0, import_react_query.useQueryClient)();
|
|
112
150
|
const { emit } = useEmporixTelemetry();
|
|
113
|
-
const [myCompanies, setMyCompanies] =
|
|
114
|
-
const [activeCompany, setActive] =
|
|
115
|
-
const [status, setStatus] =
|
|
116
|
-
const [error, setError] =
|
|
117
|
-
const activeRef =
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
storage.
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
)
|
|
151
|
+
const [myCompanies, setMyCompanies] = (0, import_react2.useState)([]);
|
|
152
|
+
const [activeCompany, setActive] = (0, import_react2.useState)(null);
|
|
153
|
+
const [status, setStatus] = (0, import_react2.useState)("idle");
|
|
154
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
155
|
+
const activeRef = (0, import_react2.useRef)(null);
|
|
156
|
+
(0, import_react2.useEffect)(() => {
|
|
157
|
+
activeRef.current = activeCompany;
|
|
158
|
+
}, [activeCompany]);
|
|
159
|
+
const switchChain = (0, import_react2.useRef)(Promise.resolve());
|
|
160
|
+
const switchTo = (0, import_react2.useCallback)(
|
|
161
|
+
(target) => {
|
|
162
|
+
const run = async () => {
|
|
163
|
+
const start = Date.now();
|
|
164
|
+
const from = activeRef.current?.id ?? null;
|
|
165
|
+
const refreshToken = storage.getRefreshToken();
|
|
166
|
+
const token = storage.getCustomerToken();
|
|
167
|
+
if (!refreshToken || !token) {
|
|
168
|
+
setActive(target);
|
|
169
|
+
storage.setActiveLegalEntityId(target?.id ?? null);
|
|
170
|
+
} else {
|
|
171
|
+
const next = await client.customers.refresh({
|
|
172
|
+
refreshToken,
|
|
173
|
+
...target ? { legalEntityId: target.id } : {}
|
|
174
|
+
});
|
|
175
|
+
storage.setCustomerToken(next.customerToken);
|
|
176
|
+
if (next.refreshToken) storage.setRefreshToken(next.refreshToken);
|
|
177
|
+
storage.setCartId(null);
|
|
178
|
+
storage.setActiveLegalEntityId(target?.id ?? null);
|
|
179
|
+
setActive(target);
|
|
180
|
+
qc.invalidateQueries({
|
|
181
|
+
predicate: (q) => Array.isArray(q.queryKey) && q.queryKey.some(
|
|
182
|
+
(k) => k === "cart" || k === "companies" || k === "customer" || k === from || target !== null && k === target.id
|
|
183
|
+
)
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
emit({
|
|
187
|
+
type: "company:switched",
|
|
188
|
+
from,
|
|
189
|
+
to: target?.id ?? null,
|
|
190
|
+
durationMs: Date.now() - start
|
|
142
191
|
});
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
from,
|
|
147
|
-
to: target?.id ?? null,
|
|
148
|
-
durationMs: Date.now() - start
|
|
192
|
+
};
|
|
193
|
+
const task = switchChain.current.then(run, run);
|
|
194
|
+
switchChain.current = task.catch(() => {
|
|
149
195
|
});
|
|
196
|
+
return task;
|
|
150
197
|
},
|
|
151
198
|
[client, storage, qc, emit]
|
|
152
199
|
);
|
|
153
|
-
const load =
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
setStatus("loading");
|
|
162
|
-
try {
|
|
163
|
-
const companies = await client.companies.listMine(emporixSdk.auth.customer(token));
|
|
164
|
-
setMyCompanies(companies);
|
|
165
|
-
const persisted = initialActiveLegalEntityId ?? storage.getActiveLegalEntityId();
|
|
166
|
-
const matched = persisted ? companies.find((c) => c.id === persisted) ?? null : null;
|
|
167
|
-
if (matched) {
|
|
168
|
-
setActive(matched);
|
|
169
|
-
if (storage.getActiveLegalEntityId() !== matched.id) {
|
|
170
|
-
storage.setActiveLegalEntityId(matched.id ?? null);
|
|
171
|
-
}
|
|
172
|
-
} else if (companies.length === 1) {
|
|
173
|
-
await switchTo(companies[0] ?? null);
|
|
174
|
-
} else {
|
|
200
|
+
const load = (0, import_react2.useCallback)(
|
|
201
|
+
async (signal) => {
|
|
202
|
+
const token = storage.getCustomerToken();
|
|
203
|
+
if (!token) {
|
|
204
|
+
if (signal?.cancelled) return;
|
|
205
|
+
setMyCompanies([]);
|
|
175
206
|
setActive(null);
|
|
176
|
-
|
|
207
|
+
setStatus("idle");
|
|
208
|
+
return;
|
|
177
209
|
}
|
|
178
|
-
setStatus("
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
210
|
+
setStatus("loading");
|
|
211
|
+
try {
|
|
212
|
+
const companies = await client.companies.listMine(import_emporix_sdk.auth.customer(token));
|
|
213
|
+
if (signal?.cancelled) return;
|
|
214
|
+
setMyCompanies(companies);
|
|
215
|
+
const persisted = initialActiveLegalEntityId ?? storage.getActiveLegalEntityId();
|
|
216
|
+
const matched = persisted ? companies.find((c) => c.id === persisted) ?? null : null;
|
|
217
|
+
if (matched) {
|
|
218
|
+
setActive(matched);
|
|
219
|
+
if (storage.getActiveLegalEntityId() !== matched.id) {
|
|
220
|
+
storage.setActiveLegalEntityId(matched.id ?? null);
|
|
221
|
+
}
|
|
222
|
+
} else if (companies.length === 1) {
|
|
223
|
+
await switchTo(companies[0] ?? null);
|
|
224
|
+
} else {
|
|
225
|
+
setActive(null);
|
|
226
|
+
if (persisted && !matched) storage.setActiveLegalEntityId(null);
|
|
227
|
+
}
|
|
228
|
+
if (signal?.cancelled) return;
|
|
229
|
+
setStatus("idle");
|
|
230
|
+
} catch (e) {
|
|
231
|
+
if (signal?.cancelled) return;
|
|
232
|
+
setError(e);
|
|
233
|
+
setStatus("error");
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
[client, storage, initialActiveLegalEntityId, switchTo]
|
|
237
|
+
);
|
|
238
|
+
(0, import_react2.useEffect)(() => {
|
|
239
|
+
const signal = { cancelled: false };
|
|
240
|
+
void load(signal);
|
|
241
|
+
return () => {
|
|
242
|
+
signal.cancelled = true;
|
|
243
|
+
};
|
|
186
244
|
}, [load]);
|
|
187
|
-
|
|
245
|
+
(0, import_react2.useEffect)(() => {
|
|
188
246
|
let prev = storage.getCustomerToken();
|
|
189
247
|
return storage.subscribe?.((next) => {
|
|
190
248
|
const becameAuth = !prev && next;
|
|
@@ -193,7 +251,7 @@ function CompanyContextProvider({
|
|
|
193
251
|
if (becameAuth || becameUnauth) void load();
|
|
194
252
|
});
|
|
195
253
|
}, [storage, load]);
|
|
196
|
-
const setActiveCompany =
|
|
254
|
+
const setActiveCompany = (0, import_react2.useCallback)(
|
|
197
255
|
async (legalEntityId) => {
|
|
198
256
|
setStatus("switching");
|
|
199
257
|
try {
|
|
@@ -213,7 +271,7 @@ function CompanyContextProvider({
|
|
|
213
271
|
},
|
|
214
272
|
[myCompanies, switchTo]
|
|
215
273
|
);
|
|
216
|
-
const value =
|
|
274
|
+
const value = (0, import_react2.useMemo)(() => {
|
|
217
275
|
const mode = activeCompany ? "b2b" : myCompanies.length > 1 ? "unresolved" : "b2c";
|
|
218
276
|
return {
|
|
219
277
|
activeCompany,
|
|
@@ -225,10 +283,13 @@ function CompanyContextProvider({
|
|
|
225
283
|
refetchMyCompanies: load
|
|
226
284
|
};
|
|
227
285
|
}, [activeCompany, myCompanies, status, error, setActiveCompany, load]);
|
|
228
|
-
return /* @__PURE__ */
|
|
286
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(EmporixCompanyContext.Provider, { value, children });
|
|
229
287
|
}
|
|
230
|
-
|
|
231
|
-
|
|
288
|
+
|
|
289
|
+
// src/provider.tsx
|
|
290
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
291
|
+
var EmporixContext = (0, import_react3.createContext)(null);
|
|
292
|
+
var EmporixSiteContext = (0, import_react3.createContext)(null);
|
|
232
293
|
var DEFAULT_QUERY_OPTIONS = {
|
|
233
294
|
staleTime: 3e4,
|
|
234
295
|
refetchOnWindowFocus: false,
|
|
@@ -247,27 +308,35 @@ function EmporixProvider({
|
|
|
247
308
|
onCustomerSessionExpired,
|
|
248
309
|
children
|
|
249
310
|
}) {
|
|
250
|
-
const value =
|
|
311
|
+
const value = (0, import_react3.useMemo)(() => {
|
|
251
312
|
const s = storage ?? createMemoryStorage(
|
|
252
313
|
initialCustomerToken !== void 0 ? { initial: initialCustomerToken } : {}
|
|
253
314
|
);
|
|
254
|
-
if (initialCustomerToken && storage && storage.getCustomerToken() === null) {
|
|
255
|
-
storage.setCustomerToken(initialCustomerToken);
|
|
256
|
-
}
|
|
257
315
|
return { client, storage: s };
|
|
258
316
|
}, [client, storage, initialCustomerToken]);
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
)
|
|
263
|
-
|
|
317
|
+
const [fallbackQc] = (0, import_react3.useState)(() => new import_react_query2.QueryClient());
|
|
318
|
+
const qc = queryClient ?? fallbackQc;
|
|
319
|
+
const defaultsRef = (0, import_react3.useRef)(null);
|
|
320
|
+
if (defaultsRef.current !== qc) {
|
|
321
|
+
qc.setQueryDefaults(["emporix"], {
|
|
322
|
+
...DEFAULT_QUERY_OPTIONS,
|
|
323
|
+
...qc.getDefaultOptions().queries,
|
|
324
|
+
...qc.getQueryDefaults(["emporix"])
|
|
325
|
+
});
|
|
326
|
+
defaultsRef.current = qc;
|
|
327
|
+
}
|
|
328
|
+
const wiredRef = (0, import_react3.useRef)(null);
|
|
329
|
+
if (wiredRef.current?.client !== client || wiredRef.current?.storage !== value.storage) {
|
|
264
330
|
client.tokenProvider.attachAnonymousStore?.({
|
|
265
331
|
read: () => value.storage.getAnonymousSession(),
|
|
266
332
|
write: (s) => value.storage.setAnonymousSession(s)
|
|
267
333
|
});
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
334
|
+
if (initialCustomerToken && storage && storage.getCustomerToken() === null) {
|
|
335
|
+
storage.setCustomerToken(initialCustomerToken);
|
|
336
|
+
}
|
|
337
|
+
wiredRef.current = { client, storage: value.storage };
|
|
338
|
+
}
|
|
339
|
+
const safeEmit = (0, import_react3.useCallback)(
|
|
271
340
|
(event) => {
|
|
272
341
|
if (!onTelemetry) return;
|
|
273
342
|
try {
|
|
@@ -278,8 +347,8 @@ function EmporixProvider({
|
|
|
278
347
|
},
|
|
279
348
|
[onTelemetry]
|
|
280
349
|
);
|
|
281
|
-
const telemetryValue =
|
|
282
|
-
|
|
350
|
+
const telemetryValue = (0, import_react3.useMemo)(() => ({ emit: safeEmit }), [safeEmit]);
|
|
351
|
+
(0, import_react3.useEffect)(() => {
|
|
283
352
|
if (!onTelemetry) return;
|
|
284
353
|
const startedAt = /* @__PURE__ */ new Map();
|
|
285
354
|
const unsubQuery = qc.getQueryCache().subscribe((event) => {
|
|
@@ -358,7 +427,7 @@ function EmporixProvider({
|
|
|
358
427
|
unsubStorage?.();
|
|
359
428
|
};
|
|
360
429
|
}, [qc, onTelemetry, client, value.storage, safeEmit]);
|
|
361
|
-
|
|
430
|
+
(0, import_react3.useEffect)(() => {
|
|
362
431
|
if (!autoRefreshCustomerToken) return;
|
|
363
432
|
const storage2 = value.storage;
|
|
364
433
|
client.setCustomerTokenRefresher({
|
|
@@ -388,14 +457,14 @@ function EmporixProvider({
|
|
|
388
457
|
});
|
|
389
458
|
return () => client.setCustomerTokenRefresher(null);
|
|
390
459
|
}, [autoRefreshCustomerToken, client, value.storage, safeEmit, onCustomerSessionExpired]);
|
|
391
|
-
return /* @__PURE__ */
|
|
460
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(EmporixContext.Provider, { value, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(EmporixTelemetryContext.Provider, { value: telemetryValue, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_query2.QueryClientProvider, { client: qc, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
392
461
|
SiteContextProvider,
|
|
393
462
|
{
|
|
394
463
|
client,
|
|
395
464
|
storage: value.storage,
|
|
396
465
|
...initialSiteCode !== void 0 ? { initialSiteCode } : {},
|
|
397
466
|
...initialLanguage !== void 0 ? { initialLanguage } : {},
|
|
398
|
-
children: /* @__PURE__ */
|
|
467
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
399
468
|
CompanyContextProvider,
|
|
400
469
|
{
|
|
401
470
|
client,
|
|
@@ -414,30 +483,30 @@ function SiteContextProvider({
|
|
|
414
483
|
initialLanguage,
|
|
415
484
|
children
|
|
416
485
|
}) {
|
|
417
|
-
const qc =
|
|
418
|
-
const [siteCode, setSiteCodeState] =
|
|
486
|
+
const qc = (0, import_react_query2.useQueryClient)();
|
|
487
|
+
const [siteCode, setSiteCodeState] = (0, import_react3.useState)(() => {
|
|
419
488
|
if (initialSiteCode !== void 0) return initialSiteCode;
|
|
420
489
|
const fromStorage = storage.getSiteCode();
|
|
421
490
|
if (fromStorage !== null) return fromStorage;
|
|
422
491
|
return client.config?.credentials?.storefront?.context?.siteCode ?? null;
|
|
423
492
|
});
|
|
424
|
-
const [currency, setCurrencyState] =
|
|
493
|
+
const [currency, setCurrencyState] = (0, import_react3.useState)(
|
|
425
494
|
() => client.config?.credentials?.storefront?.context?.currency ?? null
|
|
426
495
|
);
|
|
427
|
-
const [language, setLanguageState] =
|
|
496
|
+
const [language, setLanguageState] = (0, import_react3.useState)(() => {
|
|
428
497
|
if (initialLanguage !== void 0) return initialLanguage;
|
|
429
498
|
const fromStorage = storage.getLanguage();
|
|
430
499
|
if (fromStorage !== null) return fromStorage;
|
|
431
500
|
return client.config?.credentials?.storefront?.context?.language ?? null;
|
|
432
501
|
});
|
|
433
|
-
const [targetLocation, setTargetLocation] =
|
|
434
|
-
const [isSwitching, setIsSwitching] =
|
|
435
|
-
const [switchError, setSwitchError] =
|
|
436
|
-
|
|
502
|
+
const [targetLocation, setTargetLocation] = (0, import_react3.useState)(null);
|
|
503
|
+
const [isSwitching, setIsSwitching] = (0, import_react3.useState)(false);
|
|
504
|
+
const [switchError, setSwitchError] = (0, import_react3.useState)(null);
|
|
505
|
+
(0, import_react3.useEffect)(() => {
|
|
437
506
|
if (!siteCode || currency !== null && targetLocation !== null && language !== null) return;
|
|
438
507
|
let cancelled = false;
|
|
439
508
|
const token = storage.getCustomerToken();
|
|
440
|
-
const authCtx = token ?
|
|
509
|
+
const authCtx = token ? import_emporix_sdk2.auth.customer(token) : import_emporix_sdk2.auth.anonymous();
|
|
441
510
|
qc.fetchQuery({
|
|
442
511
|
queryKey: [
|
|
443
512
|
"emporix",
|
|
@@ -461,10 +530,10 @@ function SiteContextProvider({
|
|
|
461
530
|
cancelled = true;
|
|
462
531
|
};
|
|
463
532
|
}, [siteCode]);
|
|
464
|
-
|
|
533
|
+
(0, import_react3.useEffect)(() => {
|
|
465
534
|
if (language) client.setStorefrontContext({ language });
|
|
466
535
|
}, []);
|
|
467
|
-
const setSite =
|
|
536
|
+
const setSite = (0, import_react3.useCallback)(
|
|
468
537
|
async (code) => {
|
|
469
538
|
storage.setSiteCode(code);
|
|
470
539
|
storage.setCartId(null);
|
|
@@ -479,7 +548,7 @@ function SiteContextProvider({
|
|
|
479
548
|
setIsSwitching(true);
|
|
480
549
|
try {
|
|
481
550
|
const token = storage.getCustomerToken();
|
|
482
|
-
const authCtx = token ?
|
|
551
|
+
const authCtx = token ? import_emporix_sdk2.auth.customer(token) : import_emporix_sdk2.auth.anonymous();
|
|
483
552
|
const site = await qc.fetchQuery({
|
|
484
553
|
queryKey: [
|
|
485
554
|
"emporix",
|
|
@@ -514,7 +583,7 @@ function SiteContextProvider({
|
|
|
514
583
|
},
|
|
515
584
|
[client, storage, qc, language]
|
|
516
585
|
);
|
|
517
|
-
const setCurrency =
|
|
586
|
+
const setCurrency = (0, import_react3.useCallback)(
|
|
518
587
|
async (next) => {
|
|
519
588
|
storage.setCartId(null);
|
|
520
589
|
setCurrencyState(next);
|
|
@@ -524,7 +593,7 @@ function SiteContextProvider({
|
|
|
524
593
|
setIsSwitching(true);
|
|
525
594
|
try {
|
|
526
595
|
const token = storage.getCustomerToken();
|
|
527
|
-
const authCtx = token ?
|
|
596
|
+
const authCtx = token ? import_emporix_sdk2.auth.customer(token) : import_emporix_sdk2.auth.anonymous();
|
|
528
597
|
await client.sessionContext.patch(
|
|
529
598
|
{ currency: next, ...siteCode ? { siteCode } : {} },
|
|
530
599
|
authCtx
|
|
@@ -537,7 +606,7 @@ function SiteContextProvider({
|
|
|
537
606
|
},
|
|
538
607
|
[client, storage, qc, siteCode]
|
|
539
608
|
);
|
|
540
|
-
const setLanguage =
|
|
609
|
+
const setLanguage = (0, import_react3.useCallback)(
|
|
541
610
|
async (next) => {
|
|
542
611
|
storage.setLanguage(next);
|
|
543
612
|
setLanguageState(next);
|
|
@@ -547,7 +616,7 @@ function SiteContextProvider({
|
|
|
547
616
|
setIsSwitching(true);
|
|
548
617
|
try {
|
|
549
618
|
const token = storage.getCustomerToken();
|
|
550
|
-
const authCtx = token ?
|
|
619
|
+
const authCtx = token ? import_emporix_sdk2.auth.customer(token) : import_emporix_sdk2.auth.anonymous();
|
|
551
620
|
await client.sessionContext.patch(
|
|
552
621
|
{ language: next, ...siteCode ? { siteCode } : {} },
|
|
553
622
|
authCtx
|
|
@@ -560,7 +629,7 @@ function SiteContextProvider({
|
|
|
560
629
|
},
|
|
561
630
|
[client, storage, qc, siteCode]
|
|
562
631
|
);
|
|
563
|
-
const value =
|
|
632
|
+
const value = (0, import_react3.useMemo)(
|
|
564
633
|
() => ({
|
|
565
634
|
siteCode,
|
|
566
635
|
currency,
|
|
@@ -574,16 +643,17 @@ function SiteContextProvider({
|
|
|
574
643
|
}),
|
|
575
644
|
[siteCode, currency, targetLocation, language, setSite, setCurrency, setLanguage, isSwitching, switchError]
|
|
576
645
|
);
|
|
577
|
-
return /* @__PURE__ */
|
|
646
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(EmporixSiteContext.Provider, { value, children });
|
|
578
647
|
}
|
|
579
648
|
function useEmporix() {
|
|
580
|
-
const ctx =
|
|
649
|
+
const ctx = (0, import_react3.useContext)(EmporixContext);
|
|
581
650
|
if (!ctx) throw new Error("useEmporix must be used within an EmporixProvider");
|
|
582
651
|
return ctx;
|
|
583
652
|
}
|
|
584
|
-
|
|
585
|
-
exports
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
653
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
654
|
+
0 && (module.exports = {
|
|
655
|
+
EmporixProvider,
|
|
656
|
+
EmporixSiteContext,
|
|
657
|
+
useEmporix
|
|
658
|
+
});
|
|
589
659
|
//# sourceMappingURL=provider.cjs.map
|