ProductRegist.js 5.33 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import React, { useState, useEffect, useRef } from 'react';
kusang96's avatar
kusang96 committed
2
3
import MainNav from '../Components/MainNav';
import SubNav from '../Components/SubNav';
kusang96's avatar
dd    
kusang96 committed
4
5
6
import { Row, Col, Button, Form, Container, Alert } from 'react-bootstrap';
import axios from 'axios';
import catchErrors from "../utils/catchErrors";
Jiwon Yoon's avatar
Jiwon Yoon committed
7

Kim, Subin's avatar
Kim, Subin committed
8
9

function ProductsRegist() {
Jiwon Yoon's avatar
Jiwon Yoon committed
10
    const [product, setProduct] = useState()
kusang96's avatar
dd    
kusang96 committed
11
    const [error, setError] = useState('')
Jiwon Yoon's avatar
Jiwon Yoon committed
12

kusang96's avatar
dd    
kusang96 committed
13
14
15
16
17
18
19
    function handleChange(e) {
        const { name, value, files } = e.target
        if (files) {
            setProduct({ ...product, [name]: files })
        } else {
            setProduct({ ...product, [name]: value })
        }
Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
    }

kusang96's avatar
dd    
kusang96 committed
22
    async function handleSubmit(e) {
Jiwon Yoon's avatar
Jiwon Yoon committed
23
        e.preventDefault()
kusang96's avatar
kusang96 committed
24
        const formData = new FormData();
kusang96's avatar
dd    
kusang96 committed
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
        for (const key in product) {
            console.log("product[key]=", product[key])
            if (key == "main_image" || key == "detail_image") {
                for (const file of product[key]) {
                    formData.append(key, file)
                }
            } else {
                formData.append(key, product[key])
            }
        }
        // formData 값 확인용
        // for (const key of formData.keys()) {

        //     console.log("key=",key);

        //   }

        //   for (const value of formData.values()) {

        //     console.log(value);

        //   }
        try {
            const response = await axios.post('/api/product/regist', formData)
        } catch (error) {
            catchErrors(error, setError)
kusang96's avatar
kusang96 committed
51
        }
Jiwon Yoon's avatar
Jiwon Yoon committed
52
53
    }

Kim, Subin's avatar
Kim, Subin committed
54
55
    return (
        <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
56
            {console.log(product)}
kusang96's avatar
kusang96 committed
57
58
            <MainNav />
            <SubNav />
Jiwon Yoon's avatar
Jiwon Yoon committed
59
60
            <Container>
                <Row className="justify-content-md-center">
Jiwon Yoon's avatar
Jiwon Yoon committed
61
                    <Col md={6} className="border m-5 p-3" style={{ background: '#F7F3F3' }}>
kusang96's avatar
dd    
kusang96 committed
62
                        {error && <Alert variant="danger" className="text-center">{error}</Alert>}
Jiwon Yoon's avatar
Jiwon Yoon committed
63
                        <h2 className="text-center mt-5 font-weight-bold">상품등록</h2>
Jiwon Yoon's avatar
Jiwon Yoon committed
64
                        <Form className="p-5" onSubmit={handleSubmit}>
Jiwon Yoon's avatar
Jiwon Yoon committed
65
66
                            <Form.Group controlId="productNameform">
                                <Form.Label>상품명</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
67
                                <Form.Control type="text" name="pro_name" placeholder="상품명" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
68
69
                            </Form.Group>
                            <Form.Group controlId="productAmountform">
Jiwon Yoon's avatar
Jiwon Yoon committed
70
71
                                <Form.Label>재고</Form.Label>
                                <Form.Control type="text" name="stock" placeholder="숫자만 입력해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
72
73
74
                            </Form.Group>
                            <Form.Group controlId="productPriceform">
                                <Form.Label>가격</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
75
                                <Form.Control type="text" name="price" placeholder="숫자만 입력해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
76
77
78
79
80
                            </Form.Group>
                            <Form.Group>
                                <Form.Label>분류</Form.Label>
                                <Row>
                                    <Col md={6}>
Jiwon Yoon's avatar
Jiwon Yoon committed
81
                                        <Form.Control as="select" name="main_category" placeholder="상위분류" onChange={handleChange}>
Jiwon Yoon's avatar
Jiwon Yoon committed
82
83
84
85
86
87
                                            <option>Pants</option>
                                            <option>Skirt</option>
                                            <option>Outer</option>
                                        </Form.Control>
                                    </Col>
                                    <Col md={6}>
Jiwon Yoon's avatar
Jiwon Yoon committed
88
                                        <Form.Control as="select" name="sub_category" placeholder="하위분류" onChange={handleChange}>
89
90
91
                                            <option>JEANS</option>
                                            <option>SKINNY JEANS</option>
                                            <option>BANDING PANTS</option>
Jiwon Yoon's avatar
Jiwon Yoon committed
92
93
94
95
96
97
                                        </Form.Control>
                                    </Col>
                                </Row>
                            </Form.Group>
                            <Form.Group controlId="productDescriptionform">
                                <Form.Label>상품설명</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
98
                                <Form.Control as="textarea" name="description" rows={3} placeholder="상품을 설명해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
99
100
101
                            </Form.Group>
                            <Form.Group>
                                <Form.Label>대표이미지</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
102
103
104
105
106
                                <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
107
                            </Form.Group>
Jiwon Yoon's avatar
Jiwon Yoon committed
108
                            <Button className="float-right" variant="primary" type="submit" style={{ background: '#91877F', borderColor: '#91877F' }}>등록</Button>
Jiwon Yoon's avatar
Jiwon Yoon committed
109
110
111
112
                        </Form>
                    </Col>
                </Row>
            </Container>
Kim, Subin's avatar
Kim, Subin committed
113
114
115
116
117
        </div>
    )
}

export default ProductsRegist