@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.
@@ -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");
@@ -864,7 +1006,17 @@ function getCartBasicAttributes({
864
1006
  const presellAttributes = [
865
1007
  {
866
1008
  key: "_presale",
867
- value: cart?.lineItems?.some((item) => item?.variant?.metafields?.presell === "presell") ? "true" : "false"
1009
+ value: cart?.lineItems?.some(
1010
+ (item) => item?.variant?.metafields?.global?.presell === "presell"
1011
+ ) ? "true" : "false"
1012
+ }
1013
+ ];
1014
+ const hideShippingAttributes = [
1015
+ {
1016
+ key: "_hide_shipping",
1017
+ value: cart?.lineItems?.some(
1018
+ (item) => item?.variant?.metafields?.global?.hideShipping === "true"
1019
+ ) ? "true" : "false"
868
1020
  }
869
1021
  ];
870
1022
  const weightAttributes = [
@@ -903,6 +1055,7 @@ function getCartBasicAttributes({
903
1055
  ...functionAttributes,
904
1056
  ...presellAttributes,
905
1057
  ...weightAttributes,
1058
+ ...hideShippingAttributes,
906
1059
  ...trackingAttributes,
907
1060
  ...getReferralAttributes()
908
1061
  ].filter((item) => item?.value !== void 0 && item?.value !== null);
@@ -1166,9 +1319,19 @@ function useUpdateCartAttributes({
1166
1319
  },
1167
1320
  [client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
1168
1321
  );
1169
- return useSWRMutation8("update-cart-attributes", updateAttributes, swrOptions);
1322
+ return useSWRMutation6("update-cart-attributes", updateAttributes, swrOptions);
1170
1323
  }
1171
1324
  var CartContext = createContext(null);
1325
+ function AutoRemoveGiftsHandler({
1326
+ options,
1327
+ onRemovingChange
1328
+ }) {
1329
+ const { isRemoving } = useAutoRemoveFreeGifts(options);
1330
+ useEffect(() => {
1331
+ onRemovingChange(isRemoving);
1332
+ }, [isRemoving, onRemovingChange]);
1333
+ return null;
1334
+ }
1172
1335
  function CartProvider({
1173
1336
  children,
1174
1337
  // swrOptions,
@@ -1196,6 +1359,7 @@ function CartProvider({
1196
1359
  });
1197
1360
  const [scriptAutoFreeGift, setScriptAutoFreeGift] = useState([]);
1198
1361
  const [functionAutoFreeGift, setFunctionAutoFreeGift] = useState([]);
1362
+ const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = useState(false);
1199
1363
  const {
1200
1364
  run: fetchCart,
1201
1365
  data: cart,
@@ -1355,7 +1519,9 @@ function CartProvider({
1355
1519
  const autoRemoveFreeGiftsOptions = useMemo(() => {
1356
1520
  return {
1357
1521
  removeFunctionGifts: !!functionAutoFreeGiftConfig,
1358
- removeScriptGifts: !!scriptAutoFreeGiftConfig
1522
+ removeScriptGifts: !!scriptAutoFreeGiftConfig,
1523
+ runOnlyOnceAfterInit: true,
1524
+ initDelay: 500
1359
1525
  };
1360
1526
  }, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
1361
1527
  const value = useMemo(
@@ -1388,7 +1554,8 @@ function CartProvider({
1388
1554
  metafieldIdentifiers,
1389
1555
  memberSetting,
1390
1556
  appContext,
1391
- autoRemoveFreeGiftsOptions
1557
+ autoRemoveFreeGiftsOptions,
1558
+ isAutoRemovingFreeGifts
1392
1559
  }),
1393
1560
  [
1394
1561
  cart,
@@ -1417,10 +1584,20 @@ function CartProvider({
1417
1584
  profile,
1418
1585
  memberSetting,
1419
1586
  appContext,
1420
- autoRemoveFreeGiftsOptions
1587
+ autoRemoveFreeGiftsOptions,
1588
+ isAutoRemovingFreeGifts
1421
1589
  ]
1422
1590
  );
1423
- return /* @__PURE__ */ jsx(CartContext.Provider, { value, children });
1591
+ return /* @__PURE__ */ jsxs(CartContext.Provider, { value, children: [
1592
+ (functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsx(
1593
+ AutoRemoveGiftsHandler,
1594
+ {
1595
+ options: autoRemoveFreeGiftsOptions,
1596
+ onRemovingChange: setIsAutoRemovingFreeGifts
1597
+ }
1598
+ ),
1599
+ children
1600
+ ] });
1424
1601
  }
1425
1602
  function useCartContext(options) {
1426
1603
  const context = useContext(CartContext);