@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.cjs CHANGED
@@ -31,7 +31,12 @@ var useCartStore = _zustand.create.call(void 0, )(
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
 
@@ -256,7 +262,9 @@ function useInvMastStock(invMastUid, options = {}) {
256
262
  ...options.queryFn ? { queryFn: options.queryFn } : {},
257
263
  enabled: (_nullishCoalesce(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: _nullishCoalesce(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 } = _reactquery.useQuery.call(void 0, {
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: (_nullishCoalesce(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: _nullishCoalesce(_optionalChain([data, 'optionalAccess', _7 => _7.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 } = _reactquery.useQuery.call(void 0, {
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: (_nullishCoalesce(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 = _nullishCoalesce(_optionalChain([data, 'optionalAccess', _11 => _11.categoryList]), () => ( []));
328
348
  return {
@@ -356,12 +376,15 @@ function useItemAttributes(itemCategoryUid, options = {}) {
356
376
  const { data, isLoading, error } = _reactquery.useQuery.call(void 0, {
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: (_nullishCoalesce(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: _nullishCoalesce(_optionalChain([data, 'optionalAccess', _12 => _12.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: _nullishCoalesce(_optionalChain([data, 'optionalAccess', _13 => _13.items]), () => ( null)),
@@ -454,13 +479,41 @@ function useSearchSuggestions(query, options = {}) {
454
479
  // src/hooks/use-cart-actions.ts
455
480
 
456
481
 
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 = _reactquery.useQueryClient.call(void 0, );
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 = _react.useCallback.call(void 0, () => {
465
518
  queryClient.invalidateQueries({ queryKey: ["cartLines", cartHdrUid] });
466
519
  queryClient.invalidateQueries({
@@ -470,35 +523,13 @@ function useCartActions(callbacks) {
470
523
  const addToCart = _react.useCallback.call(void 0,
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 && _optionalChain([notify, 'optionalAccess', _17 => _17.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
  _optionalChain([onSuccess, 'optionalCall', _18 => _18()]);
515
544
  return true;
@@ -527,20 +556,14 @@ function useCartActions(callbacks) {
527
556
  const updateQuantity = _react.useCallback.call(void 0,
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
  _optionalChain([onSuccess, 'optionalCall', _21 => _21()]);
@@ -559,20 +582,14 @@ function useCartActions(callbacks) {
559
582
  const removeFromCart = _react.useCallback.call(void 0,
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 && _optionalChain([notify, 'optionalAccess', _24 => _24.info])) {
@@ -594,11 +611,11 @@ function useCartActions(callbacks) {
594
611
  const clearCart = _react.useCallback.call(void 0,
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 && _optionalChain([notify, 'optionalAccess', _28 => _28.info])) {
@@ -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 = _reactquery.useQueryClient.call(void 0, );
655
674
  const retryCountRef = _react.useRef.call(void 0, 0);
656
675
  const isInitializingRef = _react.useRef.call(void 0, false);
676
+ const prevStatusRef = _react.useRef.call(void 0, session.status);
677
+ _react.useEffect.call(void 0, () => {
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
  _react.useEffect.call(void 0, () => {
658
687
  if (cartHdrUid || session.status === "loading") return;
659
688
  if (session.cartHdrUid && session.status === "authenticated") {
@@ -967,5 +996,6 @@ function useItemSearchInfinite(itemsFilters, itemCategoryUid, options = {}) {
967
996
 
968
997
 
969
998
 
970
- exports.AugurHooksProvider = AugurHooksProvider; exports.CATEGORY_CACHE_OPTIONS = CATEGORY_CACHE_OPTIONS; exports.INV_MAST_CACHE_OPTIONS = INV_MAST_CACHE_OPTIONS; exports.INV_MAST_DOC_CACHE_OPTIONS = INV_MAST_DOC_CACHE_OPTIONS; exports.PRICE_CACHE_OPTIONS = PRICE_CACHE_OPTIONS; exports.getCartPricingQueryOptions = getCartPricingQueryOptions; exports.getCategoryItemsInfiniteKey = getCategoryItemsInfiniteKey; exports.getInvMastDocKey = getInvMastDocKey; exports.getInvMastDocOptions = getInvMastDocOptions; exports.getInvMastKey = getInvMastKey; exports.getInvMastOptions = getInvMastOptions; exports.getInvMastStockKey = getInvMastStockKey; exports.getInvMastStockOptions = getInvMastStockOptions; exports.getItemAttributesKey = getItemAttributesKey; exports.getItemAttributesOptions = getItemAttributesOptions; exports.getItemCategoryKey = getItemCategoryKey; exports.getItemCategoryOptions = getItemCategoryOptions; exports.getItemDetailsKey = getItemDetailsKey; exports.getItemDetailsOptions = getItemDetailsOptions; exports.getItemPriceKey = getItemPriceKey; exports.getItemPriceOptions = getItemPriceOptions; exports.getItemSearchInfiniteKey = getItemSearchInfiniteKey; exports.getProductCategoryKey = getProductCategoryKey; exports.getProductCategoryOptions = getProductCategoryOptions; exports.getProductSearchKey = getProductSearchKey; exports.getProductSearchOptions = getProductSearchOptions; exports.getSearchSuggestionsKey = getSearchSuggestionsKey; exports.getSearchSuggestionsOptions = getSearchSuggestionsOptions; exports.useAugurApi = useAugurApi; exports.useCartActions = useCartActions; exports.useCartHdrUid = useCartHdrUid; exports.useCartInitialization = useCartInitialization; exports.useCartItemCount = useCartItemCount; exports.useCartLines = useCartLines; exports.useCartPricing = useCartPricing; exports.useCartStore = useCartStore; exports.useCategoryItemsInfinite = useCategoryItemsInfinite; exports.useDebounce = useDebounce; exports.useFormatPrice = useFormatPrice; exports.useInvMast = useInvMast; exports.useInvMastDoc = useInvMastDoc; exports.useInvMastStock = useInvMastStock; exports.useItemAttributes = useItemAttributes; exports.useItemCategory = useItemCategory; exports.useItemDetails = useItemDetails; exports.useItemFiltersStore = useItemFiltersStore; exports.useItemPrice = useItemPrice; exports.useItemSearchInfinite = useItemSearchInfinite; exports.usePaginationPrefetch = usePaginationPrefetch; exports.useProductCategory = useProductCategory; exports.useProductSearch = useProductSearch; exports.useSearchSuggestions = useSearchSuggestions; exports.useSetCartHdrUid = useSetCartHdrUid; exports.useSetCartLines = useSetCartLines;
999
+
1000
+ exports.AugurHooksProvider = AugurHooksProvider; exports.CATEGORY_CACHE_OPTIONS = CATEGORY_CACHE_OPTIONS; exports.INV_MAST_CACHE_OPTIONS = INV_MAST_CACHE_OPTIONS; exports.INV_MAST_DOC_CACHE_OPTIONS = INV_MAST_DOC_CACHE_OPTIONS; exports.PRICE_CACHE_OPTIONS = PRICE_CACHE_OPTIONS; exports.getCartPricingQueryOptions = getCartPricingQueryOptions; exports.getCategoryItemsInfiniteKey = getCategoryItemsInfiniteKey; exports.getInvMastDocKey = getInvMastDocKey; exports.getInvMastDocOptions = getInvMastDocOptions; exports.getInvMastKey = getInvMastKey; exports.getInvMastOptions = getInvMastOptions; exports.getInvMastStockKey = getInvMastStockKey; exports.getInvMastStockOptions = getInvMastStockOptions; exports.getItemAttributesKey = getItemAttributesKey; exports.getItemAttributesOptions = getItemAttributesOptions; exports.getItemCategoryKey = getItemCategoryKey; exports.getItemCategoryOptions = getItemCategoryOptions; exports.getItemDetailsKey = getItemDetailsKey; exports.getItemDetailsOptions = getItemDetailsOptions; exports.getItemPriceKey = getItemPriceKey; exports.getItemPriceOptions = getItemPriceOptions; exports.getItemSearchInfiniteKey = getItemSearchInfiniteKey; exports.getProductCategoryKey = getProductCategoryKey; exports.getProductCategoryOptions = getProductCategoryOptions; exports.getProductSearchKey = getProductSearchKey; exports.getProductSearchOptions = getProductSearchOptions; exports.getSearchSuggestionsKey = getSearchSuggestionsKey; exports.getSearchSuggestionsOptions = getSearchSuggestionsOptions; exports.useAugurApi = useAugurApi; exports.useCartActions = useCartActions; exports.useCartHdrUid = useCartHdrUid; exports.useCartInitialization = useCartInitialization; exports.useCartItemCount = useCartItemCount; exports.useCartLines = useCartLines; exports.useCartPricing = useCartPricing; exports.useCartStore = useCartStore; exports.useCategoryItemsInfinite = useCategoryItemsInfinite; exports.useClearCart = useClearCart; exports.useDebounce = useDebounce; exports.useFormatPrice = useFormatPrice; exports.useInvMast = useInvMast; exports.useInvMastDoc = useInvMastDoc; exports.useInvMastStock = useInvMastStock; exports.useItemAttributes = useItemAttributes; exports.useItemCategory = useItemCategory; exports.useItemDetails = useItemDetails; exports.useItemFiltersStore = useItemFiltersStore; exports.useItemPrice = useItemPrice; exports.useItemSearchInfinite = useItemSearchInfinite; exports.usePaginationPrefetch = usePaginationPrefetch; exports.useProductCategory = useProductCategory; exports.useProductSearch = useProductSearch; exports.useSearchSuggestions = useSearchSuggestions; exports.useSetCartHdrUid = useSetCartHdrUid; exports.useSetCartLines = useSetCartLines;
971
1001
  //# sourceMappingURL=index.cjs.map