@anker-in/shopify-react 0.1.1-beta.38 → 0.1.1-beta.39

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.
package/dist/index.js CHANGED
@@ -1112,6 +1112,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1112
1112
  const { trigger: applyCartCodes } = useApplyCartCodes();
1113
1113
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
1114
1114
  const { trigger: addCartLines2 } = useAddCartLines();
1115
+ const { trigger: createCart3 } = useCreateCart();
1115
1116
  const addToCart = react.useCallback(
1116
1117
  async (_key, { arg }) => {
1117
1118
  const {
@@ -1143,40 +1144,45 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1143
1144
  if (lines.length === 0) {
1144
1145
  return;
1145
1146
  }
1146
- const cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
1147
- let resultCart = await addCartLines2({
1148
- cartId,
1149
- lines,
1150
- buyerIdentity
1151
- });
1152
- if (!resultCart) {
1153
- performanceAdapter?.addToCartEnd();
1154
- return void 0;
1155
- }
1156
- console.log("npm addCartLines resultCart", resultCart);
1157
- if (resultCart.discountCodes && resultCart.discountCodes.length > 0) {
1158
- const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
1159
- if (unapplicableCodes.length > 0) {
1160
- if (onCodesInvalid) {
1161
- const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
1162
- if (handledCart) {
1163
- resultCart = handledCart;
1147
+ let cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
1148
+ let resultCart = null;
1149
+ if (!cartId) {
1150
+ resultCart = await createCart3({
1151
+ lines,
1152
+ buyerIdentity,
1153
+ discountCodes,
1154
+ customAttributes
1155
+ });
1156
+ } else {
1157
+ resultCart = await addCartLines2({
1158
+ cartId,
1159
+ lines
1160
+ });
1161
+ console.log("npm addCartLines resultCart", resultCart);
1162
+ if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
1163
+ const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
1164
+ if (unapplicableCodes.length > 0) {
1165
+ if (onCodesInvalid) {
1166
+ const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
1167
+ if (handledCart) {
1168
+ resultCart = handledCart;
1169
+ }
1170
+ } else {
1171
+ await removeInvalidCodes({
1172
+ discountCodes: unapplicableCodes
1173
+ });
1164
1174
  }
1165
- } else {
1166
- await removeInvalidCodes({
1167
- discountCodes: unapplicableCodes
1168
- });
1169
1175
  }
1170
1176
  }
1171
- }
1172
- if (discountCodes && discountCodes.length > 0) {
1173
- applyCartCodes({
1174
- replaceExistingCodes,
1175
- discountCodes
1176
- });
1177
- }
1178
- if (customAttributes && customAttributes.length > 0) {
1179
- addCustomAttributes(customAttributes);
1177
+ if (resultCart && discountCodes && discountCodes.length > 0) {
1178
+ applyCartCodes({
1179
+ replaceExistingCodes,
1180
+ discountCodes
1181
+ });
1182
+ }
1183
+ if (customAttributes && customAttributes.length > 0) {
1184
+ addCustomAttributes(customAttributes);
1185
+ }
1180
1186
  }
1181
1187
  if (withTrack) {
1182
1188
  trackAddToCartGA({
@@ -1188,7 +1194,21 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1188
1194
  performanceAdapter?.addToCartEnd();
1189
1195
  return resultCart;
1190
1196
  },
1191
- [client, locale, cartCookieAdapter, userAdapter, cart, withTrack, performanceAdapter]
1197
+ [
1198
+ client,
1199
+ locale,
1200
+ cartCookieAdapter,
1201
+ userAdapter,
1202
+ cart,
1203
+ withTrack,
1204
+ performanceAdapter,
1205
+ createCart3,
1206
+ addCartLines2,
1207
+ applyCartCodes,
1208
+ removeInvalidCodes,
1209
+ addCustomAttributes,
1210
+ config
1211
+ ]
1192
1212
  );
1193
1213
  return useSWRMutation__default.default("add-to-cart", addToCart, swrOptions);
1194
1214
  }