@anker-in/shopify-react 1.2.4-beta.0 → 1.2.5

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
@@ -2,12 +2,12 @@ import { createContext, useMemo, useContext, useRef, useState, useEffect, useCal
2
2
  import { createShopifyClient, getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, getLocalStorage, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, getCart, setLocalStorage } from '@anker-in/shopify-sdk';
3
3
  export * from '@anker-in/shopify-sdk';
4
4
  import Cookies5 from 'js-cookie';
5
- import { jsx } from 'react/jsx-runtime';
5
+ import { jsx, jsxs } 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
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);
@@ -1307,9 +1307,13 @@ function useAutoRemoveFreeGifts(options = {}) {
1307
1307
  const {
1308
1308
  removeFunctionGifts = true,
1309
1309
  removeScriptGifts = true,
1310
- isGiftLineItem
1310
+ isGiftLineItem,
1311
+ runOnlyOnceAfterInit = false,
1312
+ initDelay = 500
1311
1313
  } = options;
1312
1314
  const [isRemoving, setIsRemoving] = useState(false);
1315
+ const [isInitialized, setIsInitialized] = useState(!runOnlyOnceAfterInit);
1316
+ const [isFinished, setIsFinished] = useState(false);
1313
1317
  const { cart } = useCartContext();
1314
1318
  const { trigger: removeCartLines2 } = useRemoveCartLines();
1315
1319
  const giftsToRemove = useMemo(() => {
@@ -1346,7 +1350,29 @@ function useAutoRemoveFreeGifts(options = {}) {
1346
1350
  return false;
1347
1351
  });
1348
1352
  }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
1353
+ useDebounceEffect(
1354
+ () => {
1355
+ if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
1356
+ return;
1357
+ }
1358
+ if (!cart?.lineItems?.length) {
1359
+ return;
1360
+ }
1361
+ setIsInitialized(true);
1362
+ if (giftsToRemove.length === 0) {
1363
+ setIsFinished(true);
1364
+ }
1365
+ },
1366
+ [runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
1367
+ {
1368
+ trailing: true,
1369
+ wait: initDelay
1370
+ }
1371
+ );
1349
1372
  useEffect(() => {
1373
+ if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
1374
+ return;
1375
+ }
1350
1376
  if (isRemoving || giftsToRemove.length === 0) {
1351
1377
  return;
1352
1378
  }
@@ -1360,10 +1386,16 @@ function useAutoRemoveFreeGifts(options = {}) {
1360
1386
  console.error("Failed to remove free gifts:", error);
1361
1387
  } finally {
1362
1388
  setIsRemoving(false);
1389
+ if (runOnlyOnceAfterInit) {
1390
+ setIsFinished(true);
1391
+ }
1363
1392
  }
1364
1393
  };
1365
1394
  performRemoval();
1366
1395
  }, [
1396
+ runOnlyOnceAfterInit,
1397
+ isInitialized,
1398
+ isFinished,
1367
1399
  isRemoving,
1368
1400
  giftsToRemove,
1369
1401
  removeCartLines2
@@ -3569,6 +3601,16 @@ function clearGeoLocationCache(cacheKey = "geoLocation") {
3569
3601
  }
3570
3602
  }
3571
3603
  var CartContext = createContext(null);
3604
+ function AutoRemoveGiftsHandler({
3605
+ options,
3606
+ onRemovingChange
3607
+ }) {
3608
+ const { isRemoving } = useAutoRemoveFreeGifts(options);
3609
+ useEffect(() => {
3610
+ onRemovingChange(isRemoving);
3611
+ }, [isRemoving, onRemovingChange]);
3612
+ return null;
3613
+ }
3572
3614
  function CartProvider({
3573
3615
  children,
3574
3616
  // swrOptions,
@@ -3596,6 +3638,7 @@ function CartProvider({
3596
3638
  });
3597
3639
  const [scriptAutoFreeGift, setScriptAutoFreeGift] = useState([]);
3598
3640
  const [functionAutoFreeGift, setFunctionAutoFreeGift] = useState([]);
3641
+ const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = useState(false);
3599
3642
  const {
3600
3643
  run: fetchCart,
3601
3644
  data: cart,
@@ -3755,7 +3798,9 @@ function CartProvider({
3755
3798
  const autoRemoveFreeGiftsOptions = useMemo(() => {
3756
3799
  return {
3757
3800
  removeFunctionGifts: !!functionAutoFreeGiftConfig,
3758
- removeScriptGifts: !!scriptAutoFreeGiftConfig
3801
+ removeScriptGifts: !!scriptAutoFreeGiftConfig,
3802
+ runOnlyOnceAfterInit: true,
3803
+ initDelay: 500
3759
3804
  };
3760
3805
  }, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
3761
3806
  const value = useMemo(
@@ -3788,7 +3833,8 @@ function CartProvider({
3788
3833
  metafieldIdentifiers,
3789
3834
  memberSetting,
3790
3835
  appContext,
3791
- autoRemoveFreeGiftsOptions
3836
+ autoRemoveFreeGiftsOptions,
3837
+ isAutoRemovingFreeGifts
3792
3838
  }),
3793
3839
  [
3794
3840
  cart,
@@ -3817,10 +3863,20 @@ function CartProvider({
3817
3863
  profile,
3818
3864
  memberSetting,
3819
3865
  appContext,
3820
- autoRemoveFreeGiftsOptions
3866
+ autoRemoveFreeGiftsOptions,
3867
+ isAutoRemovingFreeGifts
3821
3868
  ]
3822
3869
  );
3823
- return /* @__PURE__ */ jsx(CartContext.Provider, { value, children });
3870
+ return /* @__PURE__ */ jsxs(CartContext.Provider, { value, children: [
3871
+ (functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsx(
3872
+ AutoRemoveGiftsHandler,
3873
+ {
3874
+ options: autoRemoveFreeGiftsOptions,
3875
+ onRemovingChange: setIsAutoRemovingFreeGifts
3876
+ }
3877
+ ),
3878
+ children
3879
+ ] });
3824
3880
  }
3825
3881
  function useCartContext(options) {
3826
3882
  const context = useContext(CartContext);