@simpleapps-com/augur-hooks 0.1.0 → 0.1.2

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/dist/index.js CHANGED
@@ -31,7 +31,12 @@ var useCartStore = create()(
31
31
  0
32
32
  );
33
33
  set({ cartLines, cartItemCount }, false, "setCartLines");
34
- }
34
+ },
35
+ clearCart: () => set(
36
+ { cartHdrUid: void 0, cartLines: [], cartItemCount: 0 },
37
+ false,
38
+ "clearCart"
39
+ )
35
40
  }),
36
41
  { name: "CartStore", enabled: process.env.NODE_ENV === "development" }
37
42
  )
@@ -41,6 +46,7 @@ var useCartLines = () => useCartStore((s) => s.cartLines);
41
46
  var useCartItemCount = () => useCartStore((s) => s.cartItemCount);
42
47
  var useSetCartHdrUid = () => useCartStore((s) => s.setCartHdrUid);
43
48
  var useSetCartLines = () => useCartStore((s) => s.setCartLines);
49
+ var useClearCart = () => useCartStore((s) => s.clearCart);
44
50
 
45
51
  // src/stores/items-filters-store.ts
46
52
  import { create as create2 } from "zustand";
@@ -256,7 +262,9 @@ function useInvMastStock(invMastUid, options = {}) {
256
262
  ...options.queryFn ? { queryFn: options.queryFn } : {},
257
263
  enabled: (options.enabled ?? true) && !!invMastUid,
258
264
  retry: 3,
265
+ /* v8 ignore start */
259
266
  retryDelay: (attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 1e4)
267
+ /* v8 ignore stop */
260
268
  });
261
269
  return {
262
270
  qtyOnHand: data ?? null,
@@ -284,11 +292,17 @@ var getProductCategoryOptions = (api, itemCategoryUid) => ({
284
292
  function useProductCategory(itemCategoryUid, options = {}) {
285
293
  const api = useAugurApi();
286
294
  const { data, isLoading, error } = useQuery6({
287
- ...itemCategoryUid ? getProductCategoryOptions(api, itemCategoryUid) : { queryKey: getProductCategoryKey(null), queryFn: () => Promise.reject() },
295
+ ...itemCategoryUid ? getProductCategoryOptions(api, itemCategoryUid) : {
296
+ queryKey: getProductCategoryKey(null),
297
+ /* v8 ignore next */
298
+ queryFn: () => Promise.reject()
299
+ },
288
300
  ...options.queryFn ? { queryFn: options.queryFn } : {},
289
301
  enabled: (options.enabled ?? true) && !!itemCategoryUid,
290
302
  retry: 3,
303
+ /* v8 ignore start */
291
304
  retryDelay: (attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 1e4)
305
+ /* v8 ignore stop */
292
306
  });
293
307
  return {
294
308
  childrenTotal: data?.childrenTotal ?? 0,
@@ -318,11 +332,17 @@ var getItemDetailsOptions = (api, itemId) => ({
318
332
  function useItemDetails(itemId, options = {}) {
319
333
  const api = useAugurApi();
320
334
  const { data, isLoading, error } = useQuery7({
321
- ...itemId ? getItemDetailsOptions(api, itemId) : { queryKey: getItemDetailsKey(""), queryFn: () => Promise.reject() },
335
+ ...itemId ? getItemDetailsOptions(api, itemId) : {
336
+ queryKey: getItemDetailsKey(""),
337
+ /* v8 ignore next */
338
+ queryFn: () => Promise.reject()
339
+ },
322
340
  ...options.queryFn ? { queryFn: options.queryFn } : {},
323
341
  enabled: (options.enabled ?? true) && !!itemId,
324
342
  retry: 3,
343
+ /* v8 ignore start */
325
344
  retryDelay: (attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 1e4)
345
+ /* v8 ignore stop */
326
346
  });
327
347
  const categoryList = data?.categoryList ?? [];
328
348
  return {
@@ -356,12 +376,15 @@ function useItemAttributes(itemCategoryUid, options = {}) {
356
376
  const { data, isLoading, error } = useQuery8({
357
377
  ...itemCategoryUid ? getItemAttributesOptions(api, itemCategoryUid) : {
358
378
  queryKey: getItemAttributesKey(null),
379
+ /* v8 ignore next */
359
380
  queryFn: () => Promise.reject()
360
381
  },
361
382
  ...options.queryFn ? { queryFn: options.queryFn } : {},
362
383
  enabled: (options.enabled ?? true) && !!itemCategoryUid,
363
384
  retry: 3,
385
+ /* v8 ignore start */
364
386
  retryDelay: (attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 1e4)
387
+ /* v8 ignore stop */
365
388
  });
366
389
  return {
367
390
  attributes: data?.attributes ?? null,
@@ -405,7 +428,9 @@ function useProductSearch(pageData, options = {}) {
405
428
  ...defaultOptions,
406
429
  ...options.queryFn ? { queryFn: options.queryFn } : {},
407
430
  retry: 3,
431
+ /* v8 ignore start */
408
432
  retryDelay: (attemptIndex) => Math.min(1e3 * 2 ** attemptIndex, 1e4)
433
+ /* v8 ignore stop */
409
434
  });
410
435
  return {
411
436
  productItems: data?.items ?? null,
@@ -454,13 +479,41 @@ function useSearchSuggestions(query, options = {}) {
454
479
  // src/hooks/use-cart-actions.ts
455
480
  import { useCallback } from "react";
456
481
  import { useQueryClient } from "@tanstack/react-query";
482
+ function requireCartUid(cartHdrUid) {
483
+ const num = typeof cartHdrUid === "string" ? Number(cartHdrUid) : cartHdrUid;
484
+ if (!num) throw new Error("Cart UID is not set");
485
+ return num;
486
+ }
487
+ function buildOptimisticAdd(cartLines, item, cartHdrUid, itemId) {
488
+ const existingIndex = cartLines.findIndex(
489
+ (line) => line.invMastUid === item.invMastUid
490
+ );
491
+ if (existingIndex >= 0) {
492
+ return cartLines.map(
493
+ (line, index) => index === existingIndex ? { ...line, quantity: line.quantity + item.quantity } : line
494
+ );
495
+ }
496
+ return [
497
+ ...cartLines,
498
+ {
499
+ cartHdrUid,
500
+ invMastUid: item.invMastUid,
501
+ invMastUidCount: 1,
502
+ isAssembly: "N",
503
+ itemId,
504
+ lineNo: cartLines.length + 1,
505
+ lineNote: null,
506
+ quantity: item.quantity,
507
+ unitOfMeasure: item.unitOfMeasure
508
+ }
509
+ ];
510
+ }
457
511
  function useCartActions(callbacks) {
458
512
  const queryClient = useQueryClient();
459
513
  const cartHdrUid = useCartHdrUid();
460
514
  const cartLines = useCartLines();
461
515
  const setCartLines = useSetCartLines();
462
516
  const notify = callbacks.toast;
463
- const cartHdrUidNum = typeof cartHdrUid === "string" ? Number(cartHdrUid) : cartHdrUid;
464
517
  const invalidateCartCache = useCallback(() => {
465
518
  queryClient.invalidateQueries({ queryKey: ["cartLines", cartHdrUid] });
466
519
  queryClient.invalidateQueries({
@@ -470,35 +523,13 @@ function useCartActions(callbacks) {
470
523
  const addToCart = useCallback(
471
524
  async (item, options = {}) => {
472
525
  const { showToast = true, itemId = "", onSuccess, onError } = options;
473
- if (!cartHdrUidNum) throw new Error("Cart UID is not set");
526
+ const uid = requireCartUid(cartHdrUid);
474
527
  const previousCartLines = [...cartLines];
475
- const existingIndex = cartLines.findIndex(
476
- (line) => line.invMastUid === item.invMastUid
477
- );
478
- let optimisticCartLines;
479
- if (existingIndex >= 0) {
480
- optimisticCartLines = cartLines.map(
481
- (line, index) => index === existingIndex ? { ...line, quantity: line.quantity + item.quantity } : line
482
- );
483
- } else {
484
- const newLine = {
485
- cartHdrUid: cartHdrUidNum,
486
- invMastUid: item.invMastUid,
487
- invMastUidCount: 1,
488
- isAssembly: "N",
489
- itemId,
490
- lineNo: cartLines.length + 1,
491
- lineNote: null,
492
- quantity: item.quantity,
493
- unitOfMeasure: item.unitOfMeasure
494
- };
495
- optimisticCartLines = [...cartLines, newLine];
496
- }
497
- setCartLines(optimisticCartLines);
528
+ setCartLines(buildOptimisticAdd(cartLines, item, uid, itemId));
498
529
  try {
499
- const success = await callbacks.addToCart(cartHdrUidNum, [
530
+ const success = await callbacks.addToCart(uid, [
500
531
  {
501
- cartHdrUid: cartHdrUidNum,
532
+ cartHdrUid: uid,
502
533
  invMastUid: item.invMastUid,
503
534
  quantity: item.quantity,
504
535
  unitOfMeasure: item.unitOfMeasure
@@ -507,9 +538,7 @@ function useCartActions(callbacks) {
507
538
  if (!success) throw new Error("Failed to add item to cart");
508
539
  invalidateCartCache();
509
540
  if (showToast && notify?.info) {
510
- notify.info(
511
- `${item.quantity} x ${itemId || "Item"} added to cart`
512
- );
541
+ notify.info(`${item.quantity} x ${itemId || "Item"} added to cart`);
513
542
  }
514
543
  onSuccess?.();
515
544
  return true;
@@ -527,20 +556,14 @@ function useCartActions(callbacks) {
527
556
  const updateQuantity = useCallback(
528
557
  async (invMastUid, newQuantity, options = {}) => {
529
558
  const { showToast = false, onSuccess, onError } = options;
530
- if (!cartHdrUidNum) throw new Error("Cart UID is not set");
559
+ const uid = requireCartUid(cartHdrUid);
531
560
  const previousCartLines = [...cartLines];
532
- const optimisticCartLines = cartLines.map(
561
+ const updatedLines = cartLines.map(
533
562
  (line) => line.invMastUid === invMastUid ? { ...line, quantity: newQuantity } : line
534
563
  );
535
- setCartLines(optimisticCartLines);
564
+ setCartLines(updatedLines);
536
565
  try {
537
- const updatedLines = cartLines.map(
538
- (line) => line.invMastUid === invMastUid ? { ...line, quantity: newQuantity } : line
539
- );
540
- const success = await callbacks.updateCartLines(
541
- cartHdrUidNum,
542
- updatedLines
543
- );
566
+ const success = await callbacks.updateCartLines(uid, updatedLines);
544
567
  if (!success) throw new Error("Failed to update quantity");
545
568
  invalidateCartCache();
546
569
  onSuccess?.();
@@ -559,20 +582,14 @@ function useCartActions(callbacks) {
559
582
  const removeFromCart = useCallback(
560
583
  async (invMastUid, options = {}) => {
561
584
  const { showToast = true, itemId = "", onSuccess, onError } = options;
562
- if (!cartHdrUidNum) throw new Error("Cart UID is not set");
585
+ const uid = requireCartUid(cartHdrUid);
563
586
  const previousCartLines = [...cartLines];
564
- const optimisticCartLines = cartLines.filter(
565
- (line) => line.invMastUid !== invMastUid
566
- );
567
- setCartLines(optimisticCartLines);
587
+ setCartLines(cartLines.filter((line) => line.invMastUid !== invMastUid));
568
588
  try {
569
- const updatedLines = cartLines.map(
589
+ const zeroedLines = cartLines.map(
570
590
  (line) => line.invMastUid === invMastUid ? { ...line, quantity: 0 } : line
571
591
  );
572
- const success = await callbacks.updateCartLines(
573
- cartHdrUidNum,
574
- updatedLines
575
- );
592
+ const success = await callbacks.updateCartLines(uid, zeroedLines);
576
593
  if (!success) throw new Error("Failed to remove item");
577
594
  invalidateCartCache();
578
595
  if (showToast && notify?.info) {
@@ -594,11 +611,11 @@ function useCartActions(callbacks) {
594
611
  const clearCart = useCallback(
595
612
  async (options = {}) => {
596
613
  const { showToast = true, onSuccess, onError } = options;
597
- if (!cartHdrUidNum) throw new Error("Cart UID is not set");
614
+ const uid = requireCartUid(cartHdrUid);
598
615
  const previousCartLines = [...cartLines];
599
616
  setCartLines([]);
600
617
  try {
601
- const success = await callbacks.deleteItemsFromCart(cartHdrUidNum);
618
+ const success = await callbacks.deleteItemsFromCart(uid);
602
619
  if (!success) throw new Error("Failed to clear cart");
603
620
  invalidateCartCache();
604
621
  if (showToast && notify?.info) {
@@ -644,7 +661,7 @@ function useCartActions(callbacks) {
644
661
 
645
662
  // src/hooks/use-cart-initialization.ts
646
663
  import { useEffect as useEffect2, useRef } from "react";
647
- import { useQuery as useQuery11 } from "@tanstack/react-query";
664
+ import { useQuery as useQuery11, useQueryClient as useQueryClient2 } from "@tanstack/react-query";
648
665
  import { CACHE_CONFIG as CACHE_CONFIG11 } from "@simpleapps-com/augur-utils";
649
666
  var MAX_RETRY_ATTEMPTS = 7;
650
667
  var RETRY_DELAY_MS = 1e3;
@@ -652,8 +669,20 @@ function useCartInitialization(session, callbacks) {
652
669
  const cartHdrUid = useCartHdrUid();
653
670
  const setCartHdrUid = useSetCartHdrUid();
654
671
  const setCartLines = useSetCartLines();
672
+ const clearCart = useClearCart();
673
+ const queryClient = useQueryClient2();
655
674
  const retryCountRef = useRef(0);
656
675
  const isInitializingRef = useRef(false);
676
+ const prevStatusRef = useRef(session.status);
677
+ useEffect2(() => {
678
+ const prevStatus = prevStatusRef.current;
679
+ prevStatusRef.current = session.status;
680
+ if (prevStatus === "authenticated" && session.status === "unauthenticated") {
681
+ clearCart();
682
+ queryClient.removeQueries({ queryKey: ["cartLines"] });
683
+ queryClient.removeQueries({ queryKey: ["cart-also-bought"] });
684
+ }
685
+ }, [session.status, clearCart, queryClient]);
657
686
  useEffect2(() => {
658
687
  if (cartHdrUid || session.status === "loading") return;
659
688
  if (session.cartHdrUid && session.status === "authenticated") {
@@ -787,7 +816,7 @@ function getCartPricingQueryOptions(api, cartLines, customerId) {
787
816
  }
788
817
 
789
818
  // src/hooks/use-pagination-prefetch.ts
790
- import { useQueryClient as useQueryClient2 } from "@tanstack/react-query";
819
+ import { useQueryClient as useQueryClient3 } from "@tanstack/react-query";
791
820
  import { useCallback as useCallback2 } from "react";
792
821
  var usePaginationPrefetch = ({
793
822
  queryKey,
@@ -797,7 +826,7 @@ var usePaginationPrefetch = ({
797
826
  gcTime = 30 * 60 * 1e3,
798
827
  enabled = true
799
828
  }) => {
800
- const queryClient = useQueryClient2();
829
+ const queryClient = useQueryClient3();
801
830
  const prefetchPage = useCallback2(
802
831
  async (page) => {
803
832
  if (!enabled) return;
@@ -950,6 +979,7 @@ export {
950
979
  useCartPricing,
951
980
  useCartStore,
952
981
  useCategoryItemsInfinite,
982
+ useClearCart,
953
983
  useDebounce,
954
984
  useFormatPrice,
955
985
  useInvMast,