@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.
@@ -1,12 +1,12 @@
1
1
  import { createContext, useMemo, useContext, useState, useCallback, useEffect, useRef } from 'react';
2
- import { createShopifyClient, getCart, updateCartAttributes, updateCartLines, getProductsByHandles, getLocalStorage } from '@anker-in/shopify-sdk';
2
+ import { createShopifyClient, getCart, updateCartAttributes, updateCartLines, getProductsByHandles, getLocalStorage, removeCartLines, updateCartCodes } from '@anker-in/shopify-sdk';
3
3
  import Cookies5 from 'js-cookie';
4
- import { jsx } from 'react/jsx-runtime';
4
+ import { jsx, jsxs } from 'react/jsx-runtime';
5
5
  import Decimal3 from 'decimal.js';
6
6
  import { btoaID, atobID } from '@anker-in/shopify-core';
7
7
  import useSWR from 'swr';
8
- import useSWRMutation8 from 'swr/mutation';
9
- import { useRequest } from 'ahooks';
8
+ import useSWRMutation6 from 'swr/mutation';
9
+ import { useRequest, useDebounceEffect } from 'ahooks';
10
10
 
11
11
  // src/provider/context.ts
12
12
  var ShopifyContext = createContext(null);
@@ -750,6 +750,148 @@ var getGA4Data = async (measurementId = "G-R0BRMRK4CY") => {
750
750
  };
751
751
  }
752
752
  };
753
+ function useRemoveCartLines(options) {
754
+ const { client, locale, cartCookieAdapter } = useShopify();
755
+ const { mutateCart, metafieldIdentifiers } = useCartContext();
756
+ const removeLines = useCallback(
757
+ async (_key, { arg }) => {
758
+ const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
759
+ let updatedCart = await removeCartLines(client, {
760
+ cartId,
761
+ lineIds,
762
+ metafieldIdentifiers,
763
+ cookieAdapter: cartCookieAdapter
764
+ });
765
+ if (updatedCart && autoRemoveInvalidCodes) {
766
+ const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
767
+ if (unApplicableCodes.length > 0) {
768
+ if (onCodesRemoved) {
769
+ const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
770
+ if (handledCart) {
771
+ updatedCart = handledCart;
772
+ }
773
+ } else {
774
+ updatedCart = await updateCartCodes(client, {
775
+ cartId: updatedCart.id,
776
+ discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
777
+ metafieldIdentifiers,
778
+ cookieAdapter: cartCookieAdapter
779
+ }) || updatedCart;
780
+ }
781
+ }
782
+ }
783
+ if (updatedCart) {
784
+ mutateCart(updatedCart);
785
+ }
786
+ return updatedCart;
787
+ },
788
+ [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
789
+ );
790
+ return useSWRMutation6("remove-cart-lines", removeLines, options);
791
+ }
792
+
793
+ // src/hooks/cart/feature/use-auto-remove-free-gifts.ts
794
+ function useAutoRemoveFreeGifts(options = {}) {
795
+ const {
796
+ removeFunctionGifts = true,
797
+ removeScriptGifts = true,
798
+ isGiftLineItem,
799
+ runOnlyOnceAfterInit = false,
800
+ initDelay = 500
801
+ } = options;
802
+ const [isRemoving, setIsRemoving] = useState(false);
803
+ const [isInitialized, setIsInitialized] = useState(!runOnlyOnceAfterInit);
804
+ const [isFinished, setIsFinished] = useState(false);
805
+ const { cart } = useCartContext();
806
+ const { trigger: removeCartLines2 } = useRemoveCartLines();
807
+ const giftsToRemove = useMemo(() => {
808
+ if (!cart?.lineItems) {
809
+ return [];
810
+ }
811
+ return cart.lineItems.filter((item) => {
812
+ if (removeFunctionGifts) {
813
+ const functionAttr = item.customAttributes?.find(
814
+ (attr) => attr.key === "_discounts_function_env"
815
+ )?.value;
816
+ if (functionAttr) {
817
+ try {
818
+ const functionAttrObj = JSON.parse(functionAttr);
819
+ if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
820
+ return true;
821
+ }
822
+ } catch (error) {
823
+ console.error("Failed to parse _discounts_function_env:", error);
824
+ }
825
+ }
826
+ }
827
+ if (removeScriptGifts) {
828
+ const scriptGiftAttr = item.customAttributes?.find(
829
+ (attr) => attr.key === "_giveaway_gradient_gifts"
830
+ );
831
+ if (scriptGiftAttr) {
832
+ return true;
833
+ }
834
+ }
835
+ if (isGiftLineItem && isGiftLineItem(item)) {
836
+ return true;
837
+ }
838
+ return false;
839
+ });
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
+ );
860
+ useEffect(() => {
861
+ if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
862
+ return;
863
+ }
864
+ if (isRemoving || giftsToRemove.length === 0) {
865
+ return;
866
+ }
867
+ const performRemoval = async () => {
868
+ setIsRemoving(true);
869
+ try {
870
+ await removeCartLines2({
871
+ lineIds: giftsToRemove.map((item) => item.id)
872
+ });
873
+ } catch (error) {
874
+ console.error("Failed to remove free gifts:", error);
875
+ } finally {
876
+ setIsRemoving(false);
877
+ if (runOnlyOnceAfterInit) {
878
+ setIsFinished(true);
879
+ }
880
+ }
881
+ };
882
+ performRemoval();
883
+ }, [
884
+ runOnlyOnceAfterInit,
885
+ isInitialized,
886
+ isFinished,
887
+ isRemoving,
888
+ giftsToRemove,
889
+ removeCartLines2
890
+ ]);
891
+ return {
892
+ isRemoving
893
+ };
894
+ }
753
895
  var getReferralAttributes = () => {
754
896
  const inviteCode = getLocalStorage("inviteCode") || Cookies5.get("inviteCode");
755
897
  const playModeId = getLocalStorage("playModeId") || Cookies5.get("playModeId");
@@ -1166,9 +1308,19 @@ function useUpdateCartAttributes({
1166
1308
  },
1167
1309
  [client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
1168
1310
  );
1169
- return useSWRMutation8("update-cart-attributes", updateAttributes, swrOptions);
1311
+ return useSWRMutation6("update-cart-attributes", updateAttributes, swrOptions);
1170
1312
  }
1171
1313
  var CartContext = createContext(null);
1314
+ function AutoRemoveGiftsHandler({
1315
+ options,
1316
+ onRemovingChange
1317
+ }) {
1318
+ const { isRemoving } = useAutoRemoveFreeGifts(options);
1319
+ useEffect(() => {
1320
+ onRemovingChange(isRemoving);
1321
+ }, [isRemoving, onRemovingChange]);
1322
+ return null;
1323
+ }
1172
1324
  function CartProvider({
1173
1325
  children,
1174
1326
  // swrOptions,
@@ -1196,6 +1348,7 @@ function CartProvider({
1196
1348
  });
1197
1349
  const [scriptAutoFreeGift, setScriptAutoFreeGift] = useState([]);
1198
1350
  const [functionAutoFreeGift, setFunctionAutoFreeGift] = useState([]);
1351
+ const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = useState(false);
1199
1352
  const {
1200
1353
  run: fetchCart,
1201
1354
  data: cart,
@@ -1355,7 +1508,9 @@ function CartProvider({
1355
1508
  const autoRemoveFreeGiftsOptions = useMemo(() => {
1356
1509
  return {
1357
1510
  removeFunctionGifts: !!functionAutoFreeGiftConfig,
1358
- removeScriptGifts: !!scriptAutoFreeGiftConfig
1511
+ removeScriptGifts: !!scriptAutoFreeGiftConfig,
1512
+ runOnlyOnceAfterInit: true,
1513
+ initDelay: 500
1359
1514
  };
1360
1515
  }, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
1361
1516
  const value = useMemo(
@@ -1388,7 +1543,8 @@ function CartProvider({
1388
1543
  metafieldIdentifiers,
1389
1544
  memberSetting,
1390
1545
  appContext,
1391
- autoRemoveFreeGiftsOptions
1546
+ autoRemoveFreeGiftsOptions,
1547
+ isAutoRemovingFreeGifts
1392
1548
  }),
1393
1549
  [
1394
1550
  cart,
@@ -1417,10 +1573,20 @@ function CartProvider({
1417
1573
  profile,
1418
1574
  memberSetting,
1419
1575
  appContext,
1420
- autoRemoveFreeGiftsOptions
1576
+ autoRemoveFreeGiftsOptions,
1577
+ isAutoRemovingFreeGifts
1421
1578
  ]
1422
1579
  );
1423
- return /* @__PURE__ */ jsx(CartContext.Provider, { value, children });
1580
+ return /* @__PURE__ */ jsxs(CartContext.Provider, { value, children: [
1581
+ (functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsx(
1582
+ AutoRemoveGiftsHandler,
1583
+ {
1584
+ options: autoRemoveFreeGiftsOptions,
1585
+ onRemovingChange: setIsAutoRemovingFreeGifts
1586
+ }
1587
+ ),
1588
+ children
1589
+ ] });
1424
1590
  }
1425
1591
  function useCartContext(options) {
1426
1592
  const context = useContext(CartContext);