import React, { useState, useEffect, useRef } from 'react'; import { Redirect } from 'react-router-dom'; import { Row, Col, Button, Form, Container, Alert } from 'react-bootstrap'; import axios from 'axios'; import catchErrors from '../utils/catchErrors'; let color = {} let preColors = [] let colorHtml = [] let list = [] function ProductsRegist() { const INIT_PRODUCT = { pro_name: '', price: 0, stock: 0, main_category: '', sub_category: [], sizes: [], colors: [], description: '', main_image: [], detail_image: [] } const [categorys, setCategorys] = useState({ 0: [], 1: [[]] }) const [product, setProduct] = useState(INIT_PRODUCT) const [categoryNum, setCategoryNum] = useState(0) const [tag, setTag] = useState(0) 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 }) useEffect(async () => { try { const response = await axios.get('/api/categorys') const data = response.data[0] setCategorys([Object.keys(data), Object.values(data)]) } catch (error) { catchErrors(error, setError) } }, []) function addCategory() { console.log(product) list.push(
{color["colors"]}
) setProduct({ ...product, "colors": preColors }) } function colorChange(e) { color[e.target.name] = e.target.value } function handleChange(event) { const { name, value, files } = event.target console.log("event.target.name=", name, "event.target.value=", value) if (name === "sub_category") { product[name].push(value) } else if (files) { setProduct({ ...product, [name]: files }) } else { setProduct({ ...product, [name]: value }) } if (event.target.name === "main_category") { setCategoryNum(event.target.selectedIndex - 1) } } 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 const formData = new FormData(); for (let key in product) { if (key === "main_image" || key === "detail_image") { console.log(product[key][0]) formData.append(key, product[key][0]) } else { formData.append(key, product[key]) } } try { const response = axios.post('/api/product/regist', formData) // setSuccess(true) console.log(response) } catch (error) { catchErrors(error, setError) } } if (success) { return