diff --git a/src/axios/cart/Cart.js b/src/axios/cart/Cart.js index 244b545..5652603 100644 --- a/src/axios/cart/Cart.js +++ b/src/axios/cart/Cart.js @@ -14,17 +14,17 @@ import { PRODUCT_OPTION, } from '../../constants/api'; -export default function post(dto, file) { +export default function post(dto, file, isCustom) { const formData = new FormData(); - formData.append('file', file); + 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, - }, + .post(ADD_CART(), formData, { + headers: { + Authorization: sessionStorage.getItem(ACCESS_TOKEN), + "Content-Type": CONTENT_TYPE.MultipartFormData, + }, }) .then(() => { alert('장바구니에 담겼습니다!'); @@ -51,13 +51,13 @@ export const fetchCartData = (token, setCartList, setEmptyMode, setProductOption }; axios - .get(CART_LIST, { headers }) + .get(CART_LIST(), { headers }) .then((response) => { setCartList(response.data); if (!response.data || response.data === '') { setEmptyMode(true); } else { - axios.get(PRODUCT_OPTION, { headers }).then((response) => { + axios.get(PRODUCT_OPTION(), { headers }).then((response) => { setProductOption(response.data[0].productOptionList); }); } diff --git a/src/component/order/OrderOptions.js b/src/component/order/OrderOptions.js index ccc60f1..a1bd4e6 100644 --- a/src/component/order/OrderOptions.js +++ b/src/component/order/OrderOptions.js @@ -22,6 +22,7 @@ export default function OrderOptions({ productId, productInfo, price, setPrice } const [mode, setMode] = useState(''); const [additionalPrice, setAdditionalPrice] = useState({}); const [quantity, setQuantity] = useState(1); + const [licenseId, setLicenseId] = useState(''); useEffect(() => { setFrameOption({}); @@ -56,12 +57,16 @@ export default function OrderOptions({ productId, productInfo, price, setPrice } const options = Object.values(frameOption).map((item) => { return item.id; }); - const image = e.target.file.files[0]; const data = { productId: productInfo?.id, optionDetailIds: options, quantity: parseInt(quantity), }; + const isCustom = productInfo?.custom; + let image = null; + if (isCustom) { + image = e.target.file.files[0]; + } imageFile = image; let pass = true; @@ -75,7 +80,7 @@ export default function OrderOptions({ productId, productInfo, price, setPrice } pass = false; } }); - if (!imageFile) { + if (!imageFile && isCustom) { Swal.fire({ title: '이미지를 입력해주세요', icon: 'warning', @@ -83,10 +88,21 @@ export default function OrderOptions({ productId, productInfo, price, setPrice } window.location.href = '#add-image'; pass = false; } + if (!isCustom) { + data.optionDetailIds = [licenseId, ...options]; + if (licenseId == '') { + Swal.fire({ + title: '라이선스 이미지를 입력해주세요', + icon: 'warning', + }); + window.location.href = '#add-image'; + pass = false; + } + } switch (mode) { case ORDER_MODE.CART: if (!pass) break; - post(data, imageFile); + post(data, imageFile, isCustom); break; case ORDER_MODE.BUY: if (!pass) break; @@ -123,6 +139,7 @@ export default function OrderOptions({ productId, productInfo, price, setPrice } custom={productInfo.custom} optionItems={license.optionItems} moveToEditor={moveToEditor} + setLicenseId={setLicenseId} /> { ); }; -const AddImage = ({ custom, optionItems, moveToEditor }) => { +const AddImage = ({ custom, optionItems, moveToEditor, setLicenseId }) => { return ( <> {custom ? ( @@ -234,6 +251,9 @@ const AddImage = ({ custom, optionItems, moveToEditor }) => { {optionItem.artName} { + setLicenseId(optionItem.id); + }} onContextMenu={(e) => { e.preventDefault(); }} diff --git a/src/screen/cart/CartList.js b/src/screen/cart/CartList.js index eba0d83..6c95bc7 100644 --- a/src/screen/cart/CartList.js +++ b/src/screen/cart/CartList.js @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useState, useRef } from 'react'; import './Cart.css'; import './CartPrice.css'; import 'bootstrap/dist/css/bootstrap.min.css'; @@ -17,9 +17,11 @@ import { MAIN, PAYMENT } from '../../constants/path'; export default function CartList({ setEmptyMode }) { const isDesktopOrMobile = useMediaQuery({ query: '(max-width:768px)' }); const navigate = useNavigate(); + const fileInputRef = useRef(null); const [checkedList, setCheckedList] = useState([]); const [totalPrice, setTotalPrice] = useState(0.0); + const [deliveryPrice, setDeliveryPrice] = useState(0.0); const [paymentValue, setPaymentValue] = useState([]); const [editMode, setEditMode] = useState(false); const [formValue, setFormValue] = useState({}); @@ -30,8 +32,17 @@ export default function CartList({ setEmptyMode }) { let [customProductId, setCustomProductId] = useState(''); let basicFormValue = {}; const onImageChange = (e) => { - const img = e.target.files[0]; - setImageFile(img); + const file = e.target.files[0]; + if (file) { + const imageUrl = URL.createObjectURL(file); + setImageFile({ file, imageUrl }); + } + }; + const onImageClick = () => { + const fileInput = document.getElementById('fileInput'); + if (fileInput) { + fileInput.click(); + } }; const onHandleSubmit = (e) => { e.preventDefault(); @@ -63,6 +74,7 @@ export default function CartList({ setEmptyMode }) { setCustomProductId(id); setFormValue(basicFormValue); setQuantity(quantity); + setImageFile(''); }; const onHandleChange = (value, optionId) => { setFormValue({ @@ -70,7 +82,7 @@ export default function CartList({ setEmptyMode }) { [optionId]: value, }); }; - const onCheckedElement = (checked, item, price, options, quantity, url) => { + const onCheckedElement = (checked, item, price, options, quantity, url, deliveryFee) => { const frameOption = {}; options.map((option) => { frameOption[option.optionName] = option.detailName; @@ -85,11 +97,13 @@ export default function CartList({ setEmptyMode }) { if (checked) { setCheckedList([...checkedList, item]); setTotalPrice(totalPrice + price); + setDeliveryPrice(deliveryPrice + (deliveryFee * quantity)); setPaymentValue([...paymentValue, thisValue]); } else if (!checked) { setCheckedList(checkedList.filter((element) => element !== item)); setPaymentValue(paymentValue.filter((data) => data.id !== item)); setTotalPrice(totalPrice - price); + setDeliveryPrice(deliveryPrice - (deliveryFee * quantity)); } }; @@ -120,7 +134,7 @@ export default function CartList({ setEmptyMode }) { }, [cartData]); function pay() { - if (checkedList === '') { + if (checkedList == '') { alert('체크된 장바구니 항목이 없습니다'); } else { navigate(PAYMENT, { @@ -151,11 +165,12 @@ export default function CartList({ setEmptyMode }) { 제품명 - 제품가격 - 옵션 - 첨부사진 - 수량 - 주문금액 + 제품가격 + 옵션 + 첨부사진 + 수량 + 주문금액 + 배송비 @@ -180,11 +195,12 @@ export default function CartList({ setEmptyMode }) { 제품명 - 제품가격 - 옵션 + 제품가격 + 옵션 첨부사진 - 수량 - 주문금액 + 수량 + 주문금액 + 배송비 @@ -214,6 +230,7 @@ export default function CartList({ setEmptyMode }) { item.options, item.quantity, item.imageUrl, + item.deliveryFee, ); }} > @@ -230,13 +247,18 @@ export default function CartList({ setEmptyMode }) { {item.quantity} {addComma(orderAmount)}원 + {addComma(item.deliveryFee)}원 - + @@ -268,12 +290,24 @@ export default function CartList({ setEmptyMode }) { - + {imageFile !== '' ? ( +
+ Selected +
+ ) : ( + + )} + ); @@ -331,12 +366,14 @@ export default function CartList({ setEmptyMode }) {

배송비

-

무료

+

+ {deliveryPrice === 0.0 ? '무료' : ` ${addComma(deliveryPrice)} 원`} +

총 결제 금액

-

{addComma(totalPrice)} 원

+

{addComma(totalPrice + deliveryPrice)} 원