@anker-in/shopify-react 0.1.1-beta.1 → 0.1.1-beta.2

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.
@@ -143,25 +143,25 @@ var getMatchedMainProductSubTotal = (cartData, variant_list, main_product) => {
143
143
  return acc + (main_product?.spend_money_type === 1 /* ORIGIN_PRICE */ ? Number(line.subtotalAmount) || 0 : Number(line.totalAmount) || 0);
144
144
  }, 0) || 0;
145
145
  };
146
- var getDiscountEnvAttributeValue = (attributes = []) => {
147
- const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
148
- return safeParseJson(attr?.value ?? "") ?? {};
149
- };
150
- var isAttributesEqual = (attrs1 = [], attrs2 = []) => {
151
- if (attrs1.length !== attrs2.length) return false;
152
- const sorted1 = [...attrs1].sort((a, b) => a.key.localeCompare(b.key));
153
- const sorted2 = [...attrs2].sort((a, b) => a.key.localeCompare(b.key));
154
- return sorted1.every(
155
- (attr, i) => attr.key === sorted2[i]?.key && attr.value === sorted2[i]?.value
156
- );
157
- };
158
- var safeParseJson = (str) => {
146
+ var safeParse = (str) => {
159
147
  try {
160
148
  return JSON.parse(str);
161
149
  } catch (err) {
162
150
  return {};
163
151
  }
164
152
  };
153
+ var getDiscountEnvAttributeValue = (attributes = []) => {
154
+ const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
155
+ return safeParse(attr?.value ?? "") ?? {};
156
+ };
157
+ var checkAttributesUpdateNeeded = (oldAttributes, newAttributes, customAttributesNeedRemove) => {
158
+ return oldAttributes.some((attr) => {
159
+ const newAttr = newAttributes.find((newAttr2) => newAttr2.key === attr.key);
160
+ return newAttr ? newAttr.value !== attr.value : true;
161
+ }) || newAttributes.some((attr) => !oldAttributes.some((oldAttr) => oldAttr.key === attr.key)) || customAttributesNeedRemove.some(
162
+ (removeAttr) => oldAttributes.some((oldAttr) => oldAttr.key === removeAttr.key)
163
+ );
164
+ };
165
165
  var containsAll = (source, requiredItems = []) => {
166
166
  if (!requiredItems?.length) return true;
167
167
  const sourceSet = new Set(source);
@@ -450,6 +450,14 @@ var useScriptAutoFreeGift = ({
450
450
  set_points_subscribe(!!isPointsSubscribe);
451
451
  }
452
452
  }, [locale]);
453
+ const isActivityAvailable = react.useMemo(() => {
454
+ if (!campaign) return false;
455
+ const query = getQuery();
456
+ const utmCampaign = Cookies5__default.default.get("utm_campaign") || query?.utm_campaign;
457
+ if (campaign.activityAvailableQuery && !utmCampaign?.includes(campaign.activityAvailableQuery))
458
+ return false;
459
+ return true;
460
+ }, [campaign]);
453
461
  const [upgrade_multiple, upgrade_value] = react.useMemo(() => {
454
462
  let upgrade_multiple2 = 1;
455
463
  let upgrade_value2 = 0;
@@ -466,8 +474,8 @@ var useScriptAutoFreeGift = ({
466
474
  return [upgrade_multiple2, upgrade_value2];
467
475
  }, [cart?.lineItems, points_subscribe]);
468
476
  const breakpoints = react.useMemo(() => {
469
- if (!campaign) return [];
470
- return (campaign.breakpoints || []).map((item) => ({
477
+ if (!isActivityAvailable) return [];
478
+ return (campaign?.breakpoints || []).map((item) => ({
471
479
  breakpoint: new Decimal2__default.default(item.breakpoint).minus(new Decimal2__default.default(upgrade_value)).dividedBy(new Decimal2__default.default(upgrade_multiple)).toFixed(2, Decimal2__default.default.ROUND_DOWN),
472
480
  giveawayProducts: item.giveawayProducts || []
473
481
  }));
@@ -491,25 +499,26 @@ var useScriptAutoFreeGift = ({
491
499
  return true;
492
500
  }, [giftHandles]);
493
501
  const involvedLines = react.useMemo(() => {
494
- if (!campaign) return [];
502
+ if (!isActivityAvailable) return [];
495
503
  return (cart?.lineItems || []).filter((line) => {
496
504
  const isNotGift = line?.totalAmount && Number(line.totalAmount) > 0 && line.customAttributes?.every(
497
505
  (item) => item.key !== _giveaway
498
506
  );
499
507
  const hasCampaignTag = line.product?.tags?.some(
500
- (tag) => campaign.includeTags?.includes(tag.trim()) && line.variant?.availableForSale
508
+ (tag) => campaign?.includeTags?.includes(tag.trim()) && line.variant?.availableForSale
501
509
  );
502
510
  return isNotGift && hasCampaignTag;
503
511
  });
504
- }, [cart?.lineItems, campaign, _giveaway]);
512
+ }, [cart?.lineItems, isActivityAvailable, _giveaway]);
505
513
  const involvedSubTotal = react.useMemo(() => {
506
- if (!campaign) return new Decimal2__default.default(0);
514
+ if (!isActivityAvailable) return new Decimal2__default.default(0);
507
515
  return involvedLines.reduce((prev, item) => {
508
- const amount = campaign.useTotalAmount ? item.totalAmount : item.subtotalAmount;
516
+ const amount = campaign?.useTotalAmount ? item.totalAmount : item.subtotalAmount;
509
517
  return new Decimal2__default.default(prev).plus(new Decimal2__default.default(amount || 0));
510
518
  }, new Decimal2__default.default(0));
511
- }, [involvedLines, campaign]);
519
+ }, [involvedLines, isActivityAvailable]);
512
520
  const [freeGiftLevel, nextFreeGiftLevel] = react.useMemo(() => {
521
+ if (!isActivityAvailable) return [null, null];
513
522
  const sortedLevels = [...breakpoints].sort(
514
523
  (a, b) => Number(b.breakpoint) - Number(a.breakpoint)
515
524
  );
@@ -933,6 +942,9 @@ function CartProvider({
933
942
  }) {
934
943
  const { client, cartCookieAdapter } = useShopify();
935
944
  const [customAttributes, setCustomAttributes] = react.useState([]);
945
+ const [customAttributesNeedDelete, setCustomAttributesNeedDelete] = react.useState(
946
+ []
947
+ );
936
948
  const [isCodeChanging, setIsCodeChanging] = react.useState(false);
937
949
  const [loadingState, setLoadingState] = react.useState({
938
950
  editLineQuantityLoading: false,
@@ -963,7 +975,11 @@ function CartProvider({
963
975
  ahooks.useRequest(
964
976
  () => {
965
977
  const newAttributes = [...attributes, ...customAttributes];
966
- const needUpdate = cart && !isAttributesEqual(cart.customAttributes, newAttributes);
978
+ const needUpdate = cart && !checkAttributesUpdateNeeded(
979
+ cart.customAttributes,
980
+ newAttributes,
981
+ customAttributesNeedDelete
982
+ );
967
983
  if (needUpdate) {
968
984
  return updateAttributes({ attributes: newAttributes });
969
985
  } else {
@@ -984,11 +1000,12 @@ function CartProvider({
984
1000
  isCartLoading: isCartLoading || isCodeChanging,
985
1001
  setLoadingState
986
1002
  });
987
- const removeCustomAttributes = react.useCallback((attributes2) => {
988
- setCustomAttributes(
989
- (prev) => prev.filter((attr) => !attributes2.some((a) => a.key === attr.key))
990
- );
991
- }, []);
1003
+ const removeCustomAttributes = react.useCallback(
1004
+ (attributes2) => {
1005
+ setCustomAttributesNeedDelete(attributes2);
1006
+ },
1007
+ [setCustomAttributesNeedDelete]
1008
+ );
992
1009
  const addCustomAttributes = react.useCallback(
993
1010
  (attributes2) => {
994
1011
  const sameAttributes = attributes2.filter(