@anker-in/shopify-react 1.2.4-beta.0 → 1.2.5

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");
@@ -1175,9 +1317,19 @@ function useUpdateCartAttributes({
1175
1317
  },
1176
1318
  [client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
1177
1319
  );
1178
- return useSWRMutation8__default.default("update-cart-attributes", updateAttributes, swrOptions);
1320
+ return useSWRMutation6__default.default("update-cart-attributes", updateAttributes, swrOptions);
1179
1321
  }
1180
1322
  var CartContext = react.createContext(null);
1323
+ function AutoRemoveGiftsHandler({
1324
+ options,
1325
+ onRemovingChange
1326
+ }) {
1327
+ const { isRemoving } = useAutoRemoveFreeGifts(options);
1328
+ react.useEffect(() => {
1329
+ onRemovingChange(isRemoving);
1330
+ }, [isRemoving, onRemovingChange]);
1331
+ return null;
1332
+ }
1181
1333
  function CartProvider({
1182
1334
  children,
1183
1335
  // swrOptions,
@@ -1205,6 +1357,7 @@ function CartProvider({
1205
1357
  });
1206
1358
  const [scriptAutoFreeGift, setScriptAutoFreeGift] = react.useState([]);
1207
1359
  const [functionAutoFreeGift, setFunctionAutoFreeGift] = react.useState([]);
1360
+ const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = react.useState(false);
1208
1361
  const {
1209
1362
  run: fetchCart,
1210
1363
  data: cart,
@@ -1364,7 +1517,9 @@ function CartProvider({
1364
1517
  const autoRemoveFreeGiftsOptions = react.useMemo(() => {
1365
1518
  return {
1366
1519
  removeFunctionGifts: !!functionAutoFreeGiftConfig,
1367
- removeScriptGifts: !!scriptAutoFreeGiftConfig
1520
+ removeScriptGifts: !!scriptAutoFreeGiftConfig,
1521
+ runOnlyOnceAfterInit: true,
1522
+ initDelay: 500
1368
1523
  };
1369
1524
  }, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
1370
1525
  const value = react.useMemo(
@@ -1397,7 +1552,8 @@ function CartProvider({
1397
1552
  metafieldIdentifiers,
1398
1553
  memberSetting,
1399
1554
  appContext,
1400
- autoRemoveFreeGiftsOptions
1555
+ autoRemoveFreeGiftsOptions,
1556
+ isAutoRemovingFreeGifts
1401
1557
  }),
1402
1558
  [
1403
1559
  cart,
@@ -1426,10 +1582,20 @@ function CartProvider({
1426
1582
  profile,
1427
1583
  memberSetting,
1428
1584
  appContext,
1429
- autoRemoveFreeGiftsOptions
1585
+ autoRemoveFreeGiftsOptions,
1586
+ isAutoRemovingFreeGifts
1430
1587
  ]
1431
1588
  );
1432
- return /* @__PURE__ */ jsxRuntime.jsx(CartContext.Provider, { value, children });
1589
+ return /* @__PURE__ */ jsxRuntime.jsxs(CartContext.Provider, { value, children: [
1590
+ (functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsxRuntime.jsx(
1591
+ AutoRemoveGiftsHandler,
1592
+ {
1593
+ options: autoRemoveFreeGiftsOptions,
1594
+ onRemovingChange: setIsAutoRemovingFreeGifts
1595
+ }
1596
+ ),
1597
+ children
1598
+ ] });
1433
1599
  }
1434
1600
  function useCartContext(options) {
1435
1601
  const context = react.useContext(CartContext);