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

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.
@@ -4,7 +4,7 @@ import { ShopifyClient, ShopifyConfig, CartCookieAdapter, NormalizedCart, Attrib
4
4
  import { C as CookieAdapter, R as RouterAdapter, U as UserContextAdapter, P as PerformanceAdapter } from '../types-SKDHauqk.mjs';
5
5
  import * as react_jsx_runtime from 'react/jsx-runtime';
6
6
  import { SWRConfiguration } from 'swr';
7
- import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-DfR13pDe.mjs';
7
+ import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-C0UyuPrG.mjs';
8
8
  import 'decimal.js';
9
9
 
10
10
  interface ShopifyContextValue {
@@ -117,6 +117,8 @@ interface CartContextValue {
117
117
  appContext?: AppContext;
118
118
  /** Auto remove free gifts options */
119
119
  autoRemoveFreeGiftsOptions?: UseAutoRemoveFreeGiftsOptions;
120
+ /** Whether auto removing free gifts is in progress */
121
+ isAutoRemovingFreeGifts: boolean;
120
122
  }
121
123
  interface CartProviderProps {
122
124
  children: React__default.ReactNode;
@@ -4,7 +4,7 @@ import { ShopifyClient, ShopifyConfig, CartCookieAdapter, NormalizedCart, Attrib
4
4
  import { C as CookieAdapter, R as RouterAdapter, U as UserContextAdapter, P as PerformanceAdapter } from '../types-SKDHauqk.js';
5
5
  import * as react_jsx_runtime from 'react/jsx-runtime';
6
6
  import { SWRConfiguration } from 'swr';
7
- import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-DfR13pDe.js';
7
+ import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-C0UyuPrG.js';
8
8
  import 'decimal.js';
9
9
 
10
10
  interface ShopifyContextValue {
@@ -117,6 +117,8 @@ interface CartContextValue {
117
117
  appContext?: AppContext;
118
118
  /** Auto remove free gifts options */
119
119
  autoRemoveFreeGiftsOptions?: UseAutoRemoveFreeGiftsOptions;
120
+ /** Whether auto removing free gifts is in progress */
121
+ isAutoRemovingFreeGifts: boolean;
120
122
  }
121
123
  interface CartProviderProps {
122
124
  children: React__default.ReactNode;
@@ -7,7 +7,7 @@ var jsxRuntime = require('react/jsx-runtime');
7
7
  var Decimal3 = require('decimal.js');
8
8
  var shopifyCore = require('@anker-in/shopify-core');
9
9
  var useSWR = require('swr');
10
- var useSWRMutation8 = require('swr/mutation');
10
+ var useSWRMutation6 = require('swr/mutation');
11
11
  var ahooks = require('ahooks');
12
12
 
13
13
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -15,7 +15,7 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
15
15
  var Cookies5__default = /*#__PURE__*/_interopDefault(Cookies5);
16
16
  var Decimal3__default = /*#__PURE__*/_interopDefault(Decimal3);
17
17
  var useSWR__default = /*#__PURE__*/_interopDefault(useSWR);
18
- var useSWRMutation8__default = /*#__PURE__*/_interopDefault(useSWRMutation8);
18
+ var useSWRMutation6__default = /*#__PURE__*/_interopDefault(useSWRMutation6);
19
19
 
20
20
  // src/provider/context.ts
21
21
  var ShopifyContext = react.createContext(null);
@@ -759,6 +759,148 @@ var getGA4Data = async (measurementId = "G-R0BRMRK4CY") => {
759
759
  };
760
760
  }
761
761
  };
762
+ function useRemoveCartLines(options) {
763
+ const { client, locale, cartCookieAdapter } = useShopify();
764
+ const { mutateCart, metafieldIdentifiers } = useCartContext();
765
+ const removeLines = react.useCallback(
766
+ async (_key, { arg }) => {
767
+ const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
768
+ let updatedCart = await shopifySdk.removeCartLines(client, {
769
+ cartId,
770
+ lineIds,
771
+ metafieldIdentifiers,
772
+ cookieAdapter: cartCookieAdapter
773
+ });
774
+ if (updatedCart && autoRemoveInvalidCodes) {
775
+ const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
776
+ if (unApplicableCodes.length > 0) {
777
+ if (onCodesRemoved) {
778
+ const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
779
+ if (handledCart) {
780
+ updatedCart = handledCart;
781
+ }
782
+ } else {
783
+ updatedCart = await shopifySdk.updateCartCodes(client, {
784
+ cartId: updatedCart.id,
785
+ discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
786
+ metafieldIdentifiers,
787
+ cookieAdapter: cartCookieAdapter
788
+ }) || updatedCart;
789
+ }
790
+ }
791
+ }
792
+ if (updatedCart) {
793
+ mutateCart(updatedCart);
794
+ }
795
+ return updatedCart;
796
+ },
797
+ [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
798
+ );
799
+ return useSWRMutation6__default.default("remove-cart-lines", removeLines, options);
800
+ }
801
+
802
+ // src/hooks/cart/feature/use-auto-remove-free-gifts.ts
803
+ function useAutoRemoveFreeGifts(options = {}) {
804
+ const {
805
+ removeFunctionGifts = true,
806
+ removeScriptGifts = true,
807
+ isGiftLineItem,
808
+ runOnlyOnceAfterInit = false,
809
+ initDelay = 500
810
+ } = options;
811
+ const [isRemoving, setIsRemoving] = react.useState(false);
812
+ const [isInitialized, setIsInitialized] = react.useState(!runOnlyOnceAfterInit);
813
+ const [isFinished, setIsFinished] = react.useState(false);
814
+ const { cart } = useCartContext();
815
+ const { trigger: removeCartLines2 } = useRemoveCartLines();
816
+ const giftsToRemove = react.useMemo(() => {
817
+ if (!cart?.lineItems) {
818
+ return [];
819
+ }
820
+ return cart.lineItems.filter((item) => {
821
+ if (removeFunctionGifts) {
822
+ const functionAttr = item.customAttributes?.find(
823
+ (attr) => attr.key === "_discounts_function_env"
824
+ )?.value;
825
+ if (functionAttr) {
826
+ try {
827
+ const functionAttrObj = JSON.parse(functionAttr);
828
+ if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
829
+ return true;
830
+ }
831
+ } catch (error) {
832
+ console.error("Failed to parse _discounts_function_env:", error);
833
+ }
834
+ }
835
+ }
836
+ if (removeScriptGifts) {
837
+ const scriptGiftAttr = item.customAttributes?.find(
838
+ (attr) => attr.key === "_giveaway_gradient_gifts"
839
+ );
840
+ if (scriptGiftAttr) {
841
+ return true;
842
+ }
843
+ }
844
+ if (isGiftLineItem && isGiftLineItem(item)) {
845
+ return true;
846
+ }
847
+ return false;
848
+ });
849
+ }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
850
+ ahooks.useDebounceEffect(
851
+ () => {
852
+ if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
853
+ return;
854
+ }
855
+ if (!cart?.lineItems?.length) {
856
+ return;
857
+ }
858
+ setIsInitialized(true);
859
+ if (giftsToRemove.length === 0) {
860
+ setIsFinished(true);
861
+ }
862
+ },
863
+ [runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
864
+ {
865
+ trailing: true,
866
+ wait: initDelay
867
+ }
868
+ );
869
+ react.useEffect(() => {
870
+ if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
871
+ return;
872
+ }
873
+ if (isRemoving || giftsToRemove.length === 0) {
874
+ return;
875
+ }
876
+ const performRemoval = async () => {
877
+ setIsRemoving(true);
878
+ try {
879
+ await removeCartLines2({
880
+ lineIds: giftsToRemove.map((item) => item.id)
881
+ });
882
+ } catch (error) {
883
+ console.error("Failed to remove free gifts:", error);
884
+ } finally {
885
+ setIsRemoving(false);
886
+ if (runOnlyOnceAfterInit) {
887
+ setIsFinished(true);
888
+ }
889
+ }
890
+ };
891
+ performRemoval();
892
+ }, [
893
+ runOnlyOnceAfterInit,
894
+ isInitialized,
895
+ isFinished,
896
+ isRemoving,
897
+ giftsToRemove,
898
+ removeCartLines2
899
+ ]);
900
+ return {
901
+ isRemoving
902
+ };
903
+ }
762
904
  var getReferralAttributes = () => {
763
905
  const inviteCode = shopifySdk.getLocalStorage("inviteCode") || Cookies5__default.default.get("inviteCode");
764
906
  const playModeId = shopifySdk.getLocalStorage("playModeId") || Cookies5__default.default.get("playModeId");
@@ -873,7 +1015,17 @@ function getCartBasicAttributes({
873
1015
  const presellAttributes = [
874
1016
  {
875
1017
  key: "_presale",
876
- value: cart?.lineItems?.some((item) => item?.variant?.metafields?.presell === "presell") ? "true" : "false"
1018
+ value: cart?.lineItems?.some(
1019
+ (item) => item?.variant?.metafields?.global?.presell === "presell"
1020
+ ) ? "true" : "false"
1021
+ }
1022
+ ];
1023
+ const hideShippingAttributes = [
1024
+ {
1025
+ key: "_hide_shipping",
1026
+ value: cart?.lineItems?.some(
1027
+ (item) => item?.variant?.metafields?.global?.hideShipping === "true"
1028
+ ) ? "true" : "false"
877
1029
  }
878
1030
  ];
879
1031
  const weightAttributes = [
@@ -912,6 +1064,7 @@ function getCartBasicAttributes({
912
1064
  ...functionAttributes,
913
1065
  ...presellAttributes,
914
1066
  ...weightAttributes,
1067
+ ...hideShippingAttributes,
915
1068
  ...trackingAttributes,
916
1069
  ...getReferralAttributes()
917
1070
  ].filter((item) => item?.value !== void 0 && item?.value !== null);
@@ -1175,9 +1328,19 @@ function useUpdateCartAttributes({
1175
1328
  },
1176
1329
  [client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
1177
1330
  );
1178
- return useSWRMutation8__default.default("update-cart-attributes", updateAttributes, swrOptions);
1331
+ return useSWRMutation6__default.default("update-cart-attributes", updateAttributes, swrOptions);
1179
1332
  }
1180
1333
  var CartContext = react.createContext(null);
1334
+ function AutoRemoveGiftsHandler({
1335
+ options,
1336
+ onRemovingChange
1337
+ }) {
1338
+ const { isRemoving } = useAutoRemoveFreeGifts(options);
1339
+ react.useEffect(() => {
1340
+ onRemovingChange(isRemoving);
1341
+ }, [isRemoving, onRemovingChange]);
1342
+ return null;
1343
+ }
1181
1344
  function CartProvider({
1182
1345
  children,
1183
1346
  // swrOptions,
@@ -1205,6 +1368,7 @@ function CartProvider({
1205
1368
  });
1206
1369
  const [scriptAutoFreeGift, setScriptAutoFreeGift] = react.useState([]);
1207
1370
  const [functionAutoFreeGift, setFunctionAutoFreeGift] = react.useState([]);
1371
+ const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = react.useState(false);
1208
1372
  const {
1209
1373
  run: fetchCart,
1210
1374
  data: cart,
@@ -1364,7 +1528,9 @@ function CartProvider({
1364
1528
  const autoRemoveFreeGiftsOptions = react.useMemo(() => {
1365
1529
  return {
1366
1530
  removeFunctionGifts: !!functionAutoFreeGiftConfig,
1367
- removeScriptGifts: !!scriptAutoFreeGiftConfig
1531
+ removeScriptGifts: !!scriptAutoFreeGiftConfig,
1532
+ runOnlyOnceAfterInit: true,
1533
+ initDelay: 500
1368
1534
  };
1369
1535
  }, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
1370
1536
  const value = react.useMemo(
@@ -1397,7 +1563,8 @@ function CartProvider({
1397
1563
  metafieldIdentifiers,
1398
1564
  memberSetting,
1399
1565
  appContext,
1400
- autoRemoveFreeGiftsOptions
1566
+ autoRemoveFreeGiftsOptions,
1567
+ isAutoRemovingFreeGifts
1401
1568
  }),
1402
1569
  [
1403
1570
  cart,
@@ -1426,10 +1593,20 @@ function CartProvider({
1426
1593
  profile,
1427
1594
  memberSetting,
1428
1595
  appContext,
1429
- autoRemoveFreeGiftsOptions
1596
+ autoRemoveFreeGiftsOptions,
1597
+ isAutoRemovingFreeGifts
1430
1598
  ]
1431
1599
  );
1432
- return /* @__PURE__ */ jsxRuntime.jsx(CartContext.Provider, { value, children });
1600
+ return /* @__PURE__ */ jsxRuntime.jsxs(CartContext.Provider, { value, children: [
1601
+ (functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsxRuntime.jsx(
1602
+ AutoRemoveGiftsHandler,
1603
+ {
1604
+ options: autoRemoveFreeGiftsOptions,
1605
+ onRemovingChange: setIsAutoRemovingFreeGifts
1606
+ }
1607
+ ),
1608
+ children
1609
+ ] });
1433
1610
  }
1434
1611
  function useCartContext(options) {
1435
1612
  const context = react.useContext(CartContext);