@anker-in/shopify-react 1.2.9-beta.1 → 1.2.9-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 +6 -23
- package/dist/hooks/index.d.ts +6 -23
- package/dist/hooks/index.js +193 -142
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +195 -144
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +83 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +85 -10
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +3 -1
- package/dist/provider/index.d.ts +3 -1
- package/dist/provider/index.js +185 -8
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +187 -10
- package/dist/provider/index.mjs.map +1 -1
- package/dist/{types-DfR13pDe.d.mts → types-C0UyuPrG.d.mts} +12 -5
- package/dist/{types-DfR13pDe.d.ts → types-C0UyuPrG.d.ts} +12 -5
- package/package.json +17 -17
package/dist/hooks/index.mjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { createContext, useMemo, useRef, useState, useEffect, useCallback, useContext } from 'react';
|
|
2
2
|
import useSWRMutation from 'swr/mutation';
|
|
3
|
-
import { getProductsByHandles,
|
|
3
|
+
import { getProductsByHandles, removeCartLines, updateCartCodes, createCart, addCartLines, updateCartLines, getLocalStorage, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage } from '@anker-in/shopify-sdk';
|
|
4
4
|
import Cookies5 from 'js-cookie';
|
|
5
5
|
import { jsx } from 'react/jsx-runtime';
|
|
6
6
|
import Decimal3 from 'decimal.js';
|
|
7
7
|
import { atobID, btoaID } from '@anker-in/shopify-core';
|
|
8
8
|
import useSWR from 'swr';
|
|
9
|
-
import { useRequest } from 'ahooks';
|
|
9
|
+
import { useDebounceEffect, useRequest } from 'ahooks';
|
|
10
10
|
|
|
11
11
|
// src/hooks/cart/use-create-cart.ts
|
|
12
12
|
var ShopifyContext = createContext(null);
|
|
@@ -662,6 +662,177 @@ var useScriptAutoFreeGift = ({
|
|
|
662
662
|
giftProductsResult: finalGiftProductsResult
|
|
663
663
|
};
|
|
664
664
|
};
|
|
665
|
+
function useRemoveCartLines(options) {
|
|
666
|
+
const { client, locale, cartCookieAdapter } = useShopify();
|
|
667
|
+
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
668
|
+
const removeLines = useCallback(
|
|
669
|
+
async (_key, { arg }) => {
|
|
670
|
+
const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
|
|
671
|
+
let updatedCart = await removeCartLines(client, {
|
|
672
|
+
cartId,
|
|
673
|
+
lineIds,
|
|
674
|
+
metafieldIdentifiers,
|
|
675
|
+
cookieAdapter: cartCookieAdapter
|
|
676
|
+
});
|
|
677
|
+
if (updatedCart && autoRemoveInvalidCodes) {
|
|
678
|
+
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
679
|
+
if (unApplicableCodes.length > 0) {
|
|
680
|
+
if (onCodesRemoved) {
|
|
681
|
+
const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
|
|
682
|
+
if (handledCart) {
|
|
683
|
+
updatedCart = handledCart;
|
|
684
|
+
}
|
|
685
|
+
} else {
|
|
686
|
+
updatedCart = await updateCartCodes(client, {
|
|
687
|
+
cartId: updatedCart.id,
|
|
688
|
+
discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
|
|
689
|
+
metafieldIdentifiers,
|
|
690
|
+
cookieAdapter: cartCookieAdapter
|
|
691
|
+
}) || updatedCart;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
if (updatedCart) {
|
|
696
|
+
mutateCart(updatedCart);
|
|
697
|
+
}
|
|
698
|
+
return updatedCart;
|
|
699
|
+
},
|
|
700
|
+
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
701
|
+
);
|
|
702
|
+
return useSWRMutation("remove-cart-lines", removeLines, options);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// src/hooks/cart/feature/use-auto-remove-free-gifts.ts
|
|
706
|
+
function useAutoRemoveFreeGifts(options = {}) {
|
|
707
|
+
const {
|
|
708
|
+
removeFunctionGifts = true,
|
|
709
|
+
removeScriptGifts = true,
|
|
710
|
+
isGiftLineItem,
|
|
711
|
+
runOnlyOnceAfterInit = false,
|
|
712
|
+
initDelay = 500
|
|
713
|
+
} = options;
|
|
714
|
+
const [isRemoving, setIsRemoving] = useState(false);
|
|
715
|
+
const [isInitialized, setIsInitialized] = useState(!runOnlyOnceAfterInit);
|
|
716
|
+
const [isFinished, setIsFinished] = useState(false);
|
|
717
|
+
const { cart } = useCartContext();
|
|
718
|
+
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
719
|
+
const giftsToRemove = useMemo(() => {
|
|
720
|
+
if (!cart?.lineItems) {
|
|
721
|
+
return [];
|
|
722
|
+
}
|
|
723
|
+
return cart.lineItems.filter((item) => {
|
|
724
|
+
if (removeFunctionGifts) {
|
|
725
|
+
const functionAttr = item.customAttributes?.find(
|
|
726
|
+
(attr) => attr.key === "_discounts_function_env"
|
|
727
|
+
)?.value;
|
|
728
|
+
if (functionAttr) {
|
|
729
|
+
try {
|
|
730
|
+
const functionAttrObj = JSON.parse(functionAttr);
|
|
731
|
+
if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
|
|
732
|
+
return true;
|
|
733
|
+
}
|
|
734
|
+
} catch (error) {
|
|
735
|
+
console.error("Failed to parse _discounts_function_env:", error);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
if (removeScriptGifts) {
|
|
740
|
+
const scriptGiftAttr = item.customAttributes?.find(
|
|
741
|
+
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
742
|
+
);
|
|
743
|
+
if (scriptGiftAttr) {
|
|
744
|
+
return true;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
if (isGiftLineItem && isGiftLineItem(item)) {
|
|
748
|
+
return true;
|
|
749
|
+
}
|
|
750
|
+
return false;
|
|
751
|
+
});
|
|
752
|
+
}, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
|
|
753
|
+
useDebounceEffect(
|
|
754
|
+
() => {
|
|
755
|
+
if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
if (!cart?.lineItems?.length) {
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
setIsInitialized(true);
|
|
762
|
+
if (giftsToRemove.length === 0) {
|
|
763
|
+
setIsFinished(true);
|
|
764
|
+
}
|
|
765
|
+
},
|
|
766
|
+
[runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
|
|
767
|
+
{
|
|
768
|
+
trailing: true,
|
|
769
|
+
wait: initDelay
|
|
770
|
+
}
|
|
771
|
+
);
|
|
772
|
+
useEffect(() => {
|
|
773
|
+
if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
if (isRemoving || giftsToRemove.length === 0) {
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
const performRemoval = async () => {
|
|
780
|
+
setIsRemoving(true);
|
|
781
|
+
try {
|
|
782
|
+
await removeCartLines2({
|
|
783
|
+
lineIds: giftsToRemove.map((item) => item.id)
|
|
784
|
+
});
|
|
785
|
+
} catch (error) {
|
|
786
|
+
console.error("Failed to remove free gifts:", error);
|
|
787
|
+
} finally {
|
|
788
|
+
setIsRemoving(false);
|
|
789
|
+
if (runOnlyOnceAfterInit) {
|
|
790
|
+
setIsFinished(true);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
performRemoval();
|
|
795
|
+
}, [
|
|
796
|
+
runOnlyOnceAfterInit,
|
|
797
|
+
isInitialized,
|
|
798
|
+
isFinished,
|
|
799
|
+
isRemoving,
|
|
800
|
+
giftsToRemove,
|
|
801
|
+
removeCartLines2
|
|
802
|
+
]);
|
|
803
|
+
return {
|
|
804
|
+
isRemoving
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
function isFunctionGift(line) {
|
|
808
|
+
const functionAttr = line.customAttributes?.find(
|
|
809
|
+
(attr) => attr.key === "_discounts_function_env"
|
|
810
|
+
)?.value;
|
|
811
|
+
if (!functionAttr) {
|
|
812
|
+
return false;
|
|
813
|
+
}
|
|
814
|
+
try {
|
|
815
|
+
const functionAttrObj = JSON.parse(functionAttr);
|
|
816
|
+
return Boolean(
|
|
817
|
+
functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money
|
|
818
|
+
);
|
|
819
|
+
} catch {
|
|
820
|
+
return false;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
function isScriptGift(line) {
|
|
824
|
+
return line.customAttributes?.some(
|
|
825
|
+
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
826
|
+
) ?? false;
|
|
827
|
+
}
|
|
828
|
+
function isBuyGetGift(line) {
|
|
829
|
+
return line.customAttributes?.some(
|
|
830
|
+
(attr) => attr.key === "_freegift_related_handlesku"
|
|
831
|
+
) ?? false;
|
|
832
|
+
}
|
|
833
|
+
function isAnyGift(line) {
|
|
834
|
+
return isFunctionGift(line) || isScriptGift(line) || isBuyGetGift(line);
|
|
835
|
+
}
|
|
665
836
|
var CartContext = createContext(null);
|
|
666
837
|
function useCartContext(options) {
|
|
667
838
|
const context = useContext(CartContext);
|
|
@@ -1092,145 +1263,6 @@ var getLinesWithAttributes = ({
|
|
|
1092
1263
|
return functionLine;
|
|
1093
1264
|
});
|
|
1094
1265
|
};
|
|
1095
|
-
function useRemoveCartLines(options) {
|
|
1096
|
-
const { client, locale, cartCookieAdapter } = useShopify();
|
|
1097
|
-
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
1098
|
-
const removeLines = useCallback(
|
|
1099
|
-
async (_key, { arg }) => {
|
|
1100
|
-
const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
|
|
1101
|
-
let updatedCart = await removeCartLines(client, {
|
|
1102
|
-
cartId,
|
|
1103
|
-
lineIds,
|
|
1104
|
-
metafieldIdentifiers,
|
|
1105
|
-
cookieAdapter: cartCookieAdapter
|
|
1106
|
-
});
|
|
1107
|
-
if (updatedCart && autoRemoveInvalidCodes) {
|
|
1108
|
-
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
1109
|
-
if (unApplicableCodes.length > 0) {
|
|
1110
|
-
if (onCodesRemoved) {
|
|
1111
|
-
const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
|
|
1112
|
-
if (handledCart) {
|
|
1113
|
-
updatedCart = handledCart;
|
|
1114
|
-
}
|
|
1115
|
-
} else {
|
|
1116
|
-
updatedCart = await updateCartCodes(client, {
|
|
1117
|
-
cartId: updatedCart.id,
|
|
1118
|
-
discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
|
|
1119
|
-
metafieldIdentifiers,
|
|
1120
|
-
cookieAdapter: cartCookieAdapter
|
|
1121
|
-
}) || updatedCart;
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
if (updatedCart) {
|
|
1126
|
-
mutateCart(updatedCart);
|
|
1127
|
-
}
|
|
1128
|
-
return updatedCart;
|
|
1129
|
-
},
|
|
1130
|
-
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
1131
|
-
);
|
|
1132
|
-
return useSWRMutation("remove-cart-lines", removeLines, options);
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
// src/hooks/cart/feature/use-auto-remove-free-gifts.ts
|
|
1136
|
-
function useAutoRemoveFreeGifts(options = {}) {
|
|
1137
|
-
const {
|
|
1138
|
-
removeFunctionGifts = true,
|
|
1139
|
-
removeScriptGifts = true,
|
|
1140
|
-
isGiftLineItem
|
|
1141
|
-
} = options;
|
|
1142
|
-
const [isRemoving, setIsRemoving] = useState(false);
|
|
1143
|
-
const { cart } = useCartContext();
|
|
1144
|
-
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
1145
|
-
const giftsToRemove = useMemo(() => {
|
|
1146
|
-
if (!cart?.lineItems) {
|
|
1147
|
-
return [];
|
|
1148
|
-
}
|
|
1149
|
-
return cart.lineItems.filter((item) => {
|
|
1150
|
-
if (removeFunctionGifts) {
|
|
1151
|
-
const functionAttr = item.customAttributes?.find(
|
|
1152
|
-
(attr) => attr.key === "_discounts_function_env"
|
|
1153
|
-
)?.value;
|
|
1154
|
-
if (functionAttr) {
|
|
1155
|
-
try {
|
|
1156
|
-
const functionAttrObj = JSON.parse(functionAttr);
|
|
1157
|
-
if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
|
|
1158
|
-
return true;
|
|
1159
|
-
}
|
|
1160
|
-
} catch (error) {
|
|
1161
|
-
console.error("Failed to parse _discounts_function_env:", error);
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
}
|
|
1165
|
-
if (removeScriptGifts) {
|
|
1166
|
-
const scriptGiftAttr = item.customAttributes?.find(
|
|
1167
|
-
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
1168
|
-
);
|
|
1169
|
-
if (scriptGiftAttr) {
|
|
1170
|
-
return true;
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
if (isGiftLineItem && isGiftLineItem(item)) {
|
|
1174
|
-
return true;
|
|
1175
|
-
}
|
|
1176
|
-
return false;
|
|
1177
|
-
});
|
|
1178
|
-
}, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
|
|
1179
|
-
useEffect(() => {
|
|
1180
|
-
if (isRemoving || giftsToRemove.length === 0) {
|
|
1181
|
-
return;
|
|
1182
|
-
}
|
|
1183
|
-
const performRemoval = async () => {
|
|
1184
|
-
setIsRemoving(true);
|
|
1185
|
-
try {
|
|
1186
|
-
await removeCartLines2({
|
|
1187
|
-
lineIds: giftsToRemove.map((item) => item.id)
|
|
1188
|
-
});
|
|
1189
|
-
} catch (error) {
|
|
1190
|
-
console.error("Failed to remove free gifts:", error);
|
|
1191
|
-
} finally {
|
|
1192
|
-
setIsRemoving(false);
|
|
1193
|
-
}
|
|
1194
|
-
};
|
|
1195
|
-
performRemoval();
|
|
1196
|
-
}, [
|
|
1197
|
-
isRemoving,
|
|
1198
|
-
giftsToRemove,
|
|
1199
|
-
removeCartLines2
|
|
1200
|
-
]);
|
|
1201
|
-
return {
|
|
1202
|
-
isRemoving
|
|
1203
|
-
};
|
|
1204
|
-
}
|
|
1205
|
-
function isFunctionGift(line) {
|
|
1206
|
-
const functionAttr = line.customAttributes?.find(
|
|
1207
|
-
(attr) => attr.key === "_discounts_function_env"
|
|
1208
|
-
)?.value;
|
|
1209
|
-
if (!functionAttr) {
|
|
1210
|
-
return false;
|
|
1211
|
-
}
|
|
1212
|
-
try {
|
|
1213
|
-
const functionAttrObj = JSON.parse(functionAttr);
|
|
1214
|
-
return Boolean(
|
|
1215
|
-
functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money
|
|
1216
|
-
);
|
|
1217
|
-
} catch {
|
|
1218
|
-
return false;
|
|
1219
|
-
}
|
|
1220
|
-
}
|
|
1221
|
-
function isScriptGift(line) {
|
|
1222
|
-
return line.customAttributes?.some(
|
|
1223
|
-
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
1224
|
-
) ?? false;
|
|
1225
|
-
}
|
|
1226
|
-
function isBuyGetGift(line) {
|
|
1227
|
-
return line.customAttributes?.some(
|
|
1228
|
-
(attr) => attr.key === "_freegift_related_handlesku"
|
|
1229
|
-
) ?? false;
|
|
1230
|
-
}
|
|
1231
|
-
function isAnyGift(line) {
|
|
1232
|
-
return isFunctionGift(line) || isScriptGift(line) || isBuyGetGift(line);
|
|
1233
|
-
}
|
|
1234
1266
|
function useCalcGiftsFromLines({
|
|
1235
1267
|
lines,
|
|
1236
1268
|
customer,
|
|
@@ -1527,7 +1559,17 @@ function getCartBasicAttributes({
|
|
|
1527
1559
|
const presellAttributes = [
|
|
1528
1560
|
{
|
|
1529
1561
|
key: "_presale",
|
|
1530
|
-
value: cart?.lineItems?.some(
|
|
1562
|
+
value: cart?.lineItems?.some(
|
|
1563
|
+
(item) => item?.variant?.metafields?.global?.presell === "presell"
|
|
1564
|
+
) ? "true" : "false"
|
|
1565
|
+
}
|
|
1566
|
+
];
|
|
1567
|
+
const hideShippingAttributes = [
|
|
1568
|
+
{
|
|
1569
|
+
key: "_hide_shipping",
|
|
1570
|
+
value: cart?.lineItems?.some(
|
|
1571
|
+
(item) => item?.variant?.metafields?.global?.hideShipping === "true"
|
|
1572
|
+
) ? "true" : "false"
|
|
1531
1573
|
}
|
|
1532
1574
|
];
|
|
1533
1575
|
const weightAttributes = [
|
|
@@ -1566,6 +1608,7 @@ function getCartBasicAttributes({
|
|
|
1566
1608
|
...functionAttributes,
|
|
1567
1609
|
...presellAttributes,
|
|
1568
1610
|
...weightAttributes,
|
|
1611
|
+
...hideShippingAttributes,
|
|
1569
1612
|
...trackingAttributes,
|
|
1570
1613
|
...getReferralAttributes()
|
|
1571
1614
|
].filter((item) => item?.value !== void 0 && item?.value !== null);
|
|
@@ -2562,7 +2605,13 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
2562
2605
|
cookieAdapter: cartCookieAdapter,
|
|
2563
2606
|
buyerIdentity,
|
|
2564
2607
|
discountCodes,
|
|
2565
|
-
customAttributes: [
|
|
2608
|
+
customAttributes: [
|
|
2609
|
+
...basicCartAttributes.filter(
|
|
2610
|
+
(a) => !(customAttributes || []).some((c) => c.key === a.key)
|
|
2611
|
+
),
|
|
2612
|
+
...ga4Attributes.filter((a) => !(customAttributes || []).some((c) => c.key === a.key)),
|
|
2613
|
+
...customAttributes || []
|
|
2614
|
+
]
|
|
2566
2615
|
});
|
|
2567
2616
|
if (!resultCart) {
|
|
2568
2617
|
throw new Error("Failed to create cart for buy now");
|
|
@@ -2570,7 +2619,9 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
2570
2619
|
if (withTrack && resultCart.lineItems) {
|
|
2571
2620
|
trackBuyNowGA({
|
|
2572
2621
|
lineItems: lineItems.map((item) => {
|
|
2573
|
-
const cartLine = resultCart.lineItems.find(
|
|
2622
|
+
const cartLine = resultCart.lineItems.find(
|
|
2623
|
+
(line) => line.variant.id === item.variant?.id
|
|
2624
|
+
);
|
|
2574
2625
|
if (!cartLine) {
|
|
2575
2626
|
return null;
|
|
2576
2627
|
}
|