@anker-in/shopify-react 0.1.1-beta.0 → 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.
@@ -134,25 +134,25 @@ var getMatchedMainProductSubTotal = (cartData, variant_list, main_product) => {
134
134
  return acc + (main_product?.spend_money_type === 1 /* ORIGIN_PRICE */ ? Number(line.subtotalAmount) || 0 : Number(line.totalAmount) || 0);
135
135
  }, 0) || 0;
136
136
  };
137
- var getDiscountEnvAttributeValue = (attributes = []) => {
138
- const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
139
- return safeParseJson(attr?.value ?? "") ?? {};
140
- };
141
- var isAttributesEqual = (attrs1 = [], attrs2 = []) => {
142
- if (attrs1.length !== attrs2.length) return false;
143
- const sorted1 = [...attrs1].sort((a, b) => a.key.localeCompare(b.key));
144
- const sorted2 = [...attrs2].sort((a, b) => a.key.localeCompare(b.key));
145
- return sorted1.every(
146
- (attr, i) => attr.key === sorted2[i]?.key && attr.value === sorted2[i]?.value
147
- );
148
- };
149
- var safeParseJson = (str) => {
137
+ var safeParse = (str) => {
150
138
  try {
151
139
  return JSON.parse(str);
152
140
  } catch (err) {
153
141
  return {};
154
142
  }
155
143
  };
144
+ var getDiscountEnvAttributeValue = (attributes = []) => {
145
+ const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
146
+ return safeParse(attr?.value ?? "") ?? {};
147
+ };
148
+ var checkAttributesUpdateNeeded = (oldAttributes, newAttributes, customAttributesNeedRemove) => {
149
+ return oldAttributes.some((attr) => {
150
+ const newAttr = newAttributes.find((newAttr2) => newAttr2.key === attr.key);
151
+ return newAttr ? newAttr.value !== attr.value : true;
152
+ }) || newAttributes.some((attr) => !oldAttributes.some((oldAttr) => oldAttr.key === attr.key)) || customAttributesNeedRemove.some(
153
+ (removeAttr) => oldAttributes.some((oldAttr) => oldAttr.key === removeAttr.key)
154
+ );
155
+ };
156
156
  var containsAll = (source, requiredItems = []) => {
157
157
  if (!requiredItems?.length) return true;
158
158
  const sourceSet = new Set(source);
@@ -441,6 +441,14 @@ var useScriptAutoFreeGift = ({
441
441
  set_points_subscribe(!!isPointsSubscribe);
442
442
  }
443
443
  }, [locale]);
444
+ const isActivityAvailable = useMemo(() => {
445
+ if (!campaign) return false;
446
+ const query = getQuery();
447
+ const utmCampaign = Cookies5.get("utm_campaign") || query?.utm_campaign;
448
+ if (campaign.activityAvailableQuery && !utmCampaign?.includes(campaign.activityAvailableQuery))
449
+ return false;
450
+ return true;
451
+ }, [campaign]);
444
452
  const [upgrade_multiple, upgrade_value] = useMemo(() => {
445
453
  let upgrade_multiple2 = 1;
446
454
  let upgrade_value2 = 0;
@@ -457,8 +465,8 @@ var useScriptAutoFreeGift = ({
457
465
  return [upgrade_multiple2, upgrade_value2];
458
466
  }, [cart?.lineItems, points_subscribe]);
459
467
  const breakpoints = useMemo(() => {
460
- if (!campaign) return [];
461
- return (campaign.breakpoints || []).map((item) => ({
468
+ if (!isActivityAvailable) return [];
469
+ return (campaign?.breakpoints || []).map((item) => ({
462
470
  breakpoint: new Decimal2(item.breakpoint).minus(new Decimal2(upgrade_value)).dividedBy(new Decimal2(upgrade_multiple)).toFixed(2, Decimal2.ROUND_DOWN),
463
471
  giveawayProducts: item.giveawayProducts || []
464
472
  }));
@@ -482,25 +490,26 @@ var useScriptAutoFreeGift = ({
482
490
  return true;
483
491
  }, [giftHandles]);
484
492
  const involvedLines = useMemo(() => {
485
- if (!campaign) return [];
493
+ if (!isActivityAvailable) return [];
486
494
  return (cart?.lineItems || []).filter((line) => {
487
495
  const isNotGift = line?.totalAmount && Number(line.totalAmount) > 0 && line.customAttributes?.every(
488
496
  (item) => item.key !== _giveaway
489
497
  );
490
498
  const hasCampaignTag = line.product?.tags?.some(
491
- (tag) => campaign.includeTags?.includes(tag.trim()) && line.variant?.availableForSale
499
+ (tag) => campaign?.includeTags?.includes(tag.trim()) && line.variant?.availableForSale
492
500
  );
493
501
  return isNotGift && hasCampaignTag;
494
502
  });
495
- }, [cart?.lineItems, campaign, _giveaway]);
503
+ }, [cart?.lineItems, isActivityAvailable, _giveaway]);
496
504
  const involvedSubTotal = useMemo(() => {
497
- if (!campaign) return new Decimal2(0);
505
+ if (!isActivityAvailable) return new Decimal2(0);
498
506
  return involvedLines.reduce((prev, item) => {
499
- const amount = campaign.useTotalAmount ? item.totalAmount : item.subtotalAmount;
507
+ const amount = campaign?.useTotalAmount ? item.totalAmount : item.subtotalAmount;
500
508
  return new Decimal2(prev).plus(new Decimal2(amount || 0));
501
509
  }, new Decimal2(0));
502
- }, [involvedLines, campaign]);
510
+ }, [involvedLines, isActivityAvailable]);
503
511
  const [freeGiftLevel, nextFreeGiftLevel] = useMemo(() => {
512
+ if (!isActivityAvailable) return [null, null];
504
513
  const sortedLevels = [...breakpoints].sort(
505
514
  (a, b) => Number(b.breakpoint) - Number(a.breakpoint)
506
515
  );
@@ -924,6 +933,9 @@ function CartProvider({
924
933
  }) {
925
934
  const { client, cartCookieAdapter } = useShopify();
926
935
  const [customAttributes, setCustomAttributes] = useState([]);
936
+ const [customAttributesNeedDelete, setCustomAttributesNeedDelete] = useState(
937
+ []
938
+ );
927
939
  const [isCodeChanging, setIsCodeChanging] = useState(false);
928
940
  const [loadingState, setLoadingState] = useState({
929
941
  editLineQuantityLoading: false,
@@ -954,7 +966,11 @@ function CartProvider({
954
966
  useRequest(
955
967
  () => {
956
968
  const newAttributes = [...attributes, ...customAttributes];
957
- const needUpdate = cart && !isAttributesEqual(cart.customAttributes, newAttributes);
969
+ const needUpdate = cart && !checkAttributesUpdateNeeded(
970
+ cart.customAttributes,
971
+ newAttributes,
972
+ customAttributesNeedDelete
973
+ );
958
974
  if (needUpdate) {
959
975
  return updateAttributes({ attributes: newAttributes });
960
976
  } else {
@@ -975,11 +991,12 @@ function CartProvider({
975
991
  isCartLoading: isCartLoading || isCodeChanging,
976
992
  setLoadingState
977
993
  });
978
- const removeCustomAttributes = useCallback((attributes2) => {
979
- setCustomAttributes(
980
- (prev) => prev.filter((attr) => !attributes2.some((a) => a.key === attr.key))
981
- );
982
- }, []);
994
+ const removeCustomAttributes = useCallback(
995
+ (attributes2) => {
996
+ setCustomAttributesNeedDelete(attributes2);
997
+ },
998
+ [setCustomAttributesNeedDelete]
999
+ );
983
1000
  const addCustomAttributes = useCallback(
984
1001
  (attributes2) => {
985
1002
  const sameAttributes = attributes2.filter(