import axios from 'axios'; import React, { useState, useEffect, useRef } from 'react'; import { Row, Col, Form, Card, Button } from 'react-bootstrap'; import catchErrors from '../utils/catchErrors'; const INIT_PRODUCT = { pro_name: '스키니진', price: 12000, count: 1, main_category: 'PANTS', sub_category: ['SKINNY JEANS'], sizes: ['L', 'M'], colors: ['연청', '진청'], main_image: "a8f4d63ead77717f940a2b27deb707a6", productId:"5ffda03428faf35de8319360" } const preCart = [] function Product() { const [product, setProduct] = useState(INIT_PRODUCT) const [cart, setCart] = useState(INIT_PRODUCT) const [error, setError] = useState('') const [selected, setSelected] = useState({ sizes: false, colors: false }) const [price, setPrice] = useState(0) useEffect(() => { if (selected.sizes === true && selected.colors === true) { pushOptions() console.log(preCart) } }, [cart]) function handleClick(e) { const box = e.target.parentNode.parentNode box.style.display = "none" } function pushOptions() { preCart.push(cart) selected.sizes = false selected.colors = false setPrice(product.price+price) } function handleChange(e) { const { name, value } = e.target if (e.target.name === "sizes") { setCart({ ...cart, [name]: value }) selected.sizes = true } else if (e.target.name === "colors") { setCart({ ...cart, [name]: value }) selected.colors = true } // setCart({ ...cart, [name]: value }) // handleCreate() } function listDelete(e) { e.preventDefault() const parent = e.target.parentNode parent.remove() } function handleCreate() { console.log("실행", "cart=", product) // if (product !== undefined) { // if (product.colors !== "" && product.sizes !== "") { // cart.push( //
{product.color} {product.size}
// //{product.price}
//14,000원
` // const deleteA = document.createElement('a') // deleteA.innerText = 'X' // deleteA.addEventListener('click', listDelete) // shopping.appendChild(deleteA) // list.appendChild(shopping) // } // } } async function addCart() { // preCart(color, size, count), productId(productlist에서 props), userId(로컬) 를 보내줌 try { setError('') const response = await axios.put('/api/cart/addcart', { userId: localStorage.getItem('loginStatus'), productId: "a8f4d63ead77717f940a2b27deb707a6", products: preCart }) console.log(response) } catch (error) { catchErrors(error, setError) } } return (