@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.
@@ -4,7 +4,7 @@ import { ShopifyClient, ShopifyConfig, CartCookieAdapter, NormalizedCart, Attrib
4
4
  import { C as CookieAdapter, R as RouterAdapter, U as UserContextAdapter, P as PerformanceAdapter } from '../types-SKDHauqk.mjs';
5
5
  import * as react_jsx_runtime from 'react/jsx-runtime';
6
6
  import { SWRConfiguration } from 'swr';
7
- import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-CM5QrlnE.mjs';
7
+ import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-C0UyuPrG.mjs';
8
8
  import 'decimal.js';
9
9
 
10
10
  interface ShopifyContextValue {
@@ -4,7 +4,7 @@ import { ShopifyClient, ShopifyConfig, CartCookieAdapter, NormalizedCart, Attrib
4
4
  import { C as CookieAdapter, R as RouterAdapter, U as UserContextAdapter, P as PerformanceAdapter } from '../types-SKDHauqk.js';
5
5
  import * as react_jsx_runtime from 'react/jsx-runtime';
6
6
  import { SWRConfiguration } from 'swr';
7
- import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-CM5QrlnE.js';
7
+ import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-C0UyuPrG.js';
8
8
  import 'decimal.js';
9
9
 
10
10
  interface ShopifyContextValue {
@@ -804,9 +804,13 @@ function useAutoRemoveFreeGifts(options = {}) {
804
804
  const {
805
805
  removeFunctionGifts = true,
806
806
  removeScriptGifts = true,
807
- isGiftLineItem
807
+ isGiftLineItem,
808
+ runOnlyOnceAfterInit = false,
809
+ initDelay = 500
808
810
  } = options;
809
811
  const [isRemoving, setIsRemoving] = react.useState(false);
812
+ const [isInitialized, setIsInitialized] = react.useState(!runOnlyOnceAfterInit);
813
+ const [isFinished, setIsFinished] = react.useState(false);
810
814
  const { cart } = useCartContext();
811
815
  const { trigger: removeCartLines2 } = useRemoveCartLines();
812
816
  const giftsToRemove = react.useMemo(() => {
@@ -843,7 +847,29 @@ function useAutoRemoveFreeGifts(options = {}) {
843
847
  return false;
844
848
  });
845
849
  }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
850
+ ahooks.useDebounceEffect(
851
+ () => {
852
+ if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
853
+ return;
854
+ }
855
+ if (!cart?.lineItems?.length) {
856
+ return;
857
+ }
858
+ setIsInitialized(true);
859
+ if (giftsToRemove.length === 0) {
860
+ setIsFinished(true);
861
+ }
862
+ },
863
+ [runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
864
+ {
865
+ trailing: true,
866
+ wait: initDelay
867
+ }
868
+ );
846
869
  react.useEffect(() => {
870
+ if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
871
+ return;
872
+ }
847
873
  if (isRemoving || giftsToRemove.length === 0) {
848
874
  return;
849
875
  }
@@ -857,10 +883,16 @@ function useAutoRemoveFreeGifts(options = {}) {
857
883
  console.error("Failed to remove free gifts:", error);
858
884
  } finally {
859
885
  setIsRemoving(false);
886
+ if (runOnlyOnceAfterInit) {
887
+ setIsFinished(true);
888
+ }
860
889
  }
861
890
  };
862
891
  performRemoval();
863
892
  }, [
893
+ runOnlyOnceAfterInit,
894
+ isInitialized,
895
+ isFinished,
864
896
  isRemoving,
865
897
  giftsToRemove,
866
898
  removeCartLines2
@@ -1485,7 +1517,9 @@ function CartProvider({
1485
1517
  const autoRemoveFreeGiftsOptions = react.useMemo(() => {
1486
1518
  return {
1487
1519
  removeFunctionGifts: !!functionAutoFreeGiftConfig,
1488
- removeScriptGifts: !!scriptAutoFreeGiftConfig
1520
+ removeScriptGifts: !!scriptAutoFreeGiftConfig,
1521
+ runOnlyOnceAfterInit: true,
1522
+ initDelay: 500
1489
1523
  };
1490
1524
  }, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
1491
1525
  const value = react.useMemo(