@anker-in/shopify-react 1.3.0-beta.0 → 1.3.0-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.
@@ -4,7 +4,8 @@ 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 { e as PlusMemberSettingsMetafields } from '../types-CMMWxyUF.mjs';
7
+ import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-CM5QrlnE.mjs';
8
+ import 'decimal.js';
8
9
 
9
10
  interface ShopifyContextValue {
10
11
  client: ShopifyClient;
@@ -48,6 +49,12 @@ type LoadingState = {
48
49
  listingAutoCodeApplying: boolean;
49
50
  userCodeApplying: boolean;
50
51
  };
52
+ type AppContext = {
53
+ /** Whether running in app */
54
+ isInApp?: boolean;
55
+ /** App name */
56
+ appName?: string;
57
+ };
51
58
  interface CartContextValue {
52
59
  /** Current cart data */
53
60
  cart: NormalizedCart | undefined;
@@ -91,10 +98,10 @@ interface CartContextValue {
91
98
  calculatedAutoFreeGift?: any;
92
99
  /** Gifts that need to be added to cart */
93
100
  giftNeedAddToCartLines: any[];
94
- /** Auto free gift config */
95
- autoFreeGiftConfig?: any;
96
- /** Gradient gifts config */
97
- gradientGiftsConfig?: any;
101
+ /** Function auto free gift config */
102
+ functionAutoFreeGiftConfig?: any;
103
+ /** Script auto free gift config */
104
+ scriptAutoFreeGiftConfig?: ScriptAutoFreeGiftConfig;
98
105
  /** Metafield identifiers */
99
106
  metafieldIdentifiers?: {
100
107
  variant: HasMetafieldsIdentifier[];
@@ -106,17 +113,21 @@ interface CartContextValue {
106
113
  customer?: any;
107
114
  /** Plus member settings */
108
115
  memberSetting?: PlusMemberSettingsMetafields;
116
+ /** App context */
117
+ appContext?: AppContext;
118
+ /** Auto remove free gifts options */
119
+ autoRemoveFreeGiftsOptions?: UseAutoRemoveFreeGiftsOptions;
120
+ /** Whether auto removing free gifts is in progress */
121
+ isAutoRemovingFreeGifts: boolean;
109
122
  }
110
123
  interface CartProviderProps {
111
124
  children: React__default.ReactNode;
112
125
  /** SWR configuration options */
113
126
  swrOptions?: SWRConfiguration<NormalizedCart | undefined, Error>;
114
- /** Auto free gift configuration */
115
- autoFreeGiftConfig?: any;
116
- /** Gradient gifts configuration (for script gifts) */
117
- gradientGiftsConfig?: any;
118
- /** Function gift configuration */
119
- fnGiftConf?: any;
127
+ /** Function auto free gift configuration */
128
+ functionAutoFreeGiftConfig?: any;
129
+ /** Script auto free gift configuration */
130
+ scriptAutoFreeGiftConfig?: ScriptAutoFreeGiftConfig;
120
131
  /** User profile */
121
132
  profile?: any;
122
133
  /** Customer data */
@@ -130,6 +141,10 @@ interface CartProviderProps {
130
141
  };
131
142
  /** Plus member settings */
132
143
  memberSetting?: PlusMemberSettingsMetafields;
144
+ /** App context */
145
+ appContext?: AppContext;
146
+ /** 是否禁用自动更新 line item 的 code amount attributes,默认为 false */
147
+ disableUpdateLineCodeAmountAttributes?: boolean;
133
148
  }
134
149
  /**
135
150
  * Cart Provider Component
@@ -141,7 +156,8 @@ interface CartProviderProps {
141
156
  * <ShopifyProvider {...config}>
142
157
  * <CartProvider
143
158
  * locale="us"
144
- * autoFreeGiftConfig={config}
159
+ * functionAutoFreeGiftConfig={functionConfig}
160
+ * scriptAutoFreeGiftConfig={scriptConfig}
145
161
  * profile={profile}
146
162
  * customer={customer}
147
163
  * >
@@ -150,7 +166,7 @@ interface CartProviderProps {
150
166
  * </ShopifyProvider>
151
167
  * ```
152
168
  */
153
- declare function CartProvider({ children, autoFreeGiftConfig, gradientGiftsConfig, profile, customer, locale, metafieldIdentifiers, memberSetting, }: CartProviderProps): react_jsx_runtime.JSX.Element;
169
+ declare function CartProvider({ children, functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig, profile, customer, locale, metafieldIdentifiers, memberSetting, appContext, disableUpdateLineCodeAmountAttributes, }: CartProviderProps): react_jsx_runtime.JSX.Element;
154
170
  /**
155
171
  * Hook to access cart context
156
172
  *
@@ -175,4 +191,4 @@ declare function useCartContext(options: {
175
191
  optional: true;
176
192
  }): CartContextValue | null;
177
193
 
178
- export { type CartContextValue, CartProvider, type CartProviderProps, type LoadingState, ShopifyContext, type ShopifyContextValue, ShopifyProvider, type ShopifyProviderProps, useCartContext, useShopify };
194
+ export { type AppContext, type CartContextValue, CartProvider, type CartProviderProps, type LoadingState, ShopifyContext, type ShopifyContextValue, ShopifyProvider, type ShopifyProviderProps, useCartContext, useShopify };
@@ -4,7 +4,8 @@ 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 { e as PlusMemberSettingsMetafields } from '../types-CMMWxyUF.js';
7
+ import { S as ScriptAutoFreeGiftConfig, n as PlusMemberSettingsMetafields, U as UseAutoRemoveFreeGiftsOptions } from '../types-CM5QrlnE.js';
8
+ import 'decimal.js';
8
9
 
9
10
  interface ShopifyContextValue {
10
11
  client: ShopifyClient;
@@ -48,6 +49,12 @@ type LoadingState = {
48
49
  listingAutoCodeApplying: boolean;
49
50
  userCodeApplying: boolean;
50
51
  };
52
+ type AppContext = {
53
+ /** Whether running in app */
54
+ isInApp?: boolean;
55
+ /** App name */
56
+ appName?: string;
57
+ };
51
58
  interface CartContextValue {
52
59
  /** Current cart data */
53
60
  cart: NormalizedCart | undefined;
@@ -91,10 +98,10 @@ interface CartContextValue {
91
98
  calculatedAutoFreeGift?: any;
92
99
  /** Gifts that need to be added to cart */
93
100
  giftNeedAddToCartLines: any[];
94
- /** Auto free gift config */
95
- autoFreeGiftConfig?: any;
96
- /** Gradient gifts config */
97
- gradientGiftsConfig?: any;
101
+ /** Function auto free gift config */
102
+ functionAutoFreeGiftConfig?: any;
103
+ /** Script auto free gift config */
104
+ scriptAutoFreeGiftConfig?: ScriptAutoFreeGiftConfig;
98
105
  /** Metafield identifiers */
99
106
  metafieldIdentifiers?: {
100
107
  variant: HasMetafieldsIdentifier[];
@@ -106,17 +113,21 @@ interface CartContextValue {
106
113
  customer?: any;
107
114
  /** Plus member settings */
108
115
  memberSetting?: PlusMemberSettingsMetafields;
116
+ /** App context */
117
+ appContext?: AppContext;
118
+ /** Auto remove free gifts options */
119
+ autoRemoveFreeGiftsOptions?: UseAutoRemoveFreeGiftsOptions;
120
+ /** Whether auto removing free gifts is in progress */
121
+ isAutoRemovingFreeGifts: boolean;
109
122
  }
110
123
  interface CartProviderProps {
111
124
  children: React__default.ReactNode;
112
125
  /** SWR configuration options */
113
126
  swrOptions?: SWRConfiguration<NormalizedCart | undefined, Error>;
114
- /** Auto free gift configuration */
115
- autoFreeGiftConfig?: any;
116
- /** Gradient gifts configuration (for script gifts) */
117
- gradientGiftsConfig?: any;
118
- /** Function gift configuration */
119
- fnGiftConf?: any;
127
+ /** Function auto free gift configuration */
128
+ functionAutoFreeGiftConfig?: any;
129
+ /** Script auto free gift configuration */
130
+ scriptAutoFreeGiftConfig?: ScriptAutoFreeGiftConfig;
120
131
  /** User profile */
121
132
  profile?: any;
122
133
  /** Customer data */
@@ -130,6 +141,10 @@ interface CartProviderProps {
130
141
  };
131
142
  /** Plus member settings */
132
143
  memberSetting?: PlusMemberSettingsMetafields;
144
+ /** App context */
145
+ appContext?: AppContext;
146
+ /** 是否禁用自动更新 line item 的 code amount attributes,默认为 false */
147
+ disableUpdateLineCodeAmountAttributes?: boolean;
133
148
  }
134
149
  /**
135
150
  * Cart Provider Component
@@ -141,7 +156,8 @@ interface CartProviderProps {
141
156
  * <ShopifyProvider {...config}>
142
157
  * <CartProvider
143
158
  * locale="us"
144
- * autoFreeGiftConfig={config}
159
+ * functionAutoFreeGiftConfig={functionConfig}
160
+ * scriptAutoFreeGiftConfig={scriptConfig}
145
161
  * profile={profile}
146
162
  * customer={customer}
147
163
  * >
@@ -150,7 +166,7 @@ interface CartProviderProps {
150
166
  * </ShopifyProvider>
151
167
  * ```
152
168
  */
153
- declare function CartProvider({ children, autoFreeGiftConfig, gradientGiftsConfig, profile, customer, locale, metafieldIdentifiers, memberSetting, }: CartProviderProps): react_jsx_runtime.JSX.Element;
169
+ declare function CartProvider({ children, functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig, profile, customer, locale, metafieldIdentifiers, memberSetting, appContext, disableUpdateLineCodeAmountAttributes, }: CartProviderProps): react_jsx_runtime.JSX.Element;
154
170
  /**
155
171
  * Hook to access cart context
156
172
  *
@@ -175,4 +191,4 @@ declare function useCartContext(options: {
175
191
  optional: true;
176
192
  }): CartContextValue | null;
177
193
 
178
- export { type CartContextValue, CartProvider, type CartProviderProps, type LoadingState, ShopifyContext, type ShopifyContextValue, ShopifyProvider, type ShopifyProviderProps, useCartContext, useShopify };
194
+ export { type AppContext, type CartContextValue, CartProvider, type CartProviderProps, type LoadingState, ShopifyContext, type ShopifyContextValue, ShopifyProvider, type ShopifyProviderProps, useCartContext, useShopify };
@@ -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 useSWRMutation5 = 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 useSWRMutation5__default = /*#__PURE__*/_interopDefault(useSWRMutation5);
19
19
 
20
20
  // src/provider/context.ts
21
21
  var ShopifyContext = react.createContext(null);
@@ -759,6 +759,116 @@ 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 useSWRMutation5__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
+ } = options;
809
+ const [isRemoving, setIsRemoving] = react.useState(false);
810
+ const { cart } = useCartContext();
811
+ const { trigger: removeCartLines2 } = useRemoveCartLines();
812
+ const giftsToRemove = react.useMemo(() => {
813
+ if (!cart?.lineItems) {
814
+ return [];
815
+ }
816
+ return cart.lineItems.filter((item) => {
817
+ if (removeFunctionGifts) {
818
+ const functionAttr = item.customAttributes?.find(
819
+ (attr) => attr.key === "_discounts_function_env"
820
+ )?.value;
821
+ if (functionAttr) {
822
+ try {
823
+ const functionAttrObj = JSON.parse(functionAttr);
824
+ if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
825
+ return true;
826
+ }
827
+ } catch (error) {
828
+ console.error("Failed to parse _discounts_function_env:", error);
829
+ }
830
+ }
831
+ }
832
+ if (removeScriptGifts) {
833
+ const scriptGiftAttr = item.customAttributes?.find(
834
+ (attr) => attr.key === "_giveaway_gradient_gifts"
835
+ );
836
+ if (scriptGiftAttr) {
837
+ return true;
838
+ }
839
+ }
840
+ if (isGiftLineItem && isGiftLineItem(item)) {
841
+ return true;
842
+ }
843
+ return false;
844
+ });
845
+ }, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
846
+ react.useEffect(() => {
847
+ if (isRemoving || giftsToRemove.length === 0) {
848
+ return;
849
+ }
850
+ const performRemoval = async () => {
851
+ setIsRemoving(true);
852
+ try {
853
+ await removeCartLines2({
854
+ lineIds: giftsToRemove.map((item) => item.id)
855
+ });
856
+ } catch (error) {
857
+ console.error("Failed to remove free gifts:", error);
858
+ } finally {
859
+ setIsRemoving(false);
860
+ }
861
+ };
862
+ performRemoval();
863
+ }, [
864
+ isRemoving,
865
+ giftsToRemove,
866
+ removeCartLines2
867
+ ]);
868
+ return {
869
+ isRemoving
870
+ };
871
+ }
762
872
  var getReferralAttributes = () => {
763
873
  const inviteCode = shopifySdk.getLocalStorage("inviteCode") || Cookies5__default.default.get("inviteCode");
764
874
  const playModeId = shopifySdk.getLocalStorage("playModeId") || Cookies5__default.default.get("playModeId");
@@ -1175,9 +1285,19 @@ function useUpdateCartAttributes({
1175
1285
  },
1176
1286
  [client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
1177
1287
  );
1178
- return useSWRMutation8__default.default("update-cart-attributes", updateAttributes, swrOptions);
1288
+ return useSWRMutation5__default.default("update-cart-attributes", updateAttributes, swrOptions);
1179
1289
  }
1180
1290
  var CartContext = react.createContext(null);
1291
+ function AutoRemoveGiftsHandler({
1292
+ options,
1293
+ onRemovingChange
1294
+ }) {
1295
+ const { isRemoving } = useAutoRemoveFreeGifts(options);
1296
+ react.useEffect(() => {
1297
+ onRemovingChange(isRemoving);
1298
+ }, [isRemoving, onRemovingChange]);
1299
+ return null;
1300
+ }
1181
1301
  function CartProvider({
1182
1302
  children,
1183
1303
  // swrOptions,
@@ -1205,6 +1325,7 @@ function CartProvider({
1205
1325
  });
1206
1326
  const [scriptAutoFreeGift, setScriptAutoFreeGift] = react.useState([]);
1207
1327
  const [functionAutoFreeGift, setFunctionAutoFreeGift] = react.useState([]);
1328
+ const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = react.useState(false);
1208
1329
  const {
1209
1330
  run: fetchCart,
1210
1331
  data: cart,
@@ -1397,7 +1518,8 @@ function CartProvider({
1397
1518
  metafieldIdentifiers,
1398
1519
  memberSetting,
1399
1520
  appContext,
1400
- autoRemoveFreeGiftsOptions
1521
+ autoRemoveFreeGiftsOptions,
1522
+ isAutoRemovingFreeGifts
1401
1523
  }),
1402
1524
  [
1403
1525
  cart,
@@ -1426,10 +1548,20 @@ function CartProvider({
1426
1548
  profile,
1427
1549
  memberSetting,
1428
1550
  appContext,
1429
- autoRemoveFreeGiftsOptions
1551
+ autoRemoveFreeGiftsOptions,
1552
+ isAutoRemovingFreeGifts
1430
1553
  ]
1431
1554
  );
1432
- return /* @__PURE__ */ jsxRuntime.jsx(CartContext.Provider, { value, children });
1555
+ return /* @__PURE__ */ jsxRuntime.jsxs(CartContext.Provider, { value, children: [
1556
+ (functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsxRuntime.jsx(
1557
+ AutoRemoveGiftsHandler,
1558
+ {
1559
+ options: autoRemoveFreeGiftsOptions,
1560
+ onRemovingChange: setIsAutoRemovingFreeGifts
1561
+ }
1562
+ ),
1563
+ children
1564
+ ] });
1433
1565
  }
1434
1566
  function useCartContext(options) {
1435
1567
  const context = react.useContext(CartContext);