@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.
@@ -65,8 +65,6 @@ interface CartContextValue {
65
65
  removeCustomAttributes: (attributes: {
66
66
  key: string;
67
67
  }[]) => void;
68
- /** Set custom attributes */
69
- setCustomAttributes: (attributes: AttributeInput[]) => void;
70
68
  /** Current locale */
71
69
  locale: string;
72
70
  /** Whether discount code is changing */
@@ -65,8 +65,6 @@ interface CartContextValue {
65
65
  removeCustomAttributes: (attributes: {
66
66
  key: string;
67
67
  }[]) => void;
68
- /** Set custom attributes */
69
- setCustomAttributes: (attributes: AttributeInput[]) => void;
70
68
  /** Current locale */
71
69
  locale: string;
72
70
  /** Whether discount code is changing */
@@ -225,14 +225,6 @@ var getDiscountEnvAttributeValue = (attributes = []) => {
225
225
  const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
226
226
  return safeParse(attr?.value ?? "") ?? {};
227
227
  };
228
- var checkAttributesUpdateNeeded = (oldAttributes, newAttributes, customAttributesNeedRemove) => {
229
- return oldAttributes.some((attr) => {
230
- const newAttr = newAttributes.find((newAttr2) => newAttr2.key === attr.key);
231
- return newAttr ? newAttr.value !== attr.value : true;
232
- }) || newAttributes.some((attr) => !oldAttributes.some((oldAttr) => oldAttr.key === attr.key)) || customAttributesNeedRemove.some(
233
- (removeAttr) => oldAttributes.some((oldAttr) => oldAttr.key === removeAttr.key)
234
- );
235
- };
236
228
  var containsAll = (source, requiredItems = []) => {
237
229
  if (!requiredItems?.length) return true;
238
230
  const sourceSet = new Set(source);
@@ -713,6 +705,16 @@ var useScriptAutoFreeGift = ({
713
705
  giftProductsResult: finalGiftProductsResult
714
706
  };
715
707
  };
708
+
709
+ // src/hooks/cart/utils/cart-attributes.ts
710
+ var checkAttributesUpdateNeeded = (oldAttributes, newAttributes, customAttributesNeedRemove) => {
711
+ return newAttributes.some((attr) => !oldAttributes.some((oldAttr) => oldAttr.key === attr.key)) || oldAttributes.some((attr) => {
712
+ const newAttr = newAttributes.find((newAttr2) => newAttr2.key === attr.key);
713
+ return newAttr ? newAttr.value !== attr.value : true;
714
+ }) || customAttributesNeedRemove.some(
715
+ (removeAttr) => oldAttributes.some((oldAttr) => oldAttr.key === removeAttr.key)
716
+ );
717
+ };
716
718
  var getReferralAttributes = () => {
717
719
  const inviteCode = shopifySdk.getLocalStorage("inviteCode") || Cookies5__default.default.get("inviteCode");
718
720
  const playModeId = shopifySdk.getLocalStorage("playModeId") || Cookies5__default.default.get("playModeId");
@@ -1079,7 +1081,7 @@ function CartProvider({
1079
1081
  }) {
1080
1082
  const { client, cartCookieAdapter } = useShopify();
1081
1083
  const [customAttributes, setCustomAttributes] = react.useState([]);
1082
- const [customAttributesNeedDelete, setCustomAttributesNeedDelete] = react.useState(
1084
+ const [customAttributesNeedRemove, setCustomAttributesNeedRemove] = react.useState(
1083
1085
  []
1084
1086
  );
1085
1087
  const [isCodeChanging, setIsCodeChanging] = react.useState(false);
@@ -1112,12 +1114,11 @@ function CartProvider({
1112
1114
  metafieldIdentifiers,
1113
1115
  disabled: isCartLoading
1114
1116
  });
1115
- console.log("isCartLoading", isCartLoading);
1116
1117
  const { hasPlusMember } = useHasPlusMemberInCart({
1117
1118
  memberSetting,
1118
1119
  cart
1119
1120
  });
1120
- const { attributes } = useCartAttributes({
1121
+ const { attributes: commonAttributes } = useCartAttributes({
1121
1122
  profile,
1122
1123
  customer,
1123
1124
  cart,
@@ -1125,16 +1126,17 @@ function CartProvider({
1125
1126
  });
1126
1127
  ahooks.useRequest(
1127
1128
  () => {
1128
- const newAttributes = [...attributes];
1129
- customAttributes.forEach((item) => {
1130
- if (item.value && !newAttributes.some((attr) => attr.key === item.key)) {
1131
- newAttributes.push(item);
1132
- }
1133
- });
1129
+ const filteredSameCommonAttributes = commonAttributes.filter(
1130
+ (attr) => !customAttributes.some((a) => a.key === attr.key)
1131
+ );
1132
+ const newAttributes = [
1133
+ ...filteredSameCommonAttributes,
1134
+ ...customAttributes
1135
+ ];
1134
1136
  const needUpdate = cart && checkAttributesUpdateNeeded(
1135
1137
  cart.customAttributes,
1136
1138
  newAttributes,
1137
- customAttributesNeedDelete
1139
+ customAttributesNeedRemove
1138
1140
  );
1139
1141
  if (needUpdate) {
1140
1142
  return updateAttributes({ attributes: newAttributes });
@@ -1147,7 +1149,7 @@ function CartProvider({
1147
1149
  // 1 秒内只触发最后一次更新
1148
1150
  throttleTrailing: true,
1149
1151
  throttleLeading: false,
1150
- refreshDeps: [attributes, customAttributes]
1152
+ refreshDeps: [commonAttributes, customAttributes, customAttributesNeedRemove]
1151
1153
  }
1152
1154
  );
1153
1155
  useUpdateLineCodeAmountAttributes({
@@ -1158,28 +1160,21 @@ function CartProvider({
1158
1160
  metafieldIdentifiers
1159
1161
  });
1160
1162
  const removeCustomAttributes = react.useCallback(
1161
- (attributes2) => {
1162
- setCustomAttributesNeedDelete(attributes2);
1163
+ (attributes) => {
1164
+ setCustomAttributesNeedRemove(attributes);
1163
1165
  },
1164
- [setCustomAttributesNeedDelete]
1166
+ [setCustomAttributesNeedRemove]
1165
1167
  );
1166
1168
  const addCustomAttributes = react.useCallback(
1167
- (attributes2) => {
1168
- const sameAttributes = attributes2.filter(
1169
- (attr) => customAttributes.some((a) => a.key === attr.key)
1170
- );
1171
- if (sameAttributes.length) {
1172
- setCustomAttributes((prev) => {
1173
- const removedAttributes = prev.filter(
1174
- (attr) => !sameAttributes.some((a) => a.key === attr.key)
1175
- );
1176
- return [...removedAttributes, ...attributes2];
1177
- });
1178
- } else {
1179
- setCustomAttributes((prev) => [...prev, ...attributes2]);
1180
- }
1169
+ (attributes) => {
1170
+ setCustomAttributes((oldCustomAttributes) => {
1171
+ const filteredSameAttributes = oldCustomAttributes.filter(
1172
+ (attr) => !attributes.some((a) => a.key === attr.key)
1173
+ );
1174
+ return [...filteredSameAttributes, ...attributes];
1175
+ });
1181
1176
  },
1182
- [customAttributes]
1177
+ [setCustomAttributes]
1183
1178
  );
1184
1179
  const functionAutoFreeGiftResult = useCalcAutoFreeGift(cart, autoFreeGiftConfig || [], customer);
1185
1180
  const scriptAutoFreeGiftResult = useScriptAutoFreeGift({
@@ -1251,7 +1246,6 @@ function CartProvider({
1251
1246
  mutateCart,
1252
1247
  addCustomAttributes,
1253
1248
  removeCustomAttributes,
1254
- setCustomAttributes,
1255
1249
  locale,
1256
1250
  profile,
1257
1251
  customer,