@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.
@@ -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, removeCartLines, updateCartCodes, createCart, addCartLines, getLocalStorage, updateCartLines, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage } from '@anker-in/shopify-sdk';
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);
@@ -707,9 +707,13 @@ function useAutoRemoveFreeGifts(options = {}) {
707
707
  const {
708
708
  removeFunctionGifts = true,
709
709
  removeScriptGifts = true,
710
- isGiftLineItem
710
+ isGiftLineItem,
711
+ runOnlyOnceAfterInit = false,
712
+ initDelay = 500
711
713
  } = options;
712
714
  const [isRemoving, setIsRemoving] = useState(false);
715
+ const [isInitialized, setIsInitialized] = useState(!runOnlyOnceAfterInit);
716
+ const [isFinished, setIsFinished] = useState(false);
713
717
  const { cart } = useCartContext();
714
718
  const { trigger: removeCartLines2 } = useRemoveCartLines();
715
719
  const giftsToRemove = useMemo(() => {
@@ -746,7 +750,29 @@ function useAutoRemoveFreeGifts(options = {}) {
746
750
  return false;
747
751
  });
748
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
+ );
749
772
  useEffect(() => {
773
+ if (runOnlyOnceAfterInit && (!isInitialized || isFinished)) {
774
+ return;
775
+ }
750
776
  if (isRemoving || giftsToRemove.length === 0) {
751
777
  return;
752
778
  }
@@ -760,10 +786,16 @@ function useAutoRemoveFreeGifts(options = {}) {
760
786
  console.error("Failed to remove free gifts:", error);
761
787
  } finally {
762
788
  setIsRemoving(false);
789
+ if (runOnlyOnceAfterInit) {
790
+ setIsFinished(true);
791
+ }
763
792
  }
764
793
  };
765
794
  performRemoval();
766
795
  }, [
796
+ runOnlyOnceAfterInit,
797
+ isInitialized,
798
+ isFinished,
767
799
  isRemoving,
768
800
  giftsToRemove,
769
801
  removeCartLines2
@@ -1124,6 +1156,26 @@ function useRemoveCartCodes(options) {
1124
1156
  );
1125
1157
  return useSWRMutation("remove-codes", removeCodes, options);
1126
1158
  }
1159
+ function useUpdateCartLines(options) {
1160
+ const { client, locale, cartCookieAdapter } = useShopify();
1161
+ const { mutateCart, metafieldIdentifiers } = useCartContext();
1162
+ const updateLines = useCallback(
1163
+ async (_key, { arg }) => {
1164
+ const updatedCart = await updateCartLines(client, {
1165
+ ...arg,
1166
+ metafieldIdentifiers,
1167
+ cookieAdapter: cartCookieAdapter
1168
+ });
1169
+ if (updatedCart) {
1170
+ mutateCart(updatedCart);
1171
+ }
1172
+ console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
1173
+ return updatedCart;
1174
+ },
1175
+ [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
1176
+ );
1177
+ return useSWRMutation("update-cart-lines", updateLines, options);
1178
+ }
1127
1179
  var initSameLinesAttributes = ({
1128
1180
  cart,
1129
1181
  line
@@ -2285,6 +2337,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2285
2337
  const { trigger: applyCartCodes } = useApplyCartCodes();
2286
2338
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
2287
2339
  const { trigger: addCartLines2 } = useAddCartLines();
2340
+ const { trigger: updateCartLines3 } = useUpdateCartLines();
2288
2341
  const { trigger: createCart4 } = useCreateCart({
2289
2342
  updateCookie: true
2290
2343
  });
@@ -2337,10 +2390,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2337
2390
  // 初次加购时,就把所有 cart attributes 带上
2338
2391
  });
2339
2392
  } else {
2340
- resultCart = await addCartLines2({
2341
- cartId,
2342
- lines
2393
+ const linesToUpdate = [];
2394
+ const linesToAddNew = lines.filter((lineToAdd) => {
2395
+ const existingLine = cart?.lineItems?.find(
2396
+ (item) => item.variant?.id === lineToAdd.merchandiseId
2397
+ );
2398
+ if (existingLine) {
2399
+ linesToUpdate.push({
2400
+ id: existingLine.id,
2401
+ quantity: existingLine.quantity + (lineToAdd.quantity || 1),
2402
+ attributes: lineToAdd.attributes || void 0
2403
+ });
2404
+ return false;
2405
+ }
2406
+ return true;
2343
2407
  });
2408
+ if (linesToUpdate.length > 0) {
2409
+ resultCart = await updateCartLines3({
2410
+ cartId,
2411
+ lines: linesToUpdate
2412
+ });
2413
+ }
2414
+ if (linesToAddNew.length > 0) {
2415
+ resultCart = await addCartLines2({
2416
+ cartId: resultCart?.id || cartId,
2417
+ lines: linesToAddNew
2418
+ });
2419
+ }
2420
+ if (!resultCart) {
2421
+ resultCart = cart;
2422
+ }
2344
2423
  console.log("npm addCartLines resultCart", resultCart);
2345
2424
  if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
2346
2425
  const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
@@ -2401,6 +2480,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2401
2480
  performanceAdapter,
2402
2481
  createCart4,
2403
2482
  addCartLines2,
2483
+ updateCartLines3,
2404
2484
  applyCartCodes,
2405
2485
  removeInvalidCodes,
2406
2486
  addCustomAttributes,
@@ -2410,26 +2490,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2410
2490
  );
2411
2491
  return useSWRMutation("add-to-cart", addToCart, swrOptions);
2412
2492
  }
2413
- function useUpdateCartLines(options) {
2414
- const { client, locale, cartCookieAdapter } = useShopify();
2415
- const { mutateCart, metafieldIdentifiers } = useCartContext();
2416
- const updateLines = useCallback(
2417
- async (_key, { arg }) => {
2418
- const updatedCart = await updateCartLines(client, {
2419
- ...arg,
2420
- metafieldIdentifiers,
2421
- cookieAdapter: cartCookieAdapter
2422
- });
2423
- if (updatedCart) {
2424
- mutateCart(updatedCart);
2425
- }
2426
- console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
2427
- return updatedCart;
2428
- },
2429
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
2430
- );
2431
- return useSWRMutation("update-cart-lines", updateLines, options);
2432
- }
2433
2493
  function useUpdateCartAttributes({
2434
2494
  mutate,
2435
2495
  metafieldIdentifiers,