From f43e97c6f4e71d0e04403e849946c130b3ad6dc9 Mon Sep 17 00:00:00 2001 From: Dongwoo Date: Tue, 28 Nov 2023 16:48:04 +0900 Subject: [PATCH] [fix] add cart (Custom & License) --- src/axios/cart/Cart.js | 65 ++++++++++++++++++++--------- src/component/order/OrderOptions.js | 4 +- src/constants/api.js | 6 ++- src/screen/cart/CartList.js | 2 +- 4 files changed, 52 insertions(+), 25 deletions(-) diff --git a/src/axios/cart/Cart.js b/src/axios/cart/Cart.js index 5652603..bea4aad 100644 --- a/src/axios/cart/Cart.js +++ b/src/axios/cart/Cart.js @@ -4,8 +4,9 @@ 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, + ADD_CART_CUSTOM, + ADD_CART_CUSTOM_GUEST, + ADD_CART_LICENSE, CART_LIST, DELETE_CART, DELETE_CART_GUEST, @@ -13,35 +14,59 @@ import { EDIT_CART_GUEST, 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', + }); + }); } } diff --git a/src/component/order/OrderOptions.js b/src/component/order/OrderOptions.js index 506ff55..9da6b8f 100644 --- a/src/component/order/OrderOptions.js +++ b/src/component/order/OrderOptions.js @@ -76,7 +76,7 @@ export default function OrderOptions({ productId, productInfo, price, setPrice } data = { productId: productInfo?.id, optionDetailIds: options, - licenseOptionDetailId: img.id, + licenseOptionId: img.id, quantity: parseInt(quantity), }; } @@ -104,7 +104,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; diff --git a/src/constants/api.js b/src/constants/api.js index 66474ab..ea4ed6c 100644 --- a/src/constants/api.js +++ b/src/constants/api.js @@ -29,8 +29,10 @@ 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}`; diff --git a/src/screen/cart/CartList.js b/src/screen/cart/CartList.js index 6c95bc7..60991d9 100644 --- a/src/screen/cart/CartList.js +++ b/src/screen/cart/CartList.js @@ -134,7 +134,7 @@ export default function CartList({ setEmptyMode }) { }, [cartData]); function pay() { - if (checkedList == '') { + if (checkedList === '') { alert('체크된 장바구니 항목이 없습니다'); } else { navigate(PAYMENT, {