@anker-in/shopify-react 0.1.1-beta.48 → 0.1.1-beta.49

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.
@@ -216,14 +216,6 @@ var getDiscountEnvAttributeValue = (attributes = []) => {
216
216
  const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
217
217
  return safeParse(attr?.value ?? "") ?? {};
218
218
  };
219
- var checkAttributesUpdateNeeded = (oldAttributes, newAttributes, customAttributesNeedRemove) => {
220
- return oldAttributes.some((attr) => {
221
- const newAttr = newAttributes.find((newAttr2) => newAttr2.key === attr.key);
222
- return newAttr ? newAttr.value !== attr.value : true;
223
- }) || newAttributes.some((attr) => !oldAttributes.some((oldAttr) => oldAttr.key === attr.key)) || customAttributesNeedRemove.some(
224
- (removeAttr) => oldAttributes.some((oldAttr) => oldAttr.key === removeAttr.key)
225
- );
226
- };
227
219
  var containsAll = (source, requiredItems = []) => {
228
220
  if (!requiredItems?.length) return true;
229
221
  const sourceSet = new Set(source);
@@ -704,6 +696,16 @@ var useScriptAutoFreeGift = ({
704
696
  giftProductsResult: finalGiftProductsResult
705
697
  };
706
698
  };
699
+
700
+ // src/hooks/cart/utils/cart-attributes.ts
701
+ var checkAttributesUpdateNeeded = (oldAttributes, newAttributes, customAttributesNeedRemove) => {
702
+ return newAttributes.some((attr) => !oldAttributes.some((oldAttr) => oldAttr.key === attr.key)) || oldAttributes.some((attr) => {
703
+ const newAttr = newAttributes.find((newAttr2) => newAttr2.key === attr.key);
704
+ return newAttr ? newAttr.value !== attr.value : true;
705
+ }) || customAttributesNeedRemove.some(
706
+ (removeAttr) => oldAttributes.some((oldAttr) => oldAttr.key === removeAttr.key)
707
+ );
708
+ };
707
709
  var getReferralAttributes = () => {
708
710
  const inviteCode = getLocalStorage("inviteCode") || Cookies5.get("inviteCode");
709
711
  const playModeId = getLocalStorage("playModeId") || Cookies5.get("playModeId");
@@ -1070,7 +1072,7 @@ function CartProvider({
1070
1072
  }) {
1071
1073
  const { client, cartCookieAdapter } = useShopify();
1072
1074
  const [customAttributes, setCustomAttributes] = useState([]);
1073
- const [customAttributesNeedDelete, setCustomAttributesNeedDelete] = useState(
1075
+ const [customAttributesNeedRemove, setCustomAttributesNeedRemove] = useState(
1074
1076
  []
1075
1077
  );
1076
1078
  const [isCodeChanging, setIsCodeChanging] = useState(false);
@@ -1103,12 +1105,11 @@ function CartProvider({
1103
1105
  metafieldIdentifiers,
1104
1106
  disabled: isCartLoading
1105
1107
  });
1106
- console.log("isCartLoading", isCartLoading);
1107
1108
  const { hasPlusMember } = useHasPlusMemberInCart({
1108
1109
  memberSetting,
1109
1110
  cart
1110
1111
  });
1111
- const { attributes } = useCartAttributes({
1112
+ const { attributes: commonAttributes } = useCartAttributes({
1112
1113
  profile,
1113
1114
  customer,
1114
1115
  cart,
@@ -1116,16 +1117,17 @@ function CartProvider({
1116
1117
  });
1117
1118
  useRequest(
1118
1119
  () => {
1119
- const newAttributes = [...attributes];
1120
- customAttributes.forEach((item) => {
1121
- if (item.value && !newAttributes.some((attr) => attr.key === item.key)) {
1122
- newAttributes.push(item);
1123
- }
1124
- });
1120
+ const filteredSameCommonAttributes = commonAttributes.filter(
1121
+ (attr) => !customAttributes.some((a) => a.key === attr.key)
1122
+ );
1123
+ const newAttributes = [
1124
+ ...filteredSameCommonAttributes,
1125
+ ...customAttributes
1126
+ ];
1125
1127
  const needUpdate = cart && checkAttributesUpdateNeeded(
1126
1128
  cart.customAttributes,
1127
1129
  newAttributes,
1128
- customAttributesNeedDelete
1130
+ customAttributesNeedRemove
1129
1131
  );
1130
1132
  if (needUpdate) {
1131
1133
  return updateAttributes({ attributes: newAttributes });
@@ -1138,7 +1140,7 @@ function CartProvider({
1138
1140
  // 1 秒内只触发最后一次更新
1139
1141
  throttleTrailing: true,
1140
1142
  throttleLeading: false,
1141
- refreshDeps: [attributes, customAttributes]
1143
+ refreshDeps: [commonAttributes, customAttributes, customAttributesNeedRemove]
1142
1144
  }
1143
1145
  );
1144
1146
  useUpdateLineCodeAmountAttributes({
@@ -1149,28 +1151,21 @@ function CartProvider({
1149
1151
  metafieldIdentifiers
1150
1152
  });
1151
1153
  const removeCustomAttributes = useCallback(
1152
- (attributes2) => {
1153
- setCustomAttributesNeedDelete(attributes2);
1154
+ (attributes) => {
1155
+ setCustomAttributesNeedRemove(attributes);
1154
1156
  },
1155
- [setCustomAttributesNeedDelete]
1157
+ [setCustomAttributesNeedRemove]
1156
1158
  );
1157
1159
  const addCustomAttributes = useCallback(
1158
- (attributes2) => {
1159
- const sameAttributes = attributes2.filter(
1160
- (attr) => customAttributes.some((a) => a.key === attr.key)
1161
- );
1162
- if (sameAttributes.length) {
1163
- setCustomAttributes((prev) => {
1164
- const removedAttributes = prev.filter(
1165
- (attr) => !sameAttributes.some((a) => a.key === attr.key)
1166
- );
1167
- return [...removedAttributes, ...attributes2];
1168
- });
1169
- } else {
1170
- setCustomAttributes((prev) => [...prev, ...attributes2]);
1171
- }
1160
+ (attributes) => {
1161
+ setCustomAttributes((oldCustomAttributes) => {
1162
+ const filteredSameAttributes = oldCustomAttributes.filter(
1163
+ (attr) => !attributes.some((a) => a.key === attr.key)
1164
+ );
1165
+ return [...filteredSameAttributes, ...attributes];
1166
+ });
1172
1167
  },
1173
- [customAttributes]
1168
+ [setCustomAttributes]
1174
1169
  );
1175
1170
  const functionAutoFreeGiftResult = useCalcAutoFreeGift(cart, autoFreeGiftConfig || [], customer);
1176
1171
  const scriptAutoFreeGiftResult = useScriptAutoFreeGift({
@@ -1242,7 +1237,6 @@ function CartProvider({
1242
1237
  mutateCart,
1243
1238
  addCustomAttributes,
1244
1239
  removeCustomAttributes,
1245
- setCustomAttributes,
1246
1240
  locale,
1247
1241
  profile,
1248
1242
  customer,