import axios from 'axios'; import React, { useState, useEffect, useRef } from 'react'; import { Row, Col, Form, Card, Button } from 'react-bootstrap'; import { Redirect } from 'react-router-dom'; import catchErrors from '../utils/catchErrors'; function Product({ match, location }) { const [product, setProduct] = useState(location.state) const [color, setColor] = useState("") const [size, setSize] = useState("") const [cart, setCart] = useState([]) const [error, setError] = useState('') const [selected, setSelected] = useState({ sizes: false, colors: false }) const [count, setCount] = useState(1) const [price, setPrice] = useState(0) useEffect(() => { if (size && color) { pushOptions() console.log(cart) } }, [size, color]) function handleClick(e) { const box = e.target.parentNode.parentNode box.style.display = "none" } function pushOptions() { setCart([...cart, { color, size, productId: product.id }]) selected.sizes = false selected.colors = false setColor("") setSize("") setPrice(product.price + price) } function handleChange(e) { const { name, value } = e.target if (name === "sizes") { // setPreCart({ ...preCart, [name]: value }) setSize(value) selected.sizes = true } else if (name === "colors") { // setPreCart({ ...preCart, [name]: value }) setColor(value) selected.colors = true } } function deleteOption(e) { e.preventDefault() const asd = cart.filter((el) => el.color !== e.target.id || el.size !== e.target.name) setCart(asd) } function handleCount(e) { e.preventDefault() // const asd = cart.filter((el) => el.color !== e.target.id || el.size !== e.target.name) const asd= cart.map((el)=>{ if(el.color !== e.target.id || el.size !== e.target.name){ return {el} } else { return {...el, count : e.target.value} } }) // const index = product["sub_category"].findIndex((item)=>{return item === e.target.name}) // product["sub_category"].splice(index, 1) setCart(asd) setCount(e.value) } async function addCart() { console.log(cart) if (localStorage.getItem('id')) { // preCart(color, size, count), productId(productlist에서 props), userId(로컬) 를 보내줌 try { setError('') const response = await axios.put('/api/cart/addcart', { userId: localStorage.getItem('id'), // productId: product.id, products: cart }) console.log(response) } catch (error) { catchErrors(error, setError) } } else { alert("로그인을 해주세요.") return } } return (
{/* {console.log("match=", match.params, "location=", location.state, "product=", product)} */}

{product.name}

가격 : {product.price}원
색상 {product.colors.map((e) => ( ))} 사이즈 {product.sizes.map((e) => ( ))} {cart.map((e) => (
{e.color}/{e.size} {e.price}원
))} 총 금액 {price}원

설명

회원님이 선호할만한 상품 추천 handleClick(e)} style={{ fontSize: "1rem" }}>X
클로타탄원피스 구매자 수: 30 클로타탄원피스 구매자 수: 30 클로타탄원피스 구매자 수: 30 클로타탄원피스 구매자 수: 30
) } export default Product