@anker-in/shopify-react 1.3.0-beta.1 → 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.
@@ -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;
@@ -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 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);