@anker-in/shopify-react 1.3.0-beta.2 → 1.3.0-beta.4
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 +2 -2
- package/dist/hooks/index.d.ts +2 -2
- package/dist/hooks/index.js +84 -24
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +86 -26
- 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 +87 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +89 -27
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +1 -1
- package/dist/provider/index.d.ts +1 -1
- package/dist/provider/index.js +40 -6
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +40 -6
- package/dist/provider/index.mjs.map +1 -1
- package/dist/{types-CM5QrlnE.d.mts → types-C0UyuPrG.d.mts} +10 -0
- package/dist/{types-CM5QrlnE.d.ts → types-C0UyuPrG.d.ts} +10 -0
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createContext, useMemo, useContext, useRef, useState, useEffect, useCallback } from 'react';
|
|
2
|
-
import { createShopifyClient, getProductsByHandles, createCart, updateCartCodes, addCartLines, removeCartLines, getLocalStorage,
|
|
2
|
+
import { createShopifyClient, getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, getLocalStorage, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, getCart, setLocalStorage } from '@anker-in/shopify-sdk';
|
|
3
3
|
export * from '@anker-in/shopify-sdk';
|
|
4
4
|
import Cookies5 from 'js-cookie';
|
|
5
5
|
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
@@ -7,7 +7,7 @@ import Decimal3 from 'decimal.js';
|
|
|
7
7
|
import { atobID, btoaID } from '@anker-in/shopify-core';
|
|
8
8
|
import useSWR from 'swr';
|
|
9
9
|
import useSWRMutation from 'swr/mutation';
|
|
10
|
-
import { useRequest } from 'ahooks';
|
|
10
|
+
import { useDebounceEffect, useRequest } from 'ahooks';
|
|
11
11
|
|
|
12
12
|
// src/provider/context.ts
|
|
13
13
|
var ShopifyContext = createContext(null);
|
|
@@ -1155,6 +1155,26 @@ function useRemoveCartCodes(options) {
|
|
|
1155
1155
|
);
|
|
1156
1156
|
return useSWRMutation("remove-codes", removeCodes, options);
|
|
1157
1157
|
}
|
|
1158
|
+
function useUpdateCartLines(options) {
|
|
1159
|
+
const { client, locale, cartCookieAdapter } = useShopify();
|
|
1160
|
+
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
1161
|
+
const updateLines = useCallback(
|
|
1162
|
+
async (_key, { arg }) => {
|
|
1163
|
+
const updatedCart = await updateCartLines(client, {
|
|
1164
|
+
...arg,
|
|
1165
|
+
metafieldIdentifiers,
|
|
1166
|
+
cookieAdapter: cartCookieAdapter
|
|
1167
|
+
});
|
|
1168
|
+
if (updatedCart) {
|
|
1169
|
+
mutateCart(updatedCart);
|
|
1170
|
+
}
|
|
1171
|
+
console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
|
|
1172
|
+
return updatedCart;
|
|
1173
|
+
},
|
|
1174
|
+
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
1175
|
+
);
|
|
1176
|
+
return useSWRMutation("update-cart-lines", updateLines, options);
|
|
1177
|
+
}
|
|
1158
1178
|
var initSameLinesAttributes = ({
|
|
1159
1179
|
cart,
|
|
1160
1180
|
line
|
|
@@ -1287,9 +1307,13 @@ function useAutoRemoveFreeGifts(options = {}) {
|
|
|
1287
1307
|
const {
|
|
1288
1308
|
removeFunctionGifts = true,
|
|
1289
1309
|
removeScriptGifts = true,
|
|
1290
|
-
isGiftLineItem
|
|
1310
|
+
isGiftLineItem,
|
|
1311
|
+
runOnlyOnceAfterInit = false,
|
|
1312
|
+
initDelay = 500
|
|
1291
1313
|
} = options;
|
|
1292
1314
|
const [isRemoving, setIsRemoving] = useState(false);
|
|
1315
|
+
const [isInitialized, setIsInitialized] = useState(!runOnlyOnceAfterInit);
|
|
1316
|
+
const [isFinished, setIsFinished] = useState(false);
|
|
1293
1317
|
const { cart } = useCartContext();
|
|
1294
1318
|
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
1295
1319
|
const giftsToRemove = useMemo(() => {
|
|
@@ -1326,7 +1350,29 @@ function useAutoRemoveFreeGifts(options = {}) {
|
|
|
1326
1350
|
return false;
|
|
1327
1351
|
});
|
|
1328
1352
|
}, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
|
|
1353
|
+
useDebounceEffect(
|
|
1354
|
+
() => {
|
|
1355
|
+
if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
|
|
1356
|
+
return;
|
|
1357
|
+
}
|
|
1358
|
+
if (!cart?.lineItems?.length) {
|
|
1359
|
+
return;
|
|
1360
|
+
}
|
|
1361
|
+
setIsInitialized(true);
|
|
1362
|
+
if (giftsToRemove.length === 0) {
|
|
1363
|
+
setIsFinished(true);
|
|
1364
|
+
}
|
|
1365
|
+
},
|
|
1366
|
+
[runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
|
|
1367
|
+
{
|
|
1368
|
+
trailing: true,
|
|
1369
|
+
wait: initDelay
|
|
1370
|
+
}
|
|
1371
|
+
);
|
|
1329
1372
|
useEffect(() => {
|
|
1373
|
+
if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
|
|
1374
|
+
return;
|
|
1375
|
+
}
|
|
1330
1376
|
if (isRemoving || giftsToRemove.length === 0) {
|
|
1331
1377
|
return;
|
|
1332
1378
|
}
|
|
@@ -1340,10 +1386,16 @@ function useAutoRemoveFreeGifts(options = {}) {
|
|
|
1340
1386
|
console.error("Failed to remove free gifts:", error);
|
|
1341
1387
|
} finally {
|
|
1342
1388
|
setIsRemoving(false);
|
|
1389
|
+
if (runOnlyOnceAfterInit) {
|
|
1390
|
+
setIsFinished(true);
|
|
1391
|
+
}
|
|
1343
1392
|
}
|
|
1344
1393
|
};
|
|
1345
1394
|
performRemoval();
|
|
1346
1395
|
}, [
|
|
1396
|
+
runOnlyOnceAfterInit,
|
|
1397
|
+
isInitialized,
|
|
1398
|
+
isFinished,
|
|
1347
1399
|
isRemoving,
|
|
1348
1400
|
giftsToRemove,
|
|
1349
1401
|
removeCartLines2
|
|
@@ -2455,6 +2507,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
2455
2507
|
const { trigger: applyCartCodes } = useApplyCartCodes();
|
|
2456
2508
|
const { trigger: removeInvalidCodes } = useRemoveCartCodes();
|
|
2457
2509
|
const { trigger: addCartLines2 } = useAddCartLines();
|
|
2510
|
+
const { trigger: updateCartLines3 } = useUpdateCartLines();
|
|
2458
2511
|
const { trigger: createCart4 } = useCreateCart({
|
|
2459
2512
|
updateCookie: true
|
|
2460
2513
|
});
|
|
@@ -2507,10 +2560,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
2507
2560
|
// 初次加购时,就把所有 cart attributes 带上
|
|
2508
2561
|
});
|
|
2509
2562
|
} else {
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2563
|
+
const linesToUpdate = [];
|
|
2564
|
+
const linesToAddNew = lines.filter((lineToAdd) => {
|
|
2565
|
+
const existingLine = cart?.lineItems?.find(
|
|
2566
|
+
(item) => item.variant?.id === lineToAdd.merchandiseId
|
|
2567
|
+
);
|
|
2568
|
+
if (existingLine) {
|
|
2569
|
+
linesToUpdate.push({
|
|
2570
|
+
id: existingLine.id,
|
|
2571
|
+
quantity: existingLine.quantity + (lineToAdd.quantity || 1),
|
|
2572
|
+
attributes: lineToAdd.attributes || void 0
|
|
2573
|
+
});
|
|
2574
|
+
return false;
|
|
2575
|
+
}
|
|
2576
|
+
return true;
|
|
2513
2577
|
});
|
|
2578
|
+
if (linesToUpdate.length > 0) {
|
|
2579
|
+
resultCart = await updateCartLines3({
|
|
2580
|
+
cartId,
|
|
2581
|
+
lines: linesToUpdate
|
|
2582
|
+
});
|
|
2583
|
+
}
|
|
2584
|
+
if (linesToAddNew.length > 0) {
|
|
2585
|
+
resultCart = await addCartLines2({
|
|
2586
|
+
cartId: resultCart?.id || cartId,
|
|
2587
|
+
lines: linesToAddNew
|
|
2588
|
+
});
|
|
2589
|
+
}
|
|
2590
|
+
if (!resultCart) {
|
|
2591
|
+
resultCart = cart;
|
|
2592
|
+
}
|
|
2514
2593
|
console.log("npm addCartLines resultCart", resultCart);
|
|
2515
2594
|
if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
|
|
2516
2595
|
const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
@@ -2571,6 +2650,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
2571
2650
|
performanceAdapter,
|
|
2572
2651
|
createCart4,
|
|
2573
2652
|
addCartLines2,
|
|
2653
|
+
updateCartLines3,
|
|
2574
2654
|
applyCartCodes,
|
|
2575
2655
|
removeInvalidCodes,
|
|
2576
2656
|
addCustomAttributes,
|
|
@@ -2580,26 +2660,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
2580
2660
|
);
|
|
2581
2661
|
return useSWRMutation("add-to-cart", addToCart, swrOptions);
|
|
2582
2662
|
}
|
|
2583
|
-
function useUpdateCartLines(options) {
|
|
2584
|
-
const { client, locale, cartCookieAdapter } = useShopify();
|
|
2585
|
-
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
2586
|
-
const updateLines = useCallback(
|
|
2587
|
-
async (_key, { arg }) => {
|
|
2588
|
-
const updatedCart = await updateCartLines(client, {
|
|
2589
|
-
...arg,
|
|
2590
|
-
metafieldIdentifiers,
|
|
2591
|
-
cookieAdapter: cartCookieAdapter
|
|
2592
|
-
});
|
|
2593
|
-
if (updatedCart) {
|
|
2594
|
-
mutateCart(updatedCart);
|
|
2595
|
-
}
|
|
2596
|
-
console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
|
|
2597
|
-
return updatedCart;
|
|
2598
|
-
},
|
|
2599
|
-
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
2600
|
-
);
|
|
2601
|
-
return useSWRMutation("update-cart-lines", updateLines, options);
|
|
2602
|
-
}
|
|
2603
2663
|
function useUpdateCartAttributes({
|
|
2604
2664
|
mutate,
|
|
2605
2665
|
metafieldIdentifiers,
|
|
@@ -3738,7 +3798,9 @@ function CartProvider({
|
|
|
3738
3798
|
const autoRemoveFreeGiftsOptions = useMemo(() => {
|
|
3739
3799
|
return {
|
|
3740
3800
|
removeFunctionGifts: !!functionAutoFreeGiftConfig,
|
|
3741
|
-
removeScriptGifts: !!scriptAutoFreeGiftConfig
|
|
3801
|
+
removeScriptGifts: !!scriptAutoFreeGiftConfig,
|
|
3802
|
+
runOnlyOnceAfterInit: true,
|
|
3803
|
+
initDelay: 500
|
|
3742
3804
|
};
|
|
3743
3805
|
}, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
|
|
3744
3806
|
const value = useMemo(
|