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

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, removeCartLines, updateCartCodes } from '@anker-in/shopify-sdk';
2
+ import { createShopifyClient, getCart, updateCartAttributes, updateCartLines, getProductsByHandles, getLocalStorage } from '@anker-in/shopify-sdk';
3
3
  import Cookies5 from 'js-cookie';
4
- import { jsx, jsxs } from 'react/jsx-runtime';
4
+ import { jsx } 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 useSWRMutation6 from 'swr/mutation';
9
- import { useRequest, useDebounceEffect } from 'ahooks';
8
+ import useSWRMutation8 from 'swr/mutation';
9
+ import { useRequest } from 'ahooks';
10
10
 
11
11
  // src/provider/context.ts
12
12
  var ShopifyContext = createContext(null);
@@ -750,148 +750,6 @@ 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
- }
895
753
  var getReferralAttributes = () => {
896
754
  const inviteCode = getLocalStorage("inviteCode") || Cookies5.get("inviteCode");
897
755
  const playModeId = getLocalStorage("playModeId") || Cookies5.get("playModeId");
@@ -1308,19 +1166,9 @@ function useUpdateCartAttributes({
1308
1166
  },
1309
1167
  [client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
1310
1168
  );
1311
- return useSWRMutation6("update-cart-attributes", updateAttributes, swrOptions);
1169
+ return useSWRMutation8("update-cart-attributes", updateAttributes, swrOptions);
1312
1170
  }
1313
1171
  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
- }
1324
1172
  function CartProvider({
1325
1173
  children,
1326
1174
  // swrOptions,
@@ -1348,7 +1196,6 @@ function CartProvider({
1348
1196
  });
1349
1197
  const [scriptAutoFreeGift, setScriptAutoFreeGift] = useState([]);
1350
1198
  const [functionAutoFreeGift, setFunctionAutoFreeGift] = useState([]);
1351
- const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = useState(false);
1352
1199
  const {
1353
1200
  run: fetchCart,
1354
1201
  data: cart,
@@ -1508,9 +1355,7 @@ function CartProvider({
1508
1355
  const autoRemoveFreeGiftsOptions = useMemo(() => {
1509
1356
  return {
1510
1357
  removeFunctionGifts: !!functionAutoFreeGiftConfig,
1511
- removeScriptGifts: !!scriptAutoFreeGiftConfig,
1512
- runOnlyOnceAfterInit: true,
1513
- initDelay: 500
1358
+ removeScriptGifts: !!scriptAutoFreeGiftConfig
1514
1359
  };
1515
1360
  }, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
1516
1361
  const value = useMemo(
@@ -1543,8 +1388,7 @@ function CartProvider({
1543
1388
  metafieldIdentifiers,
1544
1389
  memberSetting,
1545
1390
  appContext,
1546
- autoRemoveFreeGiftsOptions,
1547
- isAutoRemovingFreeGifts
1391
+ autoRemoveFreeGiftsOptions
1548
1392
  }),
1549
1393
  [
1550
1394
  cart,
@@ -1573,20 +1417,10 @@ function CartProvider({
1573
1417
  profile,
1574
1418
  memberSetting,
1575
1419
  appContext,
1576
- autoRemoveFreeGiftsOptions,
1577
- isAutoRemovingFreeGifts
1420
+ autoRemoveFreeGiftsOptions
1578
1421
  ]
1579
1422
  );
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
- ] });
1423
+ return /* @__PURE__ */ jsx(CartContext.Provider, { value, children });
1590
1424
  }
1591
1425
  function useCartContext(options) {
1592
1426
  const context = useContext(CartContext);