@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 Decimal3 from 'decimal.js';
6
6
  import { btoaID, atobID } from '@anker-in/shopify-core';
7
7
  import useSWR from 'swr';
8
8
  import useSWRMutation5 from 'swr/mutation';
9
- import { useRequest } from 'ahooks';
9
+ import { useRequest, useDebounceEffect } from 'ahooks';
10
10
 
11
11
  // src/provider/context.ts
12
12
  var ShopifyContext = createContext(null);
@@ -795,9 +795,13 @@ function useAutoRemoveFreeGifts(options = {}) {
795
795
  const {
796
796
  removeFunctionGifts = true,
797
797
  removeScriptGifts = true,
798
- isGiftLineItem
798
+ isGiftLineItem,
799
+ runOnlyOnceAfterInit = false,
800
+ initDelay = 500
799
801
  } = options;
800
802
  const [isRemoving, setIsRemoving] = useState(false);
803
+ const [isInitialized, setIsInitialized] = useState(!runOnlyOnceAfterInit);
804
+ const [isFinished, setIsFinished] = useState(false);
801
805
  const { cart } = useCartContext();
802
806
  const { trigger: removeCartLines2 } = useRemoveCartLines();
803
807
  const giftsToRemove = useMemo(() => {
@@ -834,7 +838,29 @@ function useAutoRemoveFreeGifts(options = {}) {
834
838
  return false;
835
839
  });
836
840
  }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
841
+ useDebounceEffect(
842
+ () => {
843
+ if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
844
+ return;
845
+ }
846
+ if (!cart?.lineItems?.length) {
847
+ return;
848
+ }
849
+ setIsInitialized(true);
850
+ if (giftsToRemove.length === 0) {
851
+ setIsFinished(true);
852
+ }
853
+ },
854
+ [runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
855
+ {
856
+ trailing: true,
857
+ wait: initDelay
858
+ }
859
+ );
837
860
  useEffect(() => {
861
+ if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
862
+ return;
863
+ }
838
864
  if (isRemoving || giftsToRemove.length === 0) {
839
865
  return;
840
866
  }
@@ -848,10 +874,16 @@ function useAutoRemoveFreeGifts(options = {}) {
848
874
  console.error("Failed to remove free gifts:", error);
849
875
  } finally {
850
876
  setIsRemoving(false);
877
+ if (runOnlyOnceAfterInit) {
878
+ setIsFinished(true);
879
+ }
851
880
  }
852
881
  };
853
882
  performRemoval();
854
883
  }, [
884
+ runOnlyOnceAfterInit,
885
+ isInitialized,
886
+ isFinished,
855
887
  isRemoving,
856
888
  giftsToRemove,
857
889
  removeCartLines2
@@ -1476,7 +1508,9 @@ function CartProvider({
1476
1508
  const autoRemoveFreeGiftsOptions = useMemo(() => {
1477
1509
  return {
1478
1510
  removeFunctionGifts: !!functionAutoFreeGiftConfig,
1479
- removeScriptGifts: !!scriptAutoFreeGiftConfig
1511
+ removeScriptGifts: !!scriptAutoFreeGiftConfig,
1512
+ runOnlyOnceAfterInit: true,
1513
+ initDelay: 500
1480
1514
  };
1481
1515
  }, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
1482
1516
  const value = useMemo(