@anker-in/shopify-react 1.3.0-beta.1 → 1.3.0-beta.11
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 +7 -26
- package/dist/hooks/index.d.ts +7 -26
- package/dist/hooks/index.js +227 -229
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +229 -231
- 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 +117 -95
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +120 -98
- 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 +5 -5
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, getShop, 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 {
|
|
@@ -3288,76 +3347,15 @@ function useSearch(options = {}) {
|
|
|
3288
3347
|
swrOptions
|
|
3289
3348
|
);
|
|
3290
3349
|
}
|
|
3291
|
-
async function getSiteInfo(client, locale, metafieldIdentifiers) {
|
|
3292
|
-
const hasMetafields = metafieldIdentifiers && metafieldIdentifiers.length > 0;
|
|
3293
|
-
const query = (
|
|
3294
|
-
/* GraphQL */
|
|
3295
|
-
`
|
|
3296
|
-
query getSiteInfo(
|
|
3297
|
-
${hasMetafields ? "$shopMetafieldIdentifiers: [HasMetafieldsIdentifier!]!" : ""}
|
|
3298
|
-
) @inContext(language: $language) {
|
|
3299
|
-
shop {
|
|
3300
|
-
name
|
|
3301
|
-
description
|
|
3302
|
-
primaryDomain {
|
|
3303
|
-
url
|
|
3304
|
-
host
|
|
3305
|
-
}
|
|
3306
|
-
brand {
|
|
3307
|
-
logo {
|
|
3308
|
-
image {
|
|
3309
|
-
url
|
|
3310
|
-
}
|
|
3311
|
-
}
|
|
3312
|
-
colors {
|
|
3313
|
-
primary {
|
|
3314
|
-
background
|
|
3315
|
-
}
|
|
3316
|
-
secondary {
|
|
3317
|
-
background
|
|
3318
|
-
}
|
|
3319
|
-
}
|
|
3320
|
-
}
|
|
3321
|
-
${hasMetafields ? "metafields(identifiers: $shopMetafieldIdentifiers) { key value }" : ""}
|
|
3322
|
-
}
|
|
3323
|
-
}
|
|
3324
|
-
`
|
|
3325
|
-
);
|
|
3326
|
-
const variables = {};
|
|
3327
|
-
if (hasMetafields) {
|
|
3328
|
-
variables.shopMetafieldIdentifiers = metafieldIdentifiers;
|
|
3329
|
-
}
|
|
3330
|
-
const data = await client.query(query, variables);
|
|
3331
|
-
if (!data || !data.shop) {
|
|
3332
|
-
return void 0;
|
|
3333
|
-
}
|
|
3334
|
-
const shop = data.shop;
|
|
3335
|
-
const metafields = shop.metafields?.reduce((acc, mf) => {
|
|
3336
|
-
if (mf && mf.key) {
|
|
3337
|
-
acc[mf.key] = mf.value;
|
|
3338
|
-
}
|
|
3339
|
-
return acc;
|
|
3340
|
-
}, {});
|
|
3341
|
-
return {
|
|
3342
|
-
name: shop.name,
|
|
3343
|
-
description: shop.description,
|
|
3344
|
-
primaryDomain: shop.primaryDomain,
|
|
3345
|
-
brand: shop.brand ? {
|
|
3346
|
-
logo: shop.brand.logo,
|
|
3347
|
-
colors: shop.brand.colors ? {
|
|
3348
|
-
primary: shop.brand.colors.primary?.background,
|
|
3349
|
-
secondary: shop.brand.colors.secondary?.background
|
|
3350
|
-
} : void 0
|
|
3351
|
-
} : void 0,
|
|
3352
|
-
metafields
|
|
3353
|
-
};
|
|
3354
|
-
}
|
|
3355
3350
|
function useSite(options = {}) {
|
|
3356
3351
|
const { client, locale } = useShopify();
|
|
3357
3352
|
const { metafieldIdentifiers, ...swrOptions } = options;
|
|
3358
3353
|
return useSWR(
|
|
3359
3354
|
["site", locale, metafieldIdentifiers],
|
|
3360
|
-
() =>
|
|
3355
|
+
() => getShop(client, {
|
|
3356
|
+
locale,
|
|
3357
|
+
metafieldIdentifiers
|
|
3358
|
+
}),
|
|
3361
3359
|
swrOptions
|
|
3362
3360
|
);
|
|
3363
3361
|
}
|
|
@@ -3541,6 +3539,16 @@ function clearGeoLocationCache(cacheKey = "geoLocation") {
|
|
|
3541
3539
|
}
|
|
3542
3540
|
}
|
|
3543
3541
|
var CartContext = createContext(null);
|
|
3542
|
+
function AutoRemoveGiftsHandler({
|
|
3543
|
+
options,
|
|
3544
|
+
onRemovingChange
|
|
3545
|
+
}) {
|
|
3546
|
+
const { isRemoving } = useAutoRemoveFreeGifts(options);
|
|
3547
|
+
useEffect(() => {
|
|
3548
|
+
onRemovingChange(isRemoving);
|
|
3549
|
+
}, [isRemoving, onRemovingChange]);
|
|
3550
|
+
return null;
|
|
3551
|
+
}
|
|
3544
3552
|
function CartProvider({
|
|
3545
3553
|
children,
|
|
3546
3554
|
// swrOptions,
|
|
@@ -3568,6 +3576,7 @@ function CartProvider({
|
|
|
3568
3576
|
});
|
|
3569
3577
|
const [scriptAutoFreeGift, setScriptAutoFreeGift] = useState([]);
|
|
3570
3578
|
const [functionAutoFreeGift, setFunctionAutoFreeGift] = useState([]);
|
|
3579
|
+
const [isAutoRemovingFreeGifts, setIsAutoRemovingFreeGifts] = useState(false);
|
|
3571
3580
|
const {
|
|
3572
3581
|
run: fetchCart,
|
|
3573
3582
|
data: cart,
|
|
@@ -3727,7 +3736,9 @@ function CartProvider({
|
|
|
3727
3736
|
const autoRemoveFreeGiftsOptions = useMemo(() => {
|
|
3728
3737
|
return {
|
|
3729
3738
|
removeFunctionGifts: !!functionAutoFreeGiftConfig,
|
|
3730
|
-
removeScriptGifts: !!scriptAutoFreeGiftConfig
|
|
3739
|
+
removeScriptGifts: !!scriptAutoFreeGiftConfig,
|
|
3740
|
+
runOnlyOnceAfterInit: true,
|
|
3741
|
+
initDelay: 500
|
|
3731
3742
|
};
|
|
3732
3743
|
}, [functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig]);
|
|
3733
3744
|
const value = useMemo(
|
|
@@ -3760,7 +3771,8 @@ function CartProvider({
|
|
|
3760
3771
|
metafieldIdentifiers,
|
|
3761
3772
|
memberSetting,
|
|
3762
3773
|
appContext,
|
|
3763
|
-
autoRemoveFreeGiftsOptions
|
|
3774
|
+
autoRemoveFreeGiftsOptions,
|
|
3775
|
+
isAutoRemovingFreeGifts
|
|
3764
3776
|
}),
|
|
3765
3777
|
[
|
|
3766
3778
|
cart,
|
|
@@ -3789,10 +3801,20 @@ function CartProvider({
|
|
|
3789
3801
|
profile,
|
|
3790
3802
|
memberSetting,
|
|
3791
3803
|
appContext,
|
|
3792
|
-
autoRemoveFreeGiftsOptions
|
|
3804
|
+
autoRemoveFreeGiftsOptions,
|
|
3805
|
+
isAutoRemovingFreeGifts
|
|
3793
3806
|
]
|
|
3794
3807
|
);
|
|
3795
|
-
return /* @__PURE__ */
|
|
3808
|
+
return /* @__PURE__ */ jsxs(CartContext.Provider, { value, children: [
|
|
3809
|
+
(functionAutoFreeGiftConfig || scriptAutoFreeGiftConfig) && /* @__PURE__ */ jsx(
|
|
3810
|
+
AutoRemoveGiftsHandler,
|
|
3811
|
+
{
|
|
3812
|
+
options: autoRemoveFreeGiftsOptions,
|
|
3813
|
+
onRemovingChange: setIsAutoRemovingFreeGifts
|
|
3814
|
+
}
|
|
3815
|
+
),
|
|
3816
|
+
children
|
|
3817
|
+
] });
|
|
3796
3818
|
}
|
|
3797
3819
|
function useCartContext(options) {
|
|
3798
3820
|
const context = useContext(CartContext);
|