import React, { useState, useEffect, useRef } from 'react'; import { Redirect } from 'react-router-dom'; import { Row, Col, Button, Form, Container, Alert, Spinner } from 'react-bootstrap'; import axios from 'axios'; import catchErrors from '../utils/catchErrors'; function ProductsRegist() { const INIT_PRODUCT = { pro_name: '', price: 0, stock: 0, main_category: '', sub_category: [], sizes: [], colors: [], description: '', main_image: [], detail_image: [] } const [preColors, setPreColors] = useState([]) const [categories, setCategories] = useState({ 0: [], 1: [[]] }) const [product, setProduct] = useState(INIT_PRODUCT) const [categoryNum, setCategoryNum] = useState('') const [tag, setTag] = useState(0) const [subCate, setSubCate] = useState([]) const [cateList, setCateList] = useState([]) const [color, setColor] = useState({}) const [error, setError] = useState('') const [success, setSuccess] = useState(false) const [checked, setChecked] = useState({ "Free": false, "XL": false, "L": false, "M": false, "S": false, "XS": false }) const [disabled, setDisabled] = useState(true) const [loading, setLoading] = useState(false) const selectRef = useRef(null) const colorRef = useRef(null) useEffect(async () => { try { const response = await axios.get('/api/categories/main') const data = response.data[0] setCategories([Object.keys(data), Object.values(data)]) } catch (error) { catchErrors(error, setError) } }, []) useEffect(() => { const isProduct = Object.values(product).every(el => { console.log("el=", el); Boolean(el) }) isProduct ? setDisabled(false) : setDisabled(true) }, [product]) function deleteCategory(e) { const pdcate = product.sub_category.filter((el) => el !== e.target.name) setProduct({ ...product, "sub_category": pdcate }) setSubCate([]) } function handleCategory(e) { const { name, value, selectedIndex } = e.target if (name === "main_category") { setProduct({ ...product, [name]: value }) setCategoryNum(selectedIndex - 1) } else { subCate.push(value) setProduct({ ...product, [name]: subCate }) selectRef.current.selectedIndex = 0 } } function handleCheckBox(e) { setChecked({ ...checked, [e.target.value]: !checked[`${e.target.value}`] }) } function addColor() { colorRef.current.value = '' setProduct({ ...product, "colors":[...product.colors, color["colors"]] }) } function deleteColor(e) { console.log(product.colors) console.log(e.target.name) const pdcolors = product.colors.filter((el) => el !== e.target.name) setProduct({ ...product, "colors": pdcolors }) } function handleColor(e) { color[e.target.name] = e.target.value } function handleChange(event) { const { name, value, files } = event.target if (files) { setProduct({ ...product, [name]: files }) } else { setProduct({ ...product, [name]: value }) } } async function handleSubmit(e) { e.preventDefault() const sizes = [] for (let [key, value] of Object.entries(checked)) { if (value === true) { sizes.push(key) } } product["sizes"] = sizes console.log(product) const formData = new FormData(); for (let key in product) { if (key === "main_image") { formData.append(key, product[key][0]) } else if (key === "sizes" || key === "colors" || key === "sub_category" || key === "detail_image") { for (let i = 0; i < product[key].length; i++) { formData.append([key], product[key][i]) } } else { formData.append(key, product[key]) } } try { setLoading(true) setError('') const response = await axios.post('/api/product/regist', formData) console.log(response) setSuccess(true) } catch (error) { catchErrors(error, setError) } finally { setLoading(false) } } if (success) { alert('상품 등록을 완료하였습니다.') return } return ( {console.log(product)} {error && {error}} 상품등록 상품명 재고 가격 분류 0} > 상위분류 {categories[0].map((main) => ( {main} ))} 하위분류 {(categoryNum === '') ? '' : (categories[1][categoryNum].map((sub) => ( {sub} )))} {product.sub_category.map((el) => ( {product["main_category"]} / {el} ))} 사이즈 색상 추가 {product.colors.map((el) => ( {el} ))} 상품설명 대표이미지 상세이미지 {loading && }{' '}등록 ) } export default ProductsRegist
{product["main_category"]} / {el}
{el}