@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.
@@ -60,15 +60,10 @@ interface CreateCartInput {
60
60
  declare function useCreateCart(options?: SWRMutationConfiguration<NormalizedCart | undefined, Error, 'create-cart', CreateCartInput>): swr_mutation.SWRMutationResponse<NormalizedCart | undefined, Error, "create-cart", CreateCartInput>;
61
61
 
62
62
  interface AddCartLinesInput {
63
- /** Cart ID (optional, will use cookie or create new cart) */
64
- cartId?: string;
63
+ /** Cart ID (required) */
64
+ cartId: string;
65
65
  /** Lines to add */
66
66
  lines: CartLineInput[];
67
- /** Buyer identity for new cart */
68
- buyerIdentity?: {
69
- email?: string;
70
- countryCode?: string;
71
- };
72
67
  }
73
68
  /**
74
69
  * Hook for adding lines to cart
@@ -60,15 +60,10 @@ interface CreateCartInput {
60
60
  declare function useCreateCart(options?: SWRMutationConfiguration<NormalizedCart | undefined, Error, 'create-cart', CreateCartInput>): swr_mutation.SWRMutationResponse<NormalizedCart | undefined, Error, "create-cart", CreateCartInput>;
61
61
 
62
62
  interface AddCartLinesInput {
63
- /** Cart ID (optional, will use cookie or create new cart) */
64
- cartId?: string;
63
+ /** Cart ID (required) */
64
+ cartId: string;
65
65
  /** Lines to add */
66
66
  lines: CartLineInput[];
67
- /** Buyer identity for new cart */
68
- buyerIdentity?: {
69
- email?: string;
70
- countryCode?: string;
71
- };
72
67
  }
73
68
  /**
74
69
  * Hook for adding lines to cart
@@ -999,6 +999,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
999
999
  const { trigger: applyCartCodes } = useApplyCartCodes();
1000
1000
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
1001
1001
  const { trigger: addCartLines2 } = useAddCartLines();
1002
+ const { trigger: createCart3 } = useCreateCart();
1002
1003
  const addToCart = react.useCallback(
1003
1004
  async (_key, { arg }) => {
1004
1005
  const {
@@ -1030,40 +1031,45 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1030
1031
  if (lines.length === 0) {
1031
1032
  return;
1032
1033
  }
1033
- const cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
1034
- let resultCart = await addCartLines2({
1035
- cartId,
1036
- lines,
1037
- buyerIdentity
1038
- });
1039
- if (!resultCart) {
1040
- performanceAdapter?.addToCartEnd();
1041
- return void 0;
1042
- }
1043
- console.log("npm addCartLines resultCart", resultCart);
1044
- if (resultCart.discountCodes && resultCart.discountCodes.length > 0) {
1045
- const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
1046
- if (unapplicableCodes.length > 0) {
1047
- if (onCodesInvalid) {
1048
- const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
1049
- if (handledCart) {
1050
- resultCart = handledCart;
1034
+ let cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
1035
+ let resultCart = null;
1036
+ if (!cartId) {
1037
+ resultCart = await createCart3({
1038
+ lines,
1039
+ buyerIdentity,
1040
+ discountCodes,
1041
+ customAttributes
1042
+ });
1043
+ } else {
1044
+ resultCart = await addCartLines2({
1045
+ cartId,
1046
+ lines
1047
+ });
1048
+ console.log("npm addCartLines resultCart", resultCart);
1049
+ if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
1050
+ const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
1051
+ if (unapplicableCodes.length > 0) {
1052
+ if (onCodesInvalid) {
1053
+ const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
1054
+ if (handledCart) {
1055
+ resultCart = handledCart;
1056
+ }
1057
+ } else {
1058
+ await removeInvalidCodes({
1059
+ discountCodes: unapplicableCodes
1060
+ });
1051
1061
  }
1052
- } else {
1053
- await removeInvalidCodes({
1054
- discountCodes: unapplicableCodes
1055
- });
1056
1062
  }
1057
1063
  }
1058
- }
1059
- if (discountCodes && discountCodes.length > 0) {
1060
- applyCartCodes({
1061
- replaceExistingCodes,
1062
- discountCodes
1063
- });
1064
- }
1065
- if (customAttributes && customAttributes.length > 0) {
1066
- addCustomAttributes(customAttributes);
1064
+ if (resultCart && discountCodes && discountCodes.length > 0) {
1065
+ applyCartCodes({
1066
+ replaceExistingCodes,
1067
+ discountCodes
1068
+ });
1069
+ }
1070
+ if (customAttributes && customAttributes.length > 0) {
1071
+ addCustomAttributes(customAttributes);
1072
+ }
1067
1073
  }
1068
1074
  if (withTrack) {
1069
1075
  trackAddToCartGA({
@@ -1075,7 +1081,21 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1075
1081
  performanceAdapter?.addToCartEnd();
1076
1082
  return resultCart;
1077
1083
  },
1078
- [client, locale, cartCookieAdapter, userAdapter, cart, withTrack, performanceAdapter]
1084
+ [
1085
+ client,
1086
+ locale,
1087
+ cartCookieAdapter,
1088
+ userAdapter,
1089
+ cart,
1090
+ withTrack,
1091
+ performanceAdapter,
1092
+ createCart3,
1093
+ addCartLines2,
1094
+ applyCartCodes,
1095
+ removeInvalidCodes,
1096
+ addCustomAttributes,
1097
+ config
1098
+ ]
1079
1099
  );
1080
1100
  return useSWRMutation__default.default("add-to-cart", addToCart, swrOptions);
1081
1101
  }