@simpleapps-com/augur-hooks 2026.11.4 → 2026.11.6
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 +7 -1
- package/dist/checkout/create-checkout-provider.d.ts.map +1 -1
- package/dist/checkout/index.cjs +35 -22
- package/dist/checkout/index.cjs.map +1 -1
- package/dist/checkout/index.js +22 -9
- package/dist/checkout/index.js.map +1 -1
- package/dist/{chunk-WJ6VEQ6R.cjs → chunk-4PTXISXM.cjs} +67 -2
- package/dist/chunk-4PTXISXM.cjs.map +1 -0
- package/dist/{chunk-63ZJ4UTX.js → chunk-V3VBNWLM.js} +66 -1
- package/dist/chunk-V3VBNWLM.js.map +1 -0
- package/dist/hooks/use-cart-actions.d.ts +21 -8
- package/dist/hooks/use-cart-actions.d.ts.map +1 -1
- package/dist/hooks/use-cart-initialization.d.ts +7 -0
- package/dist/hooks/use-cart-initialization.d.ts.map +1 -1
- package/dist/hooks/use-cart-pricing.d.ts +30 -8
- package/dist/hooks/use-cart-pricing.d.ts.map +1 -1
- package/dist/hooks/use-format-price.d.ts.map +1 -1
- package/dist/hooks/use-modal-lifecycle.d.ts.map +1 -1
- package/dist/index.cjs +277 -130
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +251 -104
- package/dist/index.js.map +1 -1
- package/dist/rules.d.ts +99 -0
- package/dist/rules.d.ts.map +1 -1
- package/dist/stores/cart-store.d.ts +11 -1
- package/dist/stores/cart-store.d.ts.map +1 -1
- package/package.json +3 -3
- package/dist/chunk-63ZJ4UTX.js.map +0 -1
- package/dist/chunk-WJ6VEQ6R.cjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -4,21 +4,32 @@ import {
|
|
|
4
4
|
registerQueryAction
|
|
5
5
|
} from "./chunk-7SUD5Z2V.js";
|
|
6
6
|
import {
|
|
7
|
+
acceptsLookup,
|
|
8
|
+
addAccepted,
|
|
9
|
+
bodyOverflow,
|
|
10
|
+
cartIdle,
|
|
7
11
|
clearCheckoutPersistence,
|
|
8
12
|
clearFromSession,
|
|
13
|
+
closesOnKey,
|
|
9
14
|
determineOrderReadiness,
|
|
10
15
|
hasNumberFrom,
|
|
11
16
|
indexOfUidFrom,
|
|
17
|
+
indexOfUnitFrom,
|
|
18
|
+
initStepOf,
|
|
12
19
|
keepsAfterReverse,
|
|
13
20
|
keyPathFrom,
|
|
14
21
|
leadingTextsFrom,
|
|
22
|
+
lineUnitPrice,
|
|
15
23
|
loadFromSession,
|
|
16
24
|
qtyOrZero,
|
|
17
25
|
requiredCartUid,
|
|
26
|
+
rollsBack,
|
|
18
27
|
saveToSession,
|
|
28
|
+
sendsPrice,
|
|
19
29
|
shouldPersistOf,
|
|
20
30
|
stockEnabled,
|
|
21
31
|
stockRetryDelay,
|
|
32
|
+
toastShown,
|
|
22
33
|
useCartHdrUid,
|
|
23
34
|
useCartLineCount,
|
|
24
35
|
useCartLines,
|
|
@@ -30,8 +41,9 @@ import {
|
|
|
30
41
|
useOrderReadiness,
|
|
31
42
|
usePaymentFlow,
|
|
32
43
|
useSetCartHdrUid,
|
|
33
|
-
useSetCartLines
|
|
34
|
-
|
|
44
|
+
useSetCartLines,
|
|
45
|
+
zeroLabelShown
|
|
46
|
+
} from "./chunk-V3VBNWLM.js";
|
|
35
47
|
|
|
36
48
|
// src/provider.tsx
|
|
37
49
|
import { createContext, useContext, useMemo } from "react";
|
|
@@ -157,7 +169,7 @@ function useFormatPrice(optionsOrLocale, legacyCurrency) {
|
|
|
157
169
|
} = resolveFormatting(opts, site?.formatting);
|
|
158
170
|
const formatPrice = (price, callOptions) => {
|
|
159
171
|
const effectiveValue = cleanMoney(price * (callOptions?.quantity ?? 1));
|
|
160
|
-
if (effectiveValue === 0
|
|
172
|
+
if (zeroLabelShown(effectiveValue === 0, zeroLabel !== void 0)) {
|
|
161
173
|
return zeroLabel;
|
|
162
174
|
}
|
|
163
175
|
const precision = callOptions?.precision ?? basePrecision;
|
|
@@ -208,9 +220,13 @@ function useStock(invMastUid, options = {}) {
|
|
|
208
220
|
// src/hooks/use-cart-actions.ts
|
|
209
221
|
import { useCallback } from "react";
|
|
210
222
|
import { useQueryClient } from "@tanstack/react-query";
|
|
211
|
-
|
|
223
|
+
import { lineHasQuantity } from "@simpleapps-com/augur-utils";
|
|
224
|
+
function cartUidOf(cartHdrUid) {
|
|
212
225
|
const num = typeof cartHdrUid === "string" ? Number(cartHdrUid) : cartHdrUid;
|
|
213
|
-
|
|
226
|
+
return num === void 0 || Number.isNaN(num) ? void 0 : num;
|
|
227
|
+
}
|
|
228
|
+
function requireCartUid(cartHdrUid) {
|
|
229
|
+
const uid = requiredCartUid(cartUidOf(cartHdrUid));
|
|
214
230
|
if (uid === null) throw new Error("Cart UID is not set");
|
|
215
231
|
return uid;
|
|
216
232
|
}
|
|
@@ -243,20 +259,45 @@ function buildOptimisticAdd(cartLines, item, cartHdrUid, itemId) {
|
|
|
243
259
|
];
|
|
244
260
|
}
|
|
245
261
|
function reverseAdd(cartLines, invMastUid, quantity) {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
262
|
+
const index = indexOfUidFrom(uidsOf(cartLines), invMastUid, 0);
|
|
263
|
+
if (index < 0) return cartLines;
|
|
264
|
+
const line = cartLines[index];
|
|
265
|
+
if (!keepsAfterReverse(line.quantity, quantity)) {
|
|
266
|
+
return [...cartLines.slice(0, index), ...cartLines.slice(index + 1)];
|
|
267
|
+
}
|
|
268
|
+
return cartLines.map(
|
|
269
|
+
(l, i) => i === index ? { ...l, quantity: qtyOrZero(l.quantity) - quantity } : l
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
function targetOf(cartLines, invMastUid, unitOfMeasure) {
|
|
273
|
+
const first = indexOfUidFrom(uidsOf(cartLines), invMastUid, 0);
|
|
274
|
+
if (first < 0) return null;
|
|
275
|
+
const unit = unitOfMeasure ?? unitOf(cartLines[first]);
|
|
276
|
+
if (indexOfUnitFrom(uidsOf(cartLines), unitsOf(cartLines), invMastUid, unit, 0) < 0) return null;
|
|
277
|
+
return { invMastUid, unit };
|
|
250
278
|
}
|
|
251
|
-
function
|
|
252
|
-
|
|
279
|
+
function isTarget(line, target) {
|
|
280
|
+
return indexOfUnitFrom([line.invMastUid], [unitOf(line)], target.invMastUid, target.unit, 0) === 0;
|
|
281
|
+
}
|
|
282
|
+
function targetLine(cartLines, target) {
|
|
283
|
+
const index = indexOfUnitFrom(
|
|
284
|
+
uidsOf(cartLines),
|
|
285
|
+
unitsOf(cartLines),
|
|
286
|
+
target.invMastUid,
|
|
287
|
+
target.unit,
|
|
288
|
+
0
|
|
289
|
+
);
|
|
290
|
+
return cartLines[index];
|
|
291
|
+
}
|
|
292
|
+
function reverseQuantity(cartLines, target, previousQuantity) {
|
|
253
293
|
return cartLines.map(
|
|
254
|
-
(line) => line
|
|
294
|
+
(line) => isTarget(line, target) ? { ...line, quantity: previousQuantity } : line
|
|
255
295
|
);
|
|
256
296
|
}
|
|
257
|
-
function restoreRemoved(cartLines,
|
|
258
|
-
if (
|
|
259
|
-
|
|
297
|
+
function restoreRemoved(cartLines, target, removedLine) {
|
|
298
|
+
if (indexOfUnitFrom(uidsOf(cartLines), unitsOf(cartLines), target.invMastUid, target.unit, 0) >= 0) {
|
|
299
|
+
return cartLines;
|
|
300
|
+
}
|
|
260
301
|
return [...cartLines, removedLine];
|
|
261
302
|
}
|
|
262
303
|
function mergeRestore(cartLines, snapshot) {
|
|
@@ -266,26 +307,42 @@ function mergeRestore(cartLines, snapshot) {
|
|
|
266
307
|
}
|
|
267
308
|
return merged;
|
|
268
309
|
}
|
|
310
|
+
function sentLine(line, quantity) {
|
|
311
|
+
const { unitPrice, ...rest } = line;
|
|
312
|
+
const sent = { ...rest, quantity, lineNo: 0 };
|
|
313
|
+
if (sendsPrice(unitPrice)) sent.unitPrice = unitPrice;
|
|
314
|
+
return sent;
|
|
315
|
+
}
|
|
269
316
|
function uidsOf(cartLines) {
|
|
270
317
|
return cartLines.map((line) => line.invMastUid);
|
|
271
318
|
}
|
|
319
|
+
function unitOf(line) {
|
|
320
|
+
return line.unitOfMeasure ?? "";
|
|
321
|
+
}
|
|
322
|
+
function unitsOf(cartLines) {
|
|
323
|
+
return cartLines.map(unitOf);
|
|
324
|
+
}
|
|
272
325
|
function sendToast(notify, kind, showToast, message) {
|
|
273
|
-
if (showToast
|
|
326
|
+
if (toastShown(showToast, message !== void 0)) notify?.[kind]?.(message);
|
|
274
327
|
}
|
|
275
328
|
async function settleCartOp(op, options, notify, invalidate) {
|
|
329
|
+
const { endCartOp } = useCartStore.getState();
|
|
276
330
|
try {
|
|
277
331
|
const success = await op.call();
|
|
278
332
|
if (!success) throw new Error(op.failure);
|
|
279
|
-
invalidate();
|
|
280
|
-
sendToast(notify, "info", op.showToast, op.infoMessage);
|
|
281
|
-
options.onSuccess?.();
|
|
282
|
-
return true;
|
|
283
333
|
} catch (error) {
|
|
284
|
-
op.rollback();
|
|
334
|
+
if (rollsBack(cartUidOf(useCartStore.getState().cartHdrUid), op.cartUid)) op.rollback();
|
|
335
|
+
endCartOp();
|
|
336
|
+
invalidate();
|
|
285
337
|
sendToast(notify, "error", op.showToast, op.errorMessage);
|
|
286
338
|
options.onError?.(error);
|
|
287
339
|
return false;
|
|
288
340
|
}
|
|
341
|
+
endCartOp();
|
|
342
|
+
invalidate();
|
|
343
|
+
sendToast(notify, "info", op.showToast, op.infoMessage);
|
|
344
|
+
options.onSuccess?.();
|
|
345
|
+
return true;
|
|
289
346
|
}
|
|
290
347
|
function useCartActions(callbacks) {
|
|
291
348
|
const queryClient = useQueryClient();
|
|
@@ -294,7 +351,7 @@ function useCartActions(callbacks) {
|
|
|
294
351
|
const setCartLines = useSetCartLines();
|
|
295
352
|
const notify = callbacks.toast;
|
|
296
353
|
const invalidateCartCache = useCallback(() => {
|
|
297
|
-
void queryClient.invalidateQueries({ queryKey: ["cartLines"
|
|
354
|
+
void queryClient.invalidateQueries({ queryKey: ["cartLines"] });
|
|
298
355
|
void queryClient.invalidateQueries({
|
|
299
356
|
queryKey: ["cart-also-bought", cartHdrUid]
|
|
300
357
|
});
|
|
@@ -302,10 +359,18 @@ function useCartActions(callbacks) {
|
|
|
302
359
|
const addToCart = useCallback(
|
|
303
360
|
async (item, options = {}) => {
|
|
304
361
|
const { showToast = true, itemId = "" } = options;
|
|
362
|
+
const quantity = Number.isFinite(item.quantity) ? item.quantity : null;
|
|
363
|
+
if (!addAccepted(item.unitOfMeasure, quantity)) {
|
|
364
|
+
sendToast(notify, "error", showToast, "Choose a quantity and a unit of measure.");
|
|
365
|
+
options.onError?.(new Error("Quantity and unit of measure are required"));
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
305
368
|
const { uid, lines: liveLines } = liveCart();
|
|
369
|
+
useCartStore.getState().beginCartOp();
|
|
306
370
|
setCartLines(buildOptimisticAdd(liveLines, item, uid, itemId));
|
|
307
371
|
return settleCartOp(
|
|
308
372
|
{
|
|
373
|
+
cartUid: uid,
|
|
309
374
|
call: () => callbacks.addToCart(uid, [
|
|
310
375
|
{
|
|
311
376
|
cartHdrUid: uid,
|
|
@@ -331,21 +396,30 @@ function useCartActions(callbacks) {
|
|
|
331
396
|
);
|
|
332
397
|
const updateQuantity = useCallback(
|
|
333
398
|
async (invMastUid, newQuantity, options = {}) => {
|
|
334
|
-
const { showToast = false } = options;
|
|
399
|
+
const { showToast = false, unitOfMeasure } = options;
|
|
400
|
+
if (!lineHasQuantity(Number.isFinite(newQuantity) ? newQuantity : null)) {
|
|
401
|
+
sendToast(notify, "error", showToast, "Choose a quantity.");
|
|
402
|
+
options.onError?.(new Error("Quantity is required"));
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
335
405
|
const { uid, lines: liveLines } = liveCart();
|
|
336
|
-
const
|
|
337
|
-
|
|
338
|
-
|
|
406
|
+
const target = targetOf(liveLines, invMastUid, unitOfMeasure);
|
|
407
|
+
if (target === null) return true;
|
|
408
|
+
const line = targetLine(liveLines, target);
|
|
409
|
+
const previousQuantity = line.quantity;
|
|
410
|
+
useCartStore.getState().beginCartOp();
|
|
411
|
+
setCartLines(
|
|
412
|
+
liveLines.map((l) => isTarget(l, target) ? { ...l, quantity: newQuantity } : l)
|
|
339
413
|
);
|
|
340
|
-
setCartLines(updatedLines);
|
|
341
414
|
return settleCartOp(
|
|
342
415
|
{
|
|
343
|
-
|
|
416
|
+
cartUid: uid,
|
|
417
|
+
call: () => callbacks.updateCartLines(uid, [sentLine(line, newQuantity)]),
|
|
344
418
|
failure: "Failed to update quantity",
|
|
345
419
|
errorMessage: "Failed to update quantity. Please try again.",
|
|
346
420
|
showToast,
|
|
347
421
|
rollback: () => setCartLines(
|
|
348
|
-
reverseQuantity(useCartStore.getState().cartLines,
|
|
422
|
+
reverseQuantity(useCartStore.getState().cartLines, target, previousQuantity)
|
|
349
423
|
)
|
|
350
424
|
},
|
|
351
425
|
options,
|
|
@@ -357,23 +431,22 @@ function useCartActions(callbacks) {
|
|
|
357
431
|
);
|
|
358
432
|
const removeFromCart = useCallback(
|
|
359
433
|
async (invMastUid, options = {}) => {
|
|
360
|
-
const { showToast = true, itemId = "" } = options;
|
|
434
|
+
const { showToast = true, itemId = "", unitOfMeasure } = options;
|
|
361
435
|
const { uid, lines: liveLines } = liveCart();
|
|
362
|
-
const
|
|
363
|
-
|
|
364
|
-
const
|
|
365
|
-
|
|
366
|
-
);
|
|
436
|
+
const target = targetOf(liveLines, invMastUid, unitOfMeasure);
|
|
437
|
+
if (target === null) return true;
|
|
438
|
+
const removedLine = targetLine(liveLines, target);
|
|
439
|
+
useCartStore.getState().beginCartOp();
|
|
440
|
+
setCartLines(liveLines.filter((l) => !isTarget(l, target)));
|
|
367
441
|
return settleCartOp(
|
|
368
442
|
{
|
|
369
|
-
|
|
443
|
+
cartUid: uid,
|
|
444
|
+
call: () => callbacks.updateCartLines(uid, [sentLine(removedLine, 0)]),
|
|
370
445
|
failure: "Failed to remove item",
|
|
371
446
|
infoMessage: `${itemId || "Item"} removed from cart`,
|
|
372
447
|
errorMessage: "Failed to remove item. Please try again.",
|
|
373
448
|
showToast,
|
|
374
|
-
rollback: () => setCartLines(
|
|
375
|
-
restoreRemoved(useCartStore.getState().cartLines, invMastUid, removedLine)
|
|
376
|
-
)
|
|
449
|
+
rollback: () => setCartLines(restoreRemoved(useCartStore.getState().cartLines, target, removedLine))
|
|
377
450
|
},
|
|
378
451
|
options,
|
|
379
452
|
notify,
|
|
@@ -386,9 +459,11 @@ function useCartActions(callbacks) {
|
|
|
386
459
|
async (options = {}) => {
|
|
387
460
|
const { showToast = true } = options;
|
|
388
461
|
const { uid, lines: snapshot } = liveCart();
|
|
462
|
+
useCartStore.getState().beginCartOp();
|
|
389
463
|
setCartLines([]);
|
|
390
464
|
return settleCartOp(
|
|
391
465
|
{
|
|
466
|
+
cartUid: uid,
|
|
392
467
|
call: () => callbacks.deleteItemsFromCart(uid),
|
|
393
468
|
failure: "Failed to clear cart",
|
|
394
469
|
infoMessage: "Cart cleared",
|
|
@@ -436,14 +511,14 @@ function useModalLifecycle(isOpen, onClose) {
|
|
|
436
511
|
setIsClient(true);
|
|
437
512
|
}, []);
|
|
438
513
|
useEffect2(() => {
|
|
439
|
-
document.body.style.overflow = isOpen
|
|
514
|
+
document.body.style.overflow = bodyOverflow(isOpen);
|
|
440
515
|
return () => {
|
|
441
516
|
document.body.style.overflow = "auto";
|
|
442
517
|
};
|
|
443
518
|
}, [isOpen]);
|
|
444
519
|
useEffect2(() => {
|
|
445
520
|
const handleEscape = (e) => {
|
|
446
|
-
if (e.key
|
|
521
|
+
if (closesOnKey(e.key, isOpen)) onClose();
|
|
447
522
|
};
|
|
448
523
|
document.addEventListener("keydown", handleEscape);
|
|
449
524
|
return () => document.removeEventListener("keydown", handleEscape);
|
|
@@ -452,10 +527,11 @@ function useModalLifecycle(isOpen, onClose) {
|
|
|
452
527
|
}
|
|
453
528
|
|
|
454
529
|
// src/hooks/use-cart-initialization.ts
|
|
455
|
-
import { useEffect as useEffect3, useMemo as useMemo3, useRef } from "react";
|
|
530
|
+
import { useEffect as useEffect3, useMemo as useMemo3, useRef, useState as useState3 } from "react";
|
|
456
531
|
import { useQuery as useQuery2, useQueryClient as useQueryClient2 } from "@tanstack/react-query";
|
|
457
532
|
var MAX_RETRY_ATTEMPTS = 7;
|
|
458
533
|
var RETRY_DELAY_MS = 1e3;
|
|
534
|
+
var LOOKUP_RESTART_MS = 3e4;
|
|
459
535
|
async function waitBeforeRetry(attempt) {
|
|
460
536
|
if (attempt >= MAX_RETRY_ATTEMPTS) return;
|
|
461
537
|
const delay = Math.min(RETRY_DELAY_MS * Math.pow(1.5, attempt), 1e4);
|
|
@@ -464,6 +540,9 @@ async function waitBeforeRetry(attempt) {
|
|
|
464
540
|
var defaultGetCartToken = () => localStorage.getItem("cartToken");
|
|
465
541
|
var defaultSaveCartToken = (token) => localStorage.setItem("cartToken", token);
|
|
466
542
|
var defaultGenerateCartToken = () => crypto.randomUUID();
|
|
543
|
+
function cartKey(uid) {
|
|
544
|
+
return uid === void 0 ? void 0 : String(uid);
|
|
545
|
+
}
|
|
467
546
|
function useCartInitialization(session, callbacks) {
|
|
468
547
|
const getCartToken = callbacks.getCartToken ?? defaultGetCartToken;
|
|
469
548
|
const saveCartToken = callbacks.saveCartToken ?? defaultSaveCartToken;
|
|
@@ -484,6 +563,11 @@ function useCartInitialization(session, callbacks) {
|
|
|
484
563
|
const retryCountRef = useRef(0);
|
|
485
564
|
const isInitializingRef = useRef(false);
|
|
486
565
|
const prevStatusRef = useRef(effectiveSession.status);
|
|
566
|
+
const statusRef = useRef(effectiveSession.status);
|
|
567
|
+
statusRef.current = effectiveSession.status;
|
|
568
|
+
const [lookupRound, setLookupRound] = useState3(0);
|
|
569
|
+
const restartTimerRef = useRef(void 0);
|
|
570
|
+
useEffect3(() => () => clearTimeout(restartTimerRef.current), []);
|
|
487
571
|
useEffect3(() => {
|
|
488
572
|
const prevStatus = prevStatusRef.current;
|
|
489
573
|
prevStatusRef.current = effectiveSession.status;
|
|
@@ -494,40 +578,57 @@ function useCartInitialization(session, callbacks) {
|
|
|
494
578
|
}
|
|
495
579
|
}, [effectiveSession.status, clearCart, queryClient]);
|
|
496
580
|
useEffect3(() => {
|
|
497
|
-
|
|
498
|
-
|
|
581
|
+
const step = initStepOf(
|
|
582
|
+
effectiveSession.status === "loading",
|
|
583
|
+
effectiveSession.status === "authenticated",
|
|
584
|
+
!!effectiveSession.cartHdrUid,
|
|
585
|
+
cartKey(cartHdrUid) === cartKey(effectiveSession.cartHdrUid),
|
|
586
|
+
!!cartHdrUid,
|
|
587
|
+
effectiveSession.userId !== void 0,
|
|
588
|
+
isInitializingRef.current
|
|
589
|
+
);
|
|
590
|
+
if (step.kind === "stay") return;
|
|
591
|
+
if (step.kind === "session") {
|
|
499
592
|
setCartHdrUid(effectiveSession.cartHdrUid);
|
|
500
593
|
return;
|
|
501
594
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
595
|
+
const forUser = step.kind === "user";
|
|
596
|
+
const lookupUserId = forUser ? effectiveSession.userId : "0";
|
|
597
|
+
isInitializingRef.current = true;
|
|
598
|
+
retryCountRef.current = 0;
|
|
599
|
+
const lookUpCart = async () => {
|
|
600
|
+
const existingToken = getCartToken();
|
|
601
|
+
const cartToken = existingToken && typeof existingToken === "string" ? existingToken : generateCartToken();
|
|
602
|
+
let lastError;
|
|
603
|
+
while (retryCountRef.current < MAX_RETRY_ATTEMPTS) {
|
|
604
|
+
try {
|
|
605
|
+
const cartHdr = await callbacks.cartHdrLookup(lookupUserId, cartToken);
|
|
606
|
+
if (cartHdr && cartHdr.cartHdrUid !== 0) {
|
|
607
|
+
const stillApplies = acceptsLookup(
|
|
608
|
+
forUser,
|
|
609
|
+
statusRef.current === "authenticated",
|
|
610
|
+
!!useCartStore.getState().cartHdrUid
|
|
611
|
+
);
|
|
612
|
+
if (stillApplies) {
|
|
513
613
|
saveCartToken(cartToken);
|
|
514
614
|
setCartHdrUid(cartHdr.cartHdrUid);
|
|
515
|
-
isInitializingRef.current = false;
|
|
516
|
-
return;
|
|
517
615
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
} catch (error) {
|
|
521
|
-
lastError = error;
|
|
522
|
-
retryCountRef.current++;
|
|
523
|
-
await waitBeforeRetry(retryCountRef.current);
|
|
616
|
+
isInitializingRef.current = false;
|
|
617
|
+
return;
|
|
524
618
|
}
|
|
619
|
+
retryCountRef.current++;
|
|
620
|
+
await waitBeforeRetry(retryCountRef.current);
|
|
621
|
+
} catch (error) {
|
|
622
|
+
lastError = error;
|
|
623
|
+
retryCountRef.current++;
|
|
624
|
+
await waitBeforeRetry(retryCountRef.current);
|
|
525
625
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
}
|
|
626
|
+
}
|
|
627
|
+
console.error(`Cart initialization failed after ${MAX_RETRY_ATTEMPTS} attempts`, lastError);
|
|
628
|
+
isInitializingRef.current = false;
|
|
629
|
+
restartTimerRef.current = setTimeout(() => setLookupRound((n) => n + 1), LOOKUP_RESTART_MS);
|
|
630
|
+
};
|
|
631
|
+
void lookUpCart();
|
|
531
632
|
}, [
|
|
532
633
|
effectiveSession,
|
|
533
634
|
cartHdrUid,
|
|
@@ -535,15 +636,17 @@ function useCartInitialization(session, callbacks) {
|
|
|
535
636
|
callbacks,
|
|
536
637
|
getCartToken,
|
|
537
638
|
saveCartToken,
|
|
538
|
-
generateCartToken
|
|
639
|
+
generateCartToken,
|
|
640
|
+
lookupRound
|
|
539
641
|
]);
|
|
540
642
|
const { data: cartLinesData } = useQuery2({
|
|
541
643
|
queryKey: ["cartLines", cartHdrUid],
|
|
542
644
|
queryFn: async () => await callbacks.getCartLines(Number(cartHdrUid)) ?? [],
|
|
543
|
-
enabled: !!cartHdrUid
|
|
645
|
+
enabled: !!cartHdrUid,
|
|
646
|
+
structuralSharing: false
|
|
544
647
|
});
|
|
545
648
|
useEffect3(() => {
|
|
546
|
-
if (cartLinesData) {
|
|
649
|
+
if (cartLinesData && cartIdle(useCartStore.getState().pendingCartOps)) {
|
|
547
650
|
setCartLines(cartLinesData);
|
|
548
651
|
}
|
|
549
652
|
}, [cartLinesData, setCartLines]);
|
|
@@ -557,17 +660,29 @@ function useCartInitialization(session, callbacks) {
|
|
|
557
660
|
// src/hooks/use-cart-pricing.ts
|
|
558
661
|
import { useMemo as useMemo4 } from "react";
|
|
559
662
|
import { useQueries } from "@tanstack/react-query";
|
|
560
|
-
import {
|
|
561
|
-
|
|
562
|
-
|
|
663
|
+
import {
|
|
664
|
+
cleanMoney as cleanMoney2,
|
|
665
|
+
derivePrice,
|
|
666
|
+
linePriceAt,
|
|
667
|
+
priceQueryRuns,
|
|
668
|
+
priceQueryUnit,
|
|
669
|
+
pricingLoadingFrom,
|
|
670
|
+
pricingSettledFrom
|
|
671
|
+
} from "@simpleapps-com/augur-utils";
|
|
672
|
+
function derivedOrRefused(priceData, quantity) {
|
|
563
673
|
try {
|
|
564
|
-
|
|
565
|
-
return derived.state === "priced" ? derived.unitPrice : 0;
|
|
674
|
+
return derivePrice({ priceData, quantity });
|
|
566
675
|
} catch (error) {
|
|
567
|
-
if (error instanceof RangeError) return
|
|
676
|
+
if (error instanceof RangeError) return null;
|
|
568
677
|
throw error;
|
|
569
678
|
}
|
|
570
679
|
}
|
|
680
|
+
function deriveLineUnitPrice(priceData, quantity) {
|
|
681
|
+
const hasQuantity = typeof quantity === "number" && Number.isFinite(quantity);
|
|
682
|
+
const derived = hasQuantity ? derivedOrRefused(priceData, quantity) : null;
|
|
683
|
+
const priced = derived?.state === "priced";
|
|
684
|
+
return lineUnitPrice(hasQuantity, priced, priced ? derived.unitPrice : 0);
|
|
685
|
+
}
|
|
571
686
|
function lineTotal(unitPrice, quantity) {
|
|
572
687
|
return Number.isFinite(quantity) ? cleanMoney2(unitPrice * quantity) : 0;
|
|
573
688
|
}
|
|
@@ -578,48 +693,63 @@ function useCartPricing(options = {}) {
|
|
|
578
693
|
const cartLines = useCartLines();
|
|
579
694
|
const customerId = options.customerId ?? auth?.customerId ?? site?.defaults?.customerId;
|
|
580
695
|
const priceFn = options.queryFn;
|
|
696
|
+
const runs = cartLines.map((line) => priceQueryRuns(!!customerId, !!line.itemId, line.quantity));
|
|
581
697
|
const priceQueries = useQueries({
|
|
582
|
-
queries: cartLines.map((line) => {
|
|
583
|
-
const quantity = line.quantity ??
|
|
698
|
+
queries: cartLines.map((line, index) => {
|
|
699
|
+
const quantity = line.quantity ?? 0;
|
|
700
|
+
const enabled = runs[index] === true;
|
|
701
|
+
const unitOfMeasure = priceQueryUnit(line.unitOfMeasure) ?? void 0;
|
|
584
702
|
if (priceFn) {
|
|
585
703
|
return {
|
|
586
|
-
queryKey: [
|
|
587
|
-
|
|
704
|
+
queryKey: [
|
|
705
|
+
"price",
|
|
706
|
+
line.itemId?.toUpperCase() || "",
|
|
707
|
+
customerId,
|
|
708
|
+
quantity,
|
|
709
|
+
unitOfMeasure ?? ""
|
|
710
|
+
],
|
|
711
|
+
queryFn: () => priceFn(line.itemId, customerId, quantity, unitOfMeasure),
|
|
588
712
|
refetchOnReconnect: true,
|
|
589
713
|
refetchOnWindowFocus: false,
|
|
590
714
|
meta: { persist: true },
|
|
591
|
-
enabled
|
|
715
|
+
enabled
|
|
592
716
|
};
|
|
593
717
|
}
|
|
594
718
|
const proxyOpts = proxy.pricing.priceEngine.list({
|
|
595
719
|
itemId: line.itemId?.toUpperCase() || "",
|
|
596
720
|
customerId: Number(customerId),
|
|
597
|
-
quantity
|
|
721
|
+
quantity,
|
|
722
|
+
unitOfMeasure
|
|
598
723
|
});
|
|
599
724
|
return {
|
|
600
725
|
...proxyOpts,
|
|
601
|
-
enabled
|
|
726
|
+
enabled,
|
|
602
727
|
refetchOnReconnect: true,
|
|
603
728
|
refetchOnWindowFocus: false,
|
|
604
729
|
meta: { persist: true }
|
|
605
730
|
};
|
|
606
731
|
})
|
|
607
732
|
});
|
|
733
|
+
const linePrices = useMemo4(
|
|
734
|
+
() => cartLines.map((line, index) => {
|
|
735
|
+
const priceData = priceQueries[index]?.data;
|
|
736
|
+
if (!priceData) return null;
|
|
737
|
+
return {
|
|
738
|
+
unitPrice: deriveLineUnitPrice(priceData, line.quantity),
|
|
739
|
+
priceData
|
|
740
|
+
};
|
|
741
|
+
}),
|
|
742
|
+
[cartLines, priceQueries]
|
|
743
|
+
);
|
|
744
|
+
const lineUnitPrices = useMemo4(() => linePrices.map((p) => p?.unitPrice ?? 0), [linePrices]);
|
|
608
745
|
const prices = useMemo4(() => {
|
|
609
746
|
const map = {};
|
|
610
747
|
cartLines.forEach((line, index) => {
|
|
611
|
-
const
|
|
612
|
-
|
|
613
|
-
if (priceData) {
|
|
614
|
-
const unitPrice = deriveLineUnitPrice(priceData, line.quantity);
|
|
615
|
-
map[line.itemId.toUpperCase()] = {
|
|
616
|
-
unitPrice,
|
|
617
|
-
priceData
|
|
618
|
-
};
|
|
619
|
-
}
|
|
748
|
+
const linePrice = linePrices[index];
|
|
749
|
+
if (linePrice) map[line.itemId.toUpperCase()] = linePrice;
|
|
620
750
|
});
|
|
621
751
|
return map;
|
|
622
|
-
}, [cartLines,
|
|
752
|
+
}, [cartLines, linePrices]);
|
|
623
753
|
const getUnitPrice = useMemo4(() => {
|
|
624
754
|
return (itemId) => {
|
|
625
755
|
return prices[itemId?.toUpperCase()]?.unitPrice ?? 0;
|
|
@@ -630,19 +760,36 @@ function useCartPricing(options = {}) {
|
|
|
630
760
|
return lineTotal(getUnitPrice(itemId), quantity);
|
|
631
761
|
};
|
|
632
762
|
}, [getUnitPrice]);
|
|
763
|
+
const getLineUnitPrice = useMemo4(() => {
|
|
764
|
+
return (index) => linePriceAt(lineUnitPrices, index);
|
|
765
|
+
}, [lineUnitPrices]);
|
|
766
|
+
const getLineTotal = useMemo4(() => {
|
|
767
|
+
return (index) => lineTotal(linePriceAt(lineUnitPrices, index), cartLines[index]?.quantity ?? null);
|
|
768
|
+
}, [cartLines, lineUnitPrices]);
|
|
633
769
|
const subtotal = useMemo4(() => {
|
|
634
|
-
return cartLines.reduce(
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
}, [cartLines,
|
|
639
|
-
const isLoading =
|
|
640
|
-
|
|
770
|
+
return cartLines.reduce(
|
|
771
|
+
(sum, line, index) => cleanMoney2(sum + lineTotal(linePriceAt(lineUnitPrices, index), line.quantity)),
|
|
772
|
+
0
|
|
773
|
+
);
|
|
774
|
+
}, [cartLines, lineUnitPrices]);
|
|
775
|
+
const isLoading = pricingLoadingFrom(
|
|
776
|
+
priceQueries.map((q) => q.isLoading),
|
|
777
|
+
runs,
|
|
778
|
+
0
|
|
779
|
+
);
|
|
780
|
+
const isSuccess = pricingSettledFrom(
|
|
781
|
+
priceQueries.map((q) => q.isSuccess),
|
|
782
|
+
runs,
|
|
783
|
+
0
|
|
784
|
+
);
|
|
641
785
|
const isError = priceQueries.some((q) => q.isError);
|
|
642
786
|
return {
|
|
643
787
|
prices,
|
|
788
|
+
linePrices,
|
|
644
789
|
getUnitPrice,
|
|
645
790
|
getItemTotal,
|
|
791
|
+
getLineUnitPrice,
|
|
792
|
+
getLineTotal,
|
|
646
793
|
subtotal,
|
|
647
794
|
isLoading,
|
|
648
795
|
isSuccess,
|
|
@@ -692,7 +839,7 @@ var usePaginationPrefetch = ({
|
|
|
692
839
|
|
|
693
840
|
// src/persistence/use-persistence.ts
|
|
694
841
|
import { hours, toMillis as toMillis4 } from "@simpleapps-com/augur-utils";
|
|
695
|
-
import { useState as
|
|
842
|
+
import { useState as useState4, useEffect as useEffect5, useMemo as useMemo5 } from "react";
|
|
696
843
|
|
|
697
844
|
// src/persistence/create-persister.ts
|
|
698
845
|
import { seconds, toMillis as toMillis2 } from "@simpleapps-com/augur-utils";
|
|
@@ -834,8 +981,8 @@ function useStorageMonitor(config) {
|
|
|
834
981
|
// src/persistence/use-persistence.ts
|
|
835
982
|
var PERSISTED_DATA_FORMAT = "augur-2";
|
|
836
983
|
function useAugurPersistence(config) {
|
|
837
|
-
const [persister] =
|
|
838
|
-
const [isMounted, setIsMounted] =
|
|
984
|
+
const [persister] = useState4(() => createPersister(config));
|
|
985
|
+
const [isMounted, setIsMounted] = useState4(false);
|
|
839
986
|
useEffect5(() => {
|
|
840
987
|
setIsMounted(true);
|
|
841
988
|
}, []);
|