@anker-in/shopify-react 1.2.9-beta.1 → 1.2.9-beta.2

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
@@ -1697,7 +1729,17 @@ function getCartBasicAttributes({
1697
1729
  const presellAttributes = [
1698
1730
  {
1699
1731
  key: "_presale",
1700
- value: cart?.lineItems?.some((item) => item?.variant?.metafields?.presell === "presell") ? "true" : "false"
1732
+ value: cart?.lineItems?.some(
1733
+ (item) => item?.variant?.metafields?.global?.presell === "presell"
1734
+ ) ? "true" : "false"
1735
+ }
1736
+ ];
1737
+ const hideShippingAttributes = [
1738
+ {
1739
+ key: "_hide_shipping",
1740
+ value: cart?.lineItems?.some(
1741
+ (item) => item?.variant?.metafields?.global?.hideShipping === "true"
1742
+ ) ? "true" : "false"
1701
1743
  }
1702
1744
  ];
1703
1745
  const weightAttributes = [
@@ -1736,6 +1778,7 @@ function getCartBasicAttributes({
1736
1778
  ...functionAttributes,
1737
1779
  ...presellAttributes,
1738
1780
  ...weightAttributes,
1781
+ ...hideShippingAttributes,
1739
1782
  ...trackingAttributes,
1740
1783
  ...getReferralAttributes()
1741
1784
  ].filter((item) => item?.value !== void 0 && item?.value !== null);
@@ -2732,7 +2775,13 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
2732
2775
  cookieAdapter: cartCookieAdapter,
2733
2776
  buyerIdentity,
2734
2777
  discountCodes,
2735
- customAttributes: [...basicCartAttributes, ...ga4Attributes, ...customAttributes || []]
2778
+ customAttributes: [
2779
+ ...basicCartAttributes.filter(
2780
+ (a) => !(customAttributes || []).some((c) => c.key === a.key)
2781
+ ),
2782
+ ...ga4Attributes.filter((a) => !(customAttributes || []).some((c) => c.key === a.key)),
2783
+ ...customAttributes || []
2784
+ ]
2736
2785
  });
2737
2786
  if (!resultCart) {
2738
2787
  throw new Error("Failed to create cart for buy now");
@@ -2740,7 +2789,9 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
2740
2789
  if (withTrack && resultCart.lineItems) {
2741
2790
  trackBuyNowGA({
2742
2791
  lineItems: lineItems.map((item) => {
2743
- const cartLine = resultCart.lineItems.find((line) => line.variant.id === item.variant?.id);
2792
+ const cartLine = resultCart.lineItems.find(
2793
+ (line) => line.variant.id === item.variant?.id
2794
+ );
2744
2795
  if (!cartLine) {
2745
2796
  return null;
2746
2797
  }
@@ -3569,6 +3620,16 @@ function clearGeoLocationCache(cacheKey = "geoLocation") {
3569
3620
  }
3570
3621
  }
3571
3622
  var CartContext = createContext(null);
3623
+ function AutoRemoveGiftsHandler({
3624
+ options,
3625
+ onRemovingChange
3626
+ }) {
3627
+ const { isRemoving } = useAutoRemoveFreeGifts(options);
3628
+ useEffect(() => {
3629
+ onRemovingChange(isRemoving);
3630
+ }, [isRemoving, onRemovingChange]);
3631
+ return null;
3632
+ }
3572
3633
  function CartProvider({
3573
3634
  children,
3574
3635
  // swrOptions,
@@ -3596,6 +3657,7 @@ function CartProvider({
3596
3657
  });
3597
3658
  const [scriptAutoFreeGift, setScriptAutoFreeGift] = useState([]);
3598
3659
  const [functionAutoFreeGift, setFunctionAutoFreeGift] = useState([]);
3660
+ const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = useState(false);
3599
3661
  const {
3600
3662
  run: fetchCart,
3601
3663
  data: cart,
@@ -3755,7 +3817,9 @@ function CartProvider({
3755
3817
  const autoRemoveFreeGiftsOptions = useMemo(() => {
3756
3818
  return {
3757
3819
  removeFunctionGifts: !!functionAutoFreeGiftConfig,
3758
- removeScriptGifts: !!scriptAutoFreeGiftConfig
3820
+ removeScriptGifts: !!scriptAutoFreeGiftConfig,
3821
+ runOnlyOnceAfterInit: true,
3822
+ initDelay: 500
3759
3823
  };
3760
3824
  }, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
3761
3825
  const value = useMemo(
@@ -3788,7 +3852,8 @@ function CartProvider({
3788
3852
  metafieldIdentifiers,
3789
3853
  memberSetting,
3790
3854
  appContext,
3791
- autoRemoveFreeGiftsOptions
3855
+ autoRemoveFreeGiftsOptions,
3856
+ isAutoRemovingFreeGifts
3792
3857
  }),
3793
3858
  [
3794
3859
  cart,
@@ -3817,10 +3882,20 @@ function CartProvider({
3817
3882
  profile,
3818
3883
  memberSetting,
3819
3884
  appContext,
3820
- autoRemoveFreeGiftsOptions
3885
+ autoRemoveFreeGiftsOptions,
3886
+ isAutoRemovingFreeGifts
3821
3887
  ]
3822
3888
  );
3823
- return /* @__PURE__ */ jsx(CartContext.Provider, { value, children });
3889
+ return /* @__PURE__ */ jsxs(CartContext.Provider, { value, children: [
3890
+ (functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsx(
3891
+ AutoRemoveGiftsHandler,
3892
+ {
3893
+ options: autoRemoveFreeGiftsOptions,
3894
+ onRemovingChange: setIsAutoRemovingFreeGifts
3895
+ }
3896
+ ),
3897
+ children
3898
+ ] });
3824
3899
  }
3825
3900
  function useCartContext(options) {
3826
3901
  const context = useContext(CartContext);