Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Error about add cart (License & Custom Products) #323

Merged
merged 12 commits into from
Dec 2, 2023
Merged
153 changes: 99 additions & 54 deletions src/axios/cart/Cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,69 @@ import { ACCESS_TOKEN, GUEST_COOKIE } from '../../constants/token';
import { CONTENT_TYPE } from '../../constants/header';
import { BACK, CART } from '../../constants/path';
import {
ADD_CART,
ADD_CART_GUEST,
CART_LIST,
DELETE_CART,
DELETE_CART_GUEST,
EDIT_CART,
EDIT_CART_GUEST,
PRODUCT_OPTION,
ADD_CART_CUSTOM,
ADD_CART_CUSTOM_GUEST,
ADD_CART_LICENSE,
CART_LIST,
DELETE_CART,
DELETE_CART_GUEST,
EDIT_CART,
EDIT_CART_GUEST, EDIT_CART_IMAGE,
PRODUCT_OPTION,
} from '../../constants/api';
import Swal from "sweetalert2";

export default function post(dto, file, isCustom) {
const formData = new FormData();
if (isCustom) formData.append('file', file);
formData.append('dto', new Blob([JSON.stringify(dto)], { type: CONTENT_TYPE.ApplicationJson }));
if (sessionStorage.getItem(ACCESS_TOKEN)) {
axios
.post(ADD_CART(), formData, {
headers: {
Authorization: sessionStorage.getItem(ACCESS_TOKEN),
"Content-Type": CONTENT_TYPE.MultipartFormData,
},
})
.then(() => {
alert('장바구니에 담겼습니다!');
window.location.replace(BACK);
});

function alertSuccess() {
Swal.fire({
title: '장바구니에 담겼습니다',
icon: 'success',
});
}

if (sessionStorage.getItem(ACCESS_TOKEN)) {
if (isCustom) {
formData.append('file', file);
axios
.post(ADD_CART_CUSTOM(), formData, {
headers: {
Authorization: sessionStorage.getItem(ACCESS_TOKEN),
"Content-Type": CONTENT_TYPE.MultipartFormData,
},
})
.then(() => {
alertSuccess();
});
}
if (!isCustom) {
axios
.post(ADD_CART_LICENSE(), formData, {
headers: {
Authorization: sessionStorage.getItem(ACCESS_TOKEN),
"Content-Type": CONTENT_TYPE.MultipartFormData,
},
})
.then(() => {
alertSuccess();
});
}
} else {
axios
.post(ADD_CART_GUEST, formData, {
.post(ADD_CART_CUSTOM_GUEST(), formData, {
headers: {
Authorization: cookie.load(GUEST_COOKIE),
'Content-Type': CONTENT_TYPE.MultipartFormData,
},
})
.then(() => {
alert('비회원 장바구니에 담겼습니다!');
window.location.replace(BACK);
});
.then(() => {
Swal.fire({
title: '비회원 장바구니에 담겼습니다',
icon: 'success',
});
});
}
}

Expand All @@ -53,12 +78,23 @@ export const fetchCartData = (token, setCartList, setEmptyMode, setProductOption
axios
.get(CART_LIST(), { headers })
.then((response) => {
setCartList(response.data);
const responseData = (response.data.map(item => {
const updatedLicense = item.license === null ? '' : item.license;
const updatedOptions = item.options === null ? '' : item.options;
return {
...item,
license: updatedLicense,
options: updatedOptions
};
}));
setCartList(responseData);
console.log(responseData);
if (!response.data || response.data === '') {
setEmptyMode(true);
} else {
axios.get(PRODUCT_OPTION(), { headers }).then((response) => {
setProductOption(response.data[0].productOptionList);
setProductOption(response.data);
console.log(response.data);
});
}
})
Expand Down Expand Up @@ -100,30 +136,39 @@ export const handleDeleteClick = (checkedList) => {
};

export const handleEditClick = (customProductId, dto, file) => {
const formData = new FormData();
formData.append('file', file);
formData.append('dto', new Blob([JSON.stringify(dto)], { type: CONTENT_TYPE.ApplicationJson }));
if (sessionStorage.getItem(ACCESS_TOKEN)) {
axios
.patch(EDIT_CART(customProductId), formData, {
headers: {
Authorization: sessionStorage.getItem(ACCESS_TOKEN),
'Content-Type': CONTENT_TYPE.MultipartFormData,
},
})
.then(() => {
window.location.replace(CART);
});
} else {
axios
.patch(EDIT_CART_GUEST(customProductId), formData, {
headers: {
Authorization: cookie.load(GUEST_COOKIE),
'Content-Type': CONTENT_TYPE.MultipartFormData,
},
})
.then(() => {
window.location.replace(CART);
});
}
console.log(dto, file)
const formData = new FormData();
const imageFormData = new FormData();
imageFormData.append('file[imageFile]', file);
formData.append('dto', new Blob([JSON.stringify(dto)], {type: CONTENT_TYPE.ApplicationJson}));
const accessToken = sessionStorage.getItem(ACCESS_TOKEN);
if (accessToken) {
const editCart = (url, data) => {
axios
.patch(url, data, {
headers: {
Authorization: accessToken,
'Content-Type': CONTENT_TYPE.MultipartFormData,
},
})
.then(() => {
window.location.replace(CART);
});
};
if (file !== null) {
editCart(EDIT_CART_IMAGE(customProductId), imageFormData);
}
editCart(EDIT_CART(customProductId), formData);
} else {
axios
.patch(EDIT_CART_GUEST(customProductId), formData, {
headers: {
Authorization: cookie.load(GUEST_COOKIE),
'Content-Type': CONTENT_TYPE.MultipartFormData,
},
})
.then(() => {
window.location.replace(CART);
});
}
};
2 changes: 1 addition & 1 deletion src/component/order/OrderOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default function OrderOptions({ productId, productInfo, price, setPrice }
switch (mode) {
case ORDER_MODE.CART:
if (!pass) break;
post(data, imageFile);
post(data, imageFile, productInfo.custom);
break;
case ORDER_MODE.BUY:
if (!pass) break;
Expand Down
11 changes: 7 additions & 4 deletions src/constants/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ export const DELETE_REVIEW = (reviewId) => `/product/reviews/${reviewId}`;
export const LICENSE_LIST = () => `/product/licenseImage`;

// cart api
export const ADD_CART = () => '/product/carts/custom-products';
export const ADD_CART_GUEST = () => '/product/guest/carts/custom-products';
export const ADD_CART_CUSTOM = () => '/product/carts/custom-products';
export const ADD_CART_LICENSE = () => '/product/carts/license-products';
export const ADD_CART_CUSTOM_GUEST = () => '/product/guest/carts/custom-products';
export const ADD_CART_LICENSE_GUEST = () => '/product/guest/carts/license-products';
export const CART_LIST = () => '/product/carts';
export const PRODUCT_OPTION = () => '/product/carts/productOptionInfo';
export const DELETE_CART = (id) => `/product/carts/custom-products/${id}`;
export const DELETE_CART_GUEST = (id) => `/product/guest/carts/custom-products/${id}`;
export const EDIT_CART = (id) => `/product/carts/customProducts/${id}`;
export const EDIT_CART_GUEST = (id) => `/product/guest/carts/customProducts/${id}`;
export const EDIT_CART = (id) => `/product/carts/custom-products/${id}`;
export const EDIT_CART_GUEST = (id) => `/product/guest/carts/custom-products/${id}`;
export const EDIT_CART_IMAGE = (id) => `/product/carts/custom-products/image/${id}`;
2 changes: 1 addition & 1 deletion src/constants/header.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const CONTENT_TYPE = {
MultipartFormData: 'Multipart/form-data',
MultipartFormData: 'multipart/form-data',
ApplicationJson: 'application/json',
};
Loading
Loading