@anker-in/shopify-react 1.3.0-beta.3 → 1.3.0-beta.5

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,6 +1,6 @@
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';
@@ -1156,6 +1156,26 @@ function useRemoveCartCodes(options) {
1156
1156
  );
1157
1157
  return useSWRMutation("remove-codes", removeCodes, options);
1158
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
+ }
1159
1179
  var initSameLinesAttributes = ({
1160
1180
  cart,
1161
1181
  line
@@ -2317,6 +2337,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2317
2337
  const { trigger: applyCartCodes } = useApplyCartCodes();
2318
2338
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
2319
2339
  const { trigger: addCartLines2 } = useAddCartLines();
2340
+ const { trigger: updateCartLines3 } = useUpdateCartLines();
2320
2341
  const { trigger: createCart4 } = useCreateCart({
2321
2342
  updateCookie: true
2322
2343
  });
@@ -2369,10 +2390,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2369
2390
  // 初次加购时,就把所有 cart attributes 带上
2370
2391
  });
2371
2392
  } else {
2372
- resultCart = await addCartLines2({
2373
- cartId,
2374
- 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;
2375
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
+ }
2376
2423
  console.log("npm addCartLines resultCart", resultCart);
2377
2424
  if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
2378
2425
  const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
@@ -2433,6 +2480,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2433
2480
  performanceAdapter,
2434
2481
  createCart4,
2435
2482
  addCartLines2,
2483
+ updateCartLines3,
2436
2484
  applyCartCodes,
2437
2485
  removeInvalidCodes,
2438
2486
  addCustomAttributes,
@@ -2442,26 +2490,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2442
2490
  );
2443
2491
  return useSWRMutation("add-to-cart", addToCart, swrOptions);
2444
2492
  }
2445
- function useUpdateCartLines(options) {
2446
- const { client, locale, cartCookieAdapter } = useShopify();
2447
- const { mutateCart, metafieldIdentifiers } = useCartContext();
2448
- const updateLines = useCallback(
2449
- async (_key, { arg }) => {
2450
- const updatedCart = await updateCartLines(client, {
2451
- ...arg,
2452
- metafieldIdentifiers,
2453
- cookieAdapter: cartCookieAdapter
2454
- });
2455
- if (updatedCart) {
2456
- mutateCart(updatedCart);
2457
- }
2458
- console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
2459
- return updatedCart;
2460
- },
2461
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
2462
- );
2463
- return useSWRMutation("update-cart-lines", updateLines, options);
2464
- }
2465
2493
  function useUpdateCartAttributes({
2466
2494
  mutate,
2467
2495
  metafieldIdentifiers,