@anker-in/shopify-react 1.3.0-beta.1 → 1.3.0-beta.10
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 +4 -4
- package/dist/hooks/index.d.ts +4 -4
- package/dist/hooks/index.js +224 -165
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +226 -167
- 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 +114 -31
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +117 -34
- 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 +173 -7
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +175 -9
- 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 +3 -3
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
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
|
-
import { jsx } from 'react/jsx-runtime';
|
|
5
|
+
import { jsx, jsxs } 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
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,
|
|
@@ -3198,8 +3258,7 @@ async function performSearch(client, locale, searchQuery, first = 20, types = ["
|
|
|
3198
3258
|
const query = (
|
|
3199
3259
|
/* GraphQL */
|
|
3200
3260
|
`
|
|
3201
|
-
query search($query: String!, $first: Int!, $types: [SearchType!])
|
|
3202
|
-
@inContext(language: $language) {
|
|
3261
|
+
query search($query: String!, $first: Int!, $types: [SearchType!]) {
|
|
3203
3262
|
search(query: $query, first: $first, types: $types, unavailableProducts: HIDE) {
|
|
3204
3263
|
totalCount
|
|
3205
3264
|
edges {
|
|
@@ -3295,7 +3354,7 @@ async function getSiteInfo(client, locale, metafieldIdentifiers) {
|
|
|
3295
3354
|
`
|
|
3296
3355
|
query getSiteInfo(
|
|
3297
3356
|
${hasMetafields ? "$shopMetafieldIdentifiers: [HasMetafieldsIdentifier!]!" : ""}
|
|
3298
|
-
)
|
|
3357
|
+
) {
|
|
3299
3358
|
shop {
|
|
3300
3359
|
name
|
|
3301
3360
|
description
|
|
@@ -3541,6 +3600,16 @@ function clearGeoLocationCache(cacheKey = "geoLocation") {
|
|
|
3541
3600
|
}
|
|
3542
3601
|
}
|
|
3543
3602
|
var CartContext = createContext(null);
|
|
3603
|
+
function AutoRemoveGiftsHandler({
|
|
3604
|
+
options,
|
|
3605
|
+
onRemovingChange
|
|
3606
|
+
}) {
|
|
3607
|
+
const { isRemoving } = useAutoRemoveFreeGifts(options);
|
|
3608
|
+
useEffect(() => {
|
|
3609
|
+
onRemovingChange(isRemoving);
|
|
3610
|
+
}, [isRemoving, onRemovingChange]);
|
|
3611
|
+
return null;
|
|
3612
|
+
}
|
|
3544
3613
|
function CartProvider({
|
|
3545
3614
|
children,
|
|
3546
3615
|
// swrOptions,
|
|
@@ -3568,6 +3637,7 @@ function CartProvider({
|
|
|
3568
3637
|
});
|
|
3569
3638
|
const [scriptAutoFreeGift, setScriptAutoFreeGift] = useState([]);
|
|
3570
3639
|
const [functionAutoFreeGift, setFunctionAutoFreeGift] = useState([]);
|
|
3640
|
+
const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = useState(false);
|
|
3571
3641
|
const {
|
|
3572
3642
|
run: fetchCart,
|
|
3573
3643
|
data: cart,
|
|
@@ -3727,7 +3797,9 @@ function CartProvider({
|
|
|
3727
3797
|
const autoRemoveFreeGiftsOptions = useMemo(() => {
|
|
3728
3798
|
return {
|
|
3729
3799
|
removeFunctionGifts: !!functionAutoFreeGiftConfig,
|
|
3730
|
-
removeScriptGifts: !!scriptAutoFreeGiftConfig
|
|
3800
|
+
removeScriptGifts: !!scriptAutoFreeGiftConfig,
|
|
3801
|
+
runOnlyOnceAfterInit: true,
|
|
3802
|
+
initDelay: 500
|
|
3731
3803
|
};
|
|
3732
3804
|
}, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
|
|
3733
3805
|
const value = useMemo(
|
|
@@ -3760,7 +3832,8 @@ function CartProvider({
|
|
|
3760
3832
|
metafieldIdentifiers,
|
|
3761
3833
|
memberSetting,
|
|
3762
3834
|
appContext,
|
|
3763
|
-
autoRemoveFreeGiftsOptions
|
|
3835
|
+
autoRemoveFreeGiftsOptions,
|
|
3836
|
+
isAutoRemovingFreeGifts
|
|
3764
3837
|
}),
|
|
3765
3838
|
[
|
|
3766
3839
|
cart,
|
|
@@ -3789,10 +3862,20 @@ function CartProvider({
|
|
|
3789
3862
|
profile,
|
|
3790
3863
|
memberSetting,
|
|
3791
3864
|
appContext,
|
|
3792
|
-
autoRemoveFreeGiftsOptions
|
|
3865
|
+
autoRemoveFreeGiftsOptions,
|
|
3866
|
+
isAutoRemovingFreeGifts
|
|
3793
3867
|
]
|
|
3794
3868
|
);
|
|
3795
|
-
return /* @__PURE__ */
|
|
3869
|
+
return /* @__PURE__ */ jsxs(CartContext.Provider, { value, children: [
|
|
3870
|
+
(functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsx(
|
|
3871
|
+
AutoRemoveGiftsHandler,
|
|
3872
|
+
{
|
|
3873
|
+
options: autoRemoveFreeGiftsOptions,
|
|
3874
|
+
onRemovingChange: setIsAutoRemovingFreeGifts
|
|
3875
|
+
}
|
|
3876
|
+
),
|
|
3877
|
+
children
|
|
3878
|
+
] });
|
|
3796
3879
|
}
|
|
3797
3880
|
function useCartContext(options) {
|
|
3798
3881
|
const context = useContext(CartContext);
|