@anker-in/shopify-react 1.3.0-beta.2 → 1.3.0-beta.3

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.
@@ -6,7 +6,7 @@ import { jsx } from 'react/jsx-runtime';
6
6
  import Decimal3 from 'decimal.js';
7
7
  import { atobID, btoaID } from '@anker-in/shopify-core';
8
8
  import useSWR from 'swr';
9
- import { useRequest } from 'ahooks';
9
+ import { useDebounceEffect, useRequest } from 'ahooks';
10
10
 
11
11
  // src/hooks/cart/use-create-cart.ts
12
12
  var ShopifyContext = createContext(null);
@@ -707,9 +707,13 @@ function useAutoRemoveFreeGifts(options = {}) {
707
707
  const {
708
708
  removeFunctionGifts = true,
709
709
  removeScriptGifts = true,
710
- isGiftLineItem
710
+ isGiftLineItem,
711
+ runOnlyOnceAfterInit = false,
712
+ initDelay = 500
711
713
  } = options;
712
714
  const [isRemoving, setIsRemoving] = useState(false);
715
+ const [isInitialized, setIsInitialized] = useState(!runOnlyOnceAfterInit);
716
+ const [isFinished, setIsFinished] = useState(false);
713
717
  const { cart } = useCartContext();
714
718
  const { trigger: removeCartLines2 } = useRemoveCartLines();
715
719
  const giftsToRemove = useMemo(() => {
@@ -746,7 +750,29 @@ function useAutoRemoveFreeGifts(options = {}) {
746
750
  return false;
747
751
  });
748
752
  }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
753
+ useDebounceEffect(
754
+ () => {
755
+ if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
756
+ return;
757
+ }
758
+ if (!cart?.lineItems?.length) {
759
+ return;
760
+ }
761
+ setIsInitialized(true);
762
+ if (giftsToRemove.length === 0) {
763
+ setIsFinished(true);
764
+ }
765
+ },
766
+ [runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
767
+ {
768
+ trailing: true,
769
+ wait: initDelay
770
+ }
771
+ );
749
772
  useEffect(() => {
773
+ if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
774
+ return;
775
+ }
750
776
  if (isRemoving || giftsToRemove.length === 0) {
751
777
  return;
752
778
  }
@@ -760,10 +786,16 @@ function useAutoRemoveFreeGifts(options = {}) {
760
786
  console.error("Failed to remove free gifts:", error);
761
787
  } finally {
762
788
  setIsRemoving(false);
789
+ if (runOnlyOnceAfterInit) {
790
+ setIsFinished(true);
791
+ }
763
792
  }
764
793
  };
765
794
  performRemoval();
766
795
  }, [
796
+ runOnlyOnceAfterInit,
797
+ isInitialized,
798
+ isFinished,
767
799
  isRemoving,
768
800
  giftsToRemove,
769
801
  removeCartLines2