@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.
- package/dist/hooks/index.d.mts +93 -56
- package/dist/hooks/index.d.ts +93 -56
- package/dist/hooks/index.js +139 -139
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +140 -140
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +25 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +26 -4
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +30 -14
- package/dist/provider/index.d.ts +30 -14
- package/dist/provider/index.js +138 -6
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +139 -7
- package/dist/provider/index.mjs.map +1 -1
- package/dist/{types-CMMWxyUF.d.mts → types-CM5QrlnE.d.mts} +185 -15
- package/dist/{types-CMMWxyUF.d.ts → types-CM5QrlnE.d.ts} +185 -15
- package/package.json +3 -3
package/dist/provider/index.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
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
|
|
8
|
+
import useSWRMutation5 from 'swr/mutation';
|
|
9
9
|
import { useRequest } from 'ahooks';
|
|
10
10
|
|
|
11
11
|
// src/provider/context.ts
|
|
@@ -750,6 +750,116 @@ 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 useSWRMutation5("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
|
+
} = options;
|
|
800
|
+
const [isRemoving, setIsRemoving] = useState(false);
|
|
801
|
+
const { cart } = useCartContext();
|
|
802
|
+
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
803
|
+
const giftsToRemove = useMemo(() => {
|
|
804
|
+
if (!cart?.lineItems) {
|
|
805
|
+
return [];
|
|
806
|
+
}
|
|
807
|
+
return cart.lineItems.filter((item) => {
|
|
808
|
+
if (removeFunctionGifts) {
|
|
809
|
+
const functionAttr = item.customAttributes?.find(
|
|
810
|
+
(attr) => attr.key === "_discounts_function_env"
|
|
811
|
+
)?.value;
|
|
812
|
+
if (functionAttr) {
|
|
813
|
+
try {
|
|
814
|
+
const functionAttrObj = JSON.parse(functionAttr);
|
|
815
|
+
if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
|
|
816
|
+
return true;
|
|
817
|
+
}
|
|
818
|
+
} catch (error) {
|
|
819
|
+
console.error("Failed to parse _discounts_function_env:", error);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
if (removeScriptGifts) {
|
|
824
|
+
const scriptGiftAttr = item.customAttributes?.find(
|
|
825
|
+
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
826
|
+
);
|
|
827
|
+
if (scriptGiftAttr) {
|
|
828
|
+
return true;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
if (isGiftLineItem && isGiftLineItem(item)) {
|
|
832
|
+
return true;
|
|
833
|
+
}
|
|
834
|
+
return false;
|
|
835
|
+
});
|
|
836
|
+
}, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
|
|
837
|
+
useEffect(() => {
|
|
838
|
+
if (isRemoving || giftsToRemove.length === 0) {
|
|
839
|
+
return;
|
|
840
|
+
}
|
|
841
|
+
const performRemoval = async () => {
|
|
842
|
+
setIsRemoving(true);
|
|
843
|
+
try {
|
|
844
|
+
await removeCartLines2({
|
|
845
|
+
lineIds: giftsToRemove.map((item) => item.id)
|
|
846
|
+
});
|
|
847
|
+
} catch (error) {
|
|
848
|
+
console.error("Failed to remove free gifts:", error);
|
|
849
|
+
} finally {
|
|
850
|
+
setIsRemoving(false);
|
|
851
|
+
}
|
|
852
|
+
};
|
|
853
|
+
performRemoval();
|
|
854
|
+
}, [
|
|
855
|
+
isRemoving,
|
|
856
|
+
giftsToRemove,
|
|
857
|
+
removeCartLines2
|
|
858
|
+
]);
|
|
859
|
+
return {
|
|
860
|
+
isRemoving
|
|
861
|
+
};
|
|
862
|
+
}
|
|
753
863
|
var getReferralAttributes = () => {
|
|
754
864
|
const inviteCode = getLocalStorage("inviteCode") || Cookies5.get("inviteCode");
|
|
755
865
|
const playModeId = getLocalStorage("playModeId") || Cookies5.get("playModeId");
|
|
@@ -1166,9 +1276,19 @@ function useUpdateCartAttributes({
|
|
|
1166
1276
|
},
|
|
1167
1277
|
[client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
|
|
1168
1278
|
);
|
|
1169
|
-
return
|
|
1279
|
+
return useSWRMutation5("update-cart-attributes", updateAttributes, swrOptions);
|
|
1170
1280
|
}
|
|
1171
1281
|
var CartContext = createContext(null);
|
|
1282
|
+
function AutoRemoveGiftsHandler({
|
|
1283
|
+
options,
|
|
1284
|
+
onRemovingChange
|
|
1285
|
+
}) {
|
|
1286
|
+
const { isRemoving } = useAutoRemoveFreeGifts(options);
|
|
1287
|
+
useEffect(() => {
|
|
1288
|
+
onRemovingChange(isRemoving);
|
|
1289
|
+
}, [isRemoving, onRemovingChange]);
|
|
1290
|
+
return null;
|
|
1291
|
+
}
|
|
1172
1292
|
function CartProvider({
|
|
1173
1293
|
children,
|
|
1174
1294
|
// swrOptions,
|
|
@@ -1196,6 +1316,7 @@ function CartProvider({
|
|
|
1196
1316
|
});
|
|
1197
1317
|
const [scriptAutoFreeGift, setScriptAutoFreeGift] = useState([]);
|
|
1198
1318
|
const [functionAutoFreeGift, setFunctionAutoFreeGift] = useState([]);
|
|
1319
|
+
const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = useState(false);
|
|
1199
1320
|
const {
|
|
1200
1321
|
run: fetchCart,
|
|
1201
1322
|
data: cart,
|
|
@@ -1388,7 +1509,8 @@ function CartProvider({
|
|
|
1388
1509
|
metafieldIdentifiers,
|
|
1389
1510
|
memberSetting,
|
|
1390
1511
|
appContext,
|
|
1391
|
-
autoRemoveFreeGiftsOptions
|
|
1512
|
+
autoRemoveFreeGiftsOptions,
|
|
1513
|
+
isAutoRemovingFreeGifts
|
|
1392
1514
|
}),
|
|
1393
1515
|
[
|
|
1394
1516
|
cart,
|
|
@@ -1417,10 +1539,20 @@ function CartProvider({
|
|
|
1417
1539
|
profile,
|
|
1418
1540
|
memberSetting,
|
|
1419
1541
|
appContext,
|
|
1420
|
-
autoRemoveFreeGiftsOptions
|
|
1542
|
+
autoRemoveFreeGiftsOptions,
|
|
1543
|
+
isAutoRemovingFreeGifts
|
|
1421
1544
|
]
|
|
1422
1545
|
);
|
|
1423
|
-
return /* @__PURE__ */
|
|
1546
|
+
return /* @__PURE__ */ jsxs(CartContext.Provider, { value, children: [
|
|
1547
|
+
(functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsx(
|
|
1548
|
+
AutoRemoveGiftsHandler,
|
|
1549
|
+
{
|
|
1550
|
+
options: autoRemoveFreeGiftsOptions,
|
|
1551
|
+
onRemovingChange: setIsAutoRemovingFreeGifts
|
|
1552
|
+
}
|
|
1553
|
+
),
|
|
1554
|
+
children
|
|
1555
|
+
] });
|
|
1424
1556
|
}
|
|
1425
1557
|
function useCartContext(options) {
|
|
1426
1558
|
const context = useContext(CartContext);
|