ProductRegist.js 9.83 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import React, { useState, useEffect, useRef } from 'react';
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { Row, Col, Button, Form, Container } from 'react-bootstrap';
Jiwon Yoon's avatar
Jiwon Yoon committed
3
import axios from 'axios'
4
5
import catchErrors from '../utils/catchErrors';
import { Redirect } from 'react-router-dom';
Jiwon Yoon's avatar
Jiwon Yoon committed
6

7
8
9
let color = {}
let colors = []
let addedcolors = []
10
let list = []
Kim, Subin's avatar
Kim, Subin committed
11
12

function ProductsRegist() {
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    const INIT_PRODUCT = {
        pro_name: '',
        price: 0,
        stock: 0,
        main_category: '',
        sub_category: [],
        size: [],
        color: [],
        description: '',
        main_image: [],
        detail_image: []
    }
    const categorys = {
        "DRESS": ["LONG DRESS", "SHORT DRESS", "KNIT DRESS", "SHIRT DRESS", "PATTERN DRESS", "BUSTIER DRESS", "TWO-PIECE DRESS"],
        "OUTER": ["PADDED JACKET", "JACKET", "JUMPER", "COAT", "FLEECE", "CARDIGAN / VEST"],
        "TOP": ["KNIT", "HOODY", "BLOUSE", "SHIRT", "SWEATSHIRT", "LONG SLEEVE SHIRT", "SHORT SLEEVE / SLEEVELESS SHIRT"],
        "PANTS": ["JEANS", "SKINNY JEANS", "BANDING PANTS", "WIDE-FIT PANTS", "BOOT-CUT PANTS", "STRAIGHT-FIT PANTS", "SHORTS", "TROUSERS", "LEGGINGS", "JUMPSUIT / OVERALLS"],
        "SKIRT": ["LONG SKIRT", "MIDI SKIRT", "MINI SKIRT"],
        "TRAINING": [],
        "SHOES": ["SNEAKERS / SLIP-ON", "FLAT / LOAFER", "HEEL / PUMP", "BOOTS", "SANDAL / SLIPPER"]
    }
    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 })

    const mainCategorys = Object.keys(categorys)
    const subCategorys = Object.values(categorys)

    function addCategory() {
        console.log(product)
        list.push(
            <div>
                <span i={tag}>{product["main_category"]} / {product["sub_category"][tag]}</span>
                <input type="image" src="https://img.icons8.com/fluent-systems-regular/24/000000/close-window.png" className="float-right align-middle" onClick={deleteCategory} />
            </div>)
        setTag(tag + 1)
    }
    function deleteCategory(e) {
        const categ = e.target.parentNode
        categ.remove()
        product["sub_category"].splice(e.target.parentNode.firstElementChild.getAttribute("i"), 1)
        console.log(product)
    }
    function handleCheckBox(e) {
        setChecked({ ...checked, [e.target.value]: !checked[`${e.target.value}`] })
    }

    function addColor() {
        addedcolors.push(
            <div>{color["color"]}</div>
        )
        if (product.color) {
            setProduct({ ...product, [color]: colors })
        } else {
            setProduct({ ...product, "color": colors })

        }

    }
Jiwon Yoon's avatar
Jiwon Yoon committed
75
76
77

    function handleChange(event) {
        const { name, value } = event.target
78
79
80
81
82
83
84
85
86
87
88
        if (event.target.name === "sub_category") {
            product["sub_category"].push(event.target.value)
        } else if (event.target.name === "color") {
            color[event.target.name] = event.target.value
            // console.log(color)
        } else {
            setProduct({ ...product, [name]: value })
        }
        if (event.target.name === "main_category") {
            setCategoryNum(event.target.selectedIndex - 1)
        }
Jiwon Yoon's avatar
Jiwon Yoon committed
89
90
    }

91
    async function handleSubmit(e) {
Jiwon Yoon's avatar
Jiwon Yoon committed
92
        e.preventDefault()
93
94
95
96
97
98
99
        const sizes = []
        for (let [key, value] of Object.entries(checked)) {
            if (value === true) {
                sizes.push(key)
            }
        }
        product["size"].push(sizes)
kusang96's avatar
kusang96 committed
100
        const formData = new FormData();
101
        for (let key in product) {
kusang96's avatar
kusang96 committed
102
            formData.append(key, product[key])
103
        }
104
105
106
107
108
109
110
111
112
113
114
115
116
        console.log("formData=", formData)
        console.log(product)
        try {
            const response = axios.post('/api/product/regist', { data: formData })
            setSuccess(true)
            console.log(response)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    if (success) {
        return <Redirect to='/' />
117
    }
Kim, Subin's avatar
Kim, Subin committed
118
119
    return (
        <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
120
121
            <Container>
                <Row className="justify-content-md-center">
122
                    <Col md={8} className="border p-1" style={{ background: '#F7F3F3' }}>
Jiwon Yoon's avatar
Jiwon Yoon committed
123
                        <h2 className="text-center mt-5 font-weight-bold">상품등록</h2>
Jiwon Yoon's avatar
Jiwon Yoon committed
124
                        <Form className="p-5" onSubmit={handleSubmit}>
Jiwon Yoon's avatar
Jiwon Yoon committed
125
126
                            <Form.Group controlId="productNameform">
                                <Form.Label>상품명</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
127
                                <Form.Control type="text" name="pro_name" placeholder="상품명" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
128
129
                            </Form.Group>
                            <Form.Group controlId="productAmountform">
Jiwon Yoon's avatar
Jiwon Yoon committed
130
131
                                <Form.Label>재고</Form.Label>
                                <Form.Control type="text" name="stock" placeholder="숫자만 입력해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
132
133
134
                            </Form.Group>
                            <Form.Group controlId="productPriceform">
                                <Form.Label>가격</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
135
                                <Form.Control type="text" name="price" placeholder="숫자만 입력해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
136
137
138
139
                            </Form.Group>
                            <Form.Group>
                                <Form.Label>분류</Form.Label>
                                <Row>
140
                                    <Col md={4}>
141
142
143
                                        <Form.Control as="select" name="main_category" onChange={handleChange}>
                                            <option value="" >상위분류</option>
                                            {mainCategorys.map((main) => (
144
                                                <option value={main}>{main}</option>
145
                                            ))}
Jiwon Yoon's avatar
Jiwon Yoon committed
146
147
148
                                        </Form.Control>
                                    </Col>
                                    <Col md={6}>
149
150
151
                                        <Form.Control as="select" name="sub_category" onChange={handleChange}>
                                            <option value="" >하위분류</option>
                                            {subCategorys[categoryNum].map((sub) => (
152
                                                <option value={sub}>{sub}</option>
153
                                            ))}
Jiwon Yoon's avatar
Jiwon Yoon committed
154
155
                                        </Form.Control>
                                    </Col>
156
157
158
                                    <Col >
                                        <Button className="float-right" style={{ background: '#91877F', borderColor: '#91877F' }} onClick={addCategory}>추가</Button>
                                    </Col>
Jiwon Yoon's avatar
Jiwon Yoon committed
159
                                </Row>
160
                                {list.map((element) => element)}
Jiwon Yoon's avatar
Jiwon Yoon committed
161
                            </Form.Group>
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
                            <Form.Group>
                                <Form.Label>사이즈</Form.Label>
                                {/* {console.log(checked)} */}
                                <Form.Check type="checkbox" name="size" label="Free" value="Free" onChange={handleCheckBox} />
                                <Form.Check type="checkbox" name="size" label="XL" value="XL" onChange={handleCheckBox} />
                                <Form.Check type="checkbox" name="size" label="L" value="L" onChange={handleCheckBox} />
                                <Form.Check type="checkbox" name="size" label="M" value="M" onChange={handleCheckBox} />
                                <Form.Check type="checkbox" name="size" label="S" value="S" onChange={handleCheckBox} />
                                <Form.Check type="checkbox" name="size" label="XS" value="XS" onChange={handleCheckBox} />
                            </Form.Group>
                            <Form.Group>
                                <Form.Label>색상</Form.Label>
                                <Row>
                                    <Col md={10}>
                                        <Form.Control as="textarea" rows={1} name="color" placeholder="색상" onChange={handleChange} />

                                    </Col>
                                    <Col>

                                        <Button className="float-right" style={{ background: '#91877F', borderColor: '#91877F' }} onClick={addColor}>추가</Button>
                                    </Col>
                                </Row>
                                {addedcolors.map((element) => element)}
                            </Form.Group>

Jiwon Yoon's avatar
Jiwon Yoon committed
187
188
                            <Form.Group controlId="productDescriptionform">
                                <Form.Label>상품설명</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
189
                                <Form.Control as="textarea" name="description" rows={3} placeholder="상품을 설명해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
190
191
192
                            </Form.Group>
                            <Form.Group>
                                <Form.Label>대표이미지</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
193
194
195
196
197
                                <Form.File id="productImageform" name="main_image" onChange={handleChange} />
                            </Form.Group>
                            <Form.Group>
                                <Form.Label>상세이미지</Form.Label>
                                <Form.File id="productImageform" name="detail_image" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
198
                            </Form.Group>
199
                            <Button className="float-right" type="submit" style={{ background: '#91877F', borderColor: '#91877F' }}>등록</Button>
Jiwon Yoon's avatar
Jiwon Yoon committed
200
201
202
203
                        </Form>
                    </Col>
                </Row>
            </Container>
Kim, Subin's avatar
Kim, Subin committed
204
205
206
207
208
        </div>
    )
}

export default ProductsRegist