@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.
package/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@ import Decimal3 from 'decimal.js';
7
7
  import { atobID, btoaID } from '@anker-in/shopify-core';
8
8
  import useSWR from 'swr';
9
9
  import useSWRMutation from 'swr/mutation';
10
- import { useRequest } from 'ahooks';
10
+ import { useDebounceEffect, useRequest } from 'ahooks';
11
11
 
12
12
  // src/provider/context.ts
13
13
  var ShopifyContext = createContext(null);
@@ -1287,9 +1287,13 @@ function useAutoRemoveFreeGifts(options = {}) {
1287
1287
  const {
1288
1288
  removeFunctionGifts = true,
1289
1289
  removeScriptGifts = true,
1290
- isGiftLineItem
1290
+ isGiftLineItem,
1291
+ runOnlyOnceAfterInit = false,
1292
+ initDelay = 500
1291
1293
  } = options;
1292
1294
  const [isRemoving, setIsRemoving] = useState(false);
1295
+ const [isInitialized, setIsInitialized] = useState(!runOnlyOnceAfterInit);
1296
+ const [isFinished, setIsFinished] = useState(false);
1293
1297
  const { cart } = useCartContext();
1294
1298
  const { trigger: removeCartLines2 } = useRemoveCartLines();
1295
1299
  const giftsToRemove = useMemo(() => {
@@ -1326,7 +1330,29 @@ function useAutoRemoveFreeGifts(options = {}) {
1326
1330
  return false;
1327
1331
  });
1328
1332
  }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
1333
+ useDebounceEffect(
1334
+ () => {
1335
+ if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
1336
+ return;
1337
+ }
1338
+ if (!cart?.lineItems?.length) {
1339
+ return;
1340
+ }
1341
+ setIsInitialized(true);
1342
+ if (giftsToRemove.length === 0) {
1343
+ setIsFinished(true);
1344
+ }
1345
+ },
1346
+ [runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
1347
+ {
1348
+ trailing: true,
1349
+ wait: initDelay
1350
+ }
1351
+ );
1329
1352
  useEffect(() => {
1353
+ if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
1354
+ return;
1355
+ }
1330
1356
  if (isRemoving || giftsToRemove.length === 0) {
1331
1357
  return;
1332
1358
  }
@@ -1340,10 +1366,16 @@ function useAutoRemoveFreeGifts(options = {}) {
1340
1366
  console.error("Failed to remove free gifts:", error);
1341
1367
  } finally {
1342
1368
  setIsRemoving(false);
1369
+ if (runOnlyOnceAfterInit) {
1370
+ setIsFinished(true);
1371
+ }
1343
1372
  }
1344
1373
  };
1345
1374
  performRemoval();
1346
1375
  }, [
1376
+ runOnlyOnceAfterInit,
1377
+ isInitialized,
1378
+ isFinished,
1347
1379
  isRemoving,
1348
1380
  giftsToRemove,
1349
1381
  removeCartLines2
@@ -3738,7 +3770,9 @@ function CartProvider({
3738
3770
  const autoRemoveFreeGiftsOptions = useMemo(() => {
3739
3771
  return {
3740
3772
  removeFunctionGifts: !!functionAutoFreeGiftConfig,
3741
- removeScriptGifts: !!scriptAutoFreeGiftConfig
3773
+ removeScriptGifts: !!scriptAutoFreeGiftConfig,
3774
+ runOnlyOnceAfterInit: true,
3775
+ initDelay: 500
3742
3776
  };
3743
3777
  }, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
3744
3778
  const value = useMemo(