@anker-in/shopify-react 1.2.2-beta.6 → 1.2.2-beta.7

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.
@@ -2,7 +2,7 @@ import { createContext, useMemo, useContext, useState, useCallback, useEffect, u
2
2
  import { createShopifyClient, getCart, updateCartAttributes, updateCartLines, getProductsByHandles, getLocalStorage } from '@anker-in/shopify-sdk';
3
3
  import Cookies5 from 'js-cookie';
4
4
  import { jsx } from 'react/jsx-runtime';
5
- import Decimal2 from 'decimal.js';
5
+ import Decimal3 from 'decimal.js';
6
6
  import { btoaID, atobID } from '@anker-in/shopify-core';
7
7
  import useSWR from 'swr';
8
8
  import useSWRMutation8 from 'swr/mutation';
@@ -98,8 +98,6 @@ var CUSTOMER_SCRIPT_GIFT_KEY = "_giveaway_gradient_gifts";
98
98
  var CODE_AMOUNT_KEY = "_sku_code_money";
99
99
  var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
100
100
  var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
101
-
102
- // src/hooks/cart/utils/normalize-add-to-cart-lines.ts
103
101
  function normalizeAddToCartLines(lines) {
104
102
  return lines.filter((line) => line.variant?.id).map((line, index) => {
105
103
  const variant = line.variant;
@@ -107,8 +105,8 @@ function normalizeAddToCartLines(lines) {
107
105
  const quantity = line.quantity || 1;
108
106
  const originalPrice = variant.price?.amount ? Number(variant.price.amount) : 0;
109
107
  const finalPrice = variant.finalPrice?.amount === void 0 ? originalPrice : Number(variant.finalPrice?.amount);
110
- const subtotalAmount = originalPrice * quantity;
111
- const totalAmount = finalPrice * quantity;
108
+ const subtotalAmount = new Decimal3(originalPrice).times(quantity).toNumber();
109
+ const totalAmount = new Decimal3(finalPrice).times(quantity).toNumber();
112
110
  return {
113
111
  id: `temp-line-${index}-${variant.id}`,
114
112
  // Temporary ID for pre-cart lines
@@ -116,6 +114,7 @@ function normalizeAddToCartLines(lines) {
116
114
  quantity,
117
115
  variantId: variant.id,
118
116
  productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
117
+ amountPerQuantity: finalPrice,
119
118
  totalAmount,
120
119
  subtotalAmount,
121
120
  discountAllocations: [],
@@ -300,7 +299,7 @@ var formatScriptAutoFreeGift = ({
300
299
  path: product?.handle || "",
301
300
  variant,
302
301
  totalAmount: 0,
303
- subtotalAmount: new Decimal2(
302
+ subtotalAmount: new Decimal3(
304
303
  typeof variant?.price === "object" ? variant?.price?.amount || 0 : variant?.price || 0
305
304
  ).toNumber(),
306
305
  options: [],
@@ -360,7 +359,7 @@ var formatFunctionAutoFreeGift = ({
360
359
  path: product?.handle || "",
361
360
  variant,
362
361
  totalAmount: 0,
363
- subtotalAmount: new Decimal2(
362
+ subtotalAmount: new Decimal3(
364
363
  typeof variant?.price === "object" ? variant?.price?.amount || 0 : variant?.price || 0
365
364
  ).toNumber(),
366
365
  options: [],
@@ -587,7 +586,7 @@ var useScriptAutoFreeGift = ({
587
586
  const breakpoints = useMemo(() => {
588
587
  if (!isActivityAvailable) return [];
589
588
  return (campaign?.breakpoints || []).map((item) => ({
590
- breakpoint: new Decimal2(item.breakpoint).minus(new Decimal2(upgrade_value)).dividedBy(new Decimal2(upgrade_multiple)).toFixed(2, Decimal2.ROUND_DOWN),
589
+ breakpoint: new Decimal3(item.breakpoint).minus(new Decimal3(upgrade_value)).dividedBy(new Decimal3(upgrade_multiple)).toFixed(2, Decimal3.ROUND_DOWN),
591
590
  giveawayProducts: item.giveawayProducts || []
592
591
  }));
593
592
  }, [campaign, upgrade_multiple, upgrade_value]);
@@ -622,11 +621,11 @@ var useScriptAutoFreeGift = ({
622
621
  });
623
622
  }, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
624
623
  const involvedSubTotal = useMemo(() => {
625
- if (!isActivityAvailable) return new Decimal2(0);
624
+ if (!isActivityAvailable) return new Decimal3(0);
626
625
  return involvedLines.reduce((prev, item) => {
627
626
  const amount = campaign?.useTotalAmount ? item.totalAmount : item.subtotalAmount;
628
- return new Decimal2(prev).plus(new Decimal2(amount || 0));
629
- }, new Decimal2(0));
627
+ return new Decimal3(prev).plus(new Decimal3(amount || 0));
628
+ }, new Decimal3(0));
630
629
  }, [involvedLines, isActivityAvailable]);
631
630
  const [freeGiftLevel, nextFreeGiftLevel] = useMemo(() => {
632
631
  if (!isActivityAvailable) return [null, null];
@@ -634,7 +633,7 @@ var useScriptAutoFreeGift = ({
634
633
  (a, b) => Number(b.breakpoint) - Number(a.breakpoint)
635
634
  );
636
635
  const levelIndex = sortedLevels.findIndex(
637
- (level) => involvedSubTotal.gte(new Decimal2(level.breakpoint)) && involvedLines.length > 0
636
+ (level) => involvedSubTotal.gte(new Decimal3(level.breakpoint)) && involvedLines.length > 0
638
637
  );
639
638
  if (levelIndex === -1) {
640
639
  return [
@@ -872,8 +871,8 @@ function getCartBasicAttributes({
872
871
  {
873
872
  key: "_weight",
874
873
  value: cart?.lineItems?.reduce((acc, item) => {
875
- const itemWeight = new Decimal2(item.variant.weight ?? 0).times(item.quantity);
876
- return new Decimal2(acc).plus(itemWeight).toNumber();
874
+ const itemWeight = new Decimal3(item.variant.weight ?? 0).times(item.quantity);
875
+ return new Decimal3(acc).plus(itemWeight).toNumber();
877
876
  }, 0).toString()
878
877
  }
879
878
  ];