@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/hooks/index.d.mts
CHANGED
|
@@ -2,8 +2,8 @@ import * as swr_mutation from 'swr/mutation';
|
|
|
2
2
|
import { SWRMutationConfiguration } from 'swr/mutation';
|
|
3
3
|
import * as _anker_in_shopify_sdk from '@anker-in/shopify-sdk';
|
|
4
4
|
import { CartLineInput, BuyerIdentityInput, NormalizedCart, NormalizedProduct, NormalizedProductVariant, NormalizedLineItem, UpdateCartLinesOptions, HasMetafieldsIdentifier, Media, NormalizedCollection, CollectionsConnection, NormalizedBlog, NormalizedArticle, NormalizedAttribute } from '@anker-in/shopify-sdk';
|
|
5
|
-
import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-
|
|
6
|
-
export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-
|
|
5
|
+
import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-C0UyuPrG.mjs';
|
|
6
|
+
export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-C0UyuPrG.mjs';
|
|
7
7
|
import * as swr from 'swr';
|
|
8
8
|
import swr__default, { SWRConfiguration } from 'swr';
|
|
9
9
|
import * as swr__internal from 'swr/_internal';
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import * as swr_mutation from 'swr/mutation';
|
|
|
2
2
|
import { SWRMutationConfiguration } from 'swr/mutation';
|
|
3
3
|
import * as _anker_in_shopify_sdk from '@anker-in/shopify-sdk';
|
|
4
4
|
import { CartLineInput, BuyerIdentityInput, NormalizedCart, NormalizedProduct, NormalizedProductVariant, NormalizedLineItem, UpdateCartLinesOptions, HasMetafieldsIdentifier, Media, NormalizedCollection, CollectionsConnection, NormalizedBlog, NormalizedArticle, NormalizedAttribute } from '@anker-in/shopify-sdk';
|
|
5
|
-
import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-
|
|
6
|
-
export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-
|
|
5
|
+
import { h as UseAddToCartOptions, g as AddToCartInput, A as AddToCartLineItem, G as GtmParams, C as BuyNowTrackConfig, e as UseScriptAutoFreeGiftResult, m as PlusMemberShippingMethodConfig, n as PlusMemberSettingsMetafields, D as DeliveryPlusType, o as SelectedPlusMemberVariant } from '../types-C0UyuPrG.js';
|
|
6
|
+
export { p as DeliveryCustomData, P as PLUS_MEMBER_TYPE, j as PlusMemberMode, l as PlusMemberShippingMethodMetafields, S as ScriptAutoFreeGiftConfig, k as ShippingMethodMode, U as UseAutoRemoveFreeGiftsOptions, a as UseAutoRemoveFreeGiftsResult, d as isAnyGift, c as isBuyGetGift, i as isFunctionGift, b as isScriptGift, u as useAutoRemoveFreeGifts, f as useScriptAutoFreeGift } from '../types-C0UyuPrG.js';
|
|
7
7
|
import * as swr from 'swr';
|
|
8
8
|
import swr__default, { SWRConfiguration } from 'swr';
|
|
9
9
|
import * as swr__internal from 'swr/_internal';
|
package/dist/hooks/index.js
CHANGED
|
@@ -716,9 +716,13 @@ function useAutoRemoveFreeGifts(options = {}) {
|
|
|
716
716
|
const {
|
|
717
717
|
removeFunctionGifts = true,
|
|
718
718
|
removeScriptGifts = true,
|
|
719
|
-
isGiftLineItem
|
|
719
|
+
isGiftLineItem,
|
|
720
|
+
runOnlyOnceAfterInit = false,
|
|
721
|
+
initDelay = 500
|
|
720
722
|
} = options;
|
|
721
723
|
const [isRemoving, setIsRemoving] = react.useState(false);
|
|
724
|
+
const [isInitialized, setIsInitialized] = react.useState(!runOnlyOnceAfterInit);
|
|
725
|
+
const [isFinished, setIsFinished] = react.useState(false);
|
|
722
726
|
const { cart } = useCartContext();
|
|
723
727
|
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
724
728
|
const giftsToRemove = react.useMemo(() => {
|
|
@@ -755,7 +759,29 @@ function useAutoRemoveFreeGifts(options = {}) {
|
|
|
755
759
|
return false;
|
|
756
760
|
});
|
|
757
761
|
}, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
|
|
762
|
+
ahooks.useDebounceEffect(
|
|
763
|
+
() => {
|
|
764
|
+
if (!runOnlyOnceAfterInit || isInitialized || isFinished) {
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
if (!cart?.lineItems?.length) {
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
setIsInitialized(true);
|
|
771
|
+
if (giftsToRemove.length === 0) {
|
|
772
|
+
setIsFinished(true);
|
|
773
|
+
}
|
|
774
|
+
},
|
|
775
|
+
[runOnlyOnceAfterInit, isInitialized, isFinished, cart?.lineItems, giftsToRemove.length],
|
|
776
|
+
{
|
|
777
|
+
trailing: true,
|
|
778
|
+
wait: initDelay
|
|
779
|
+
}
|
|
780
|
+
);
|
|
758
781
|
react.useEffect(() => {
|
|
782
|
+
if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
759
785
|
if (isRemoving || giftsToRemove.length === 0) {
|
|
760
786
|
return;
|
|
761
787
|
}
|
|
@@ -769,10 +795,16 @@ function useAutoRemoveFreeGifts(options = {}) {
|
|
|
769
795
|
console.error("Failed to remove free gifts:", error);
|
|
770
796
|
} finally {
|
|
771
797
|
setIsRemoving(false);
|
|
798
|
+
if (runOnlyOnceAfterInit) {
|
|
799
|
+
setIsFinished(true);
|
|
800
|
+
}
|
|
772
801
|
}
|
|
773
802
|
};
|
|
774
803
|
performRemoval();
|
|
775
804
|
}, [
|
|
805
|
+
runOnlyOnceAfterInit,
|
|
806
|
+
isInitialized,
|
|
807
|
+
isFinished,
|
|
776
808
|
isRemoving,
|
|
777
809
|
giftsToRemove,
|
|
778
810
|
removeCartLines2
|
|
@@ -1133,6 +1165,26 @@ function useRemoveCartCodes(options) {
|
|
|
1133
1165
|
);
|
|
1134
1166
|
return useSWRMutation__default.default("remove-codes", removeCodes, options);
|
|
1135
1167
|
}
|
|
1168
|
+
function useUpdateCartLines(options) {
|
|
1169
|
+
const { client, locale, cartCookieAdapter } = useShopify();
|
|
1170
|
+
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
1171
|
+
const updateLines = react.useCallback(
|
|
1172
|
+
async (_key, { arg }) => {
|
|
1173
|
+
const updatedCart = await shopifySdk.updateCartLines(client, {
|
|
1174
|
+
...arg,
|
|
1175
|
+
metafieldIdentifiers,
|
|
1176
|
+
cookieAdapter: cartCookieAdapter
|
|
1177
|
+
});
|
|
1178
|
+
if (updatedCart) {
|
|
1179
|
+
mutateCart(updatedCart);
|
|
1180
|
+
}
|
|
1181
|
+
console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
|
|
1182
|
+
return updatedCart;
|
|
1183
|
+
},
|
|
1184
|
+
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
1185
|
+
);
|
|
1186
|
+
return useSWRMutation__default.default("update-cart-lines", updateLines, options);
|
|
1187
|
+
}
|
|
1136
1188
|
var initSameLinesAttributes = ({
|
|
1137
1189
|
cart,
|
|
1138
1190
|
line
|
|
@@ -2294,6 +2346,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
2294
2346
|
const { trigger: applyCartCodes } = useApplyCartCodes();
|
|
2295
2347
|
const { trigger: removeInvalidCodes } = useRemoveCartCodes();
|
|
2296
2348
|
const { trigger: addCartLines2 } = useAddCartLines();
|
|
2349
|
+
const { trigger: updateCartLines3 } = useUpdateCartLines();
|
|
2297
2350
|
const { trigger: createCart4 } = useCreateCart({
|
|
2298
2351
|
updateCookie: true
|
|
2299
2352
|
});
|
|
@@ -2346,10 +2399,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
2346
2399
|
// 初次加购时,就把所有 cart attributes 带上
|
|
2347
2400
|
});
|
|
2348
2401
|
} else {
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2402
|
+
const linesToUpdate = [];
|
|
2403
|
+
const linesToAddNew = lines.filter((lineToAdd) => {
|
|
2404
|
+
const existingLine = cart?.lineItems?.find(
|
|
2405
|
+
(item) => item.variant?.id === lineToAdd.merchandiseId
|
|
2406
|
+
);
|
|
2407
|
+
if (existingLine) {
|
|
2408
|
+
linesToUpdate.push({
|
|
2409
|
+
id: existingLine.id,
|
|
2410
|
+
quantity: existingLine.quantity + (lineToAdd.quantity || 1),
|
|
2411
|
+
attributes: lineToAdd.attributes || void 0
|
|
2412
|
+
});
|
|
2413
|
+
return false;
|
|
2414
|
+
}
|
|
2415
|
+
return true;
|
|
2352
2416
|
});
|
|
2417
|
+
if (linesToUpdate.length > 0) {
|
|
2418
|
+
resultCart = await updateCartLines3({
|
|
2419
|
+
cartId,
|
|
2420
|
+
lines: linesToUpdate
|
|
2421
|
+
});
|
|
2422
|
+
}
|
|
2423
|
+
if (linesToAddNew.length > 0) {
|
|
2424
|
+
resultCart = await addCartLines2({
|
|
2425
|
+
cartId: resultCart?.id || cartId,
|
|
2426
|
+
lines: linesToAddNew
|
|
2427
|
+
});
|
|
2428
|
+
}
|
|
2429
|
+
if (!resultCart) {
|
|
2430
|
+
resultCart = cart;
|
|
2431
|
+
}
|
|
2353
2432
|
console.log("npm addCartLines resultCart", resultCart);
|
|
2354
2433
|
if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
|
|
2355
2434
|
const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
@@ -2410,6 +2489,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
2410
2489
|
performanceAdapter,
|
|
2411
2490
|
createCart4,
|
|
2412
2491
|
addCartLines2,
|
|
2492
|
+
updateCartLines3,
|
|
2413
2493
|
applyCartCodes,
|
|
2414
2494
|
removeInvalidCodes,
|
|
2415
2495
|
addCustomAttributes,
|
|
@@ -2419,26 +2499,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
2419
2499
|
);
|
|
2420
2500
|
return useSWRMutation__default.default("add-to-cart", addToCart, swrOptions);
|
|
2421
2501
|
}
|
|
2422
|
-
function useUpdateCartLines(options) {
|
|
2423
|
-
const { client, locale, cartCookieAdapter } = useShopify();
|
|
2424
|
-
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
2425
|
-
const updateLines = react.useCallback(
|
|
2426
|
-
async (_key, { arg }) => {
|
|
2427
|
-
const updatedCart = await shopifySdk.updateCartLines(client, {
|
|
2428
|
-
...arg,
|
|
2429
|
-
metafieldIdentifiers,
|
|
2430
|
-
cookieAdapter: cartCookieAdapter
|
|
2431
|
-
});
|
|
2432
|
-
if (updatedCart) {
|
|
2433
|
-
mutateCart(updatedCart);
|
|
2434
|
-
}
|
|
2435
|
-
console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
|
|
2436
|
-
return updatedCart;
|
|
2437
|
-
},
|
|
2438
|
-
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
2439
|
-
);
|
|
2440
|
-
return useSWRMutation__default.default("update-cart-lines", updateLines, options);
|
|
2441
|
-
}
|
|
2442
2502
|
function useUpdateCartAttributes({
|
|
2443
2503
|
mutate,
|
|
2444
2504
|
metafieldIdentifiers,
|