ProductRegist.js 5.06 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';
Jiwon Yoon's avatar
Jiwon Yoon committed
4
import { Row, Col, Button, Form, Container } from 'react-bootstrap';
Jiwon Yoon's avatar
Jiwon Yoon committed
5
import axios from 'axios'
Jiwon Yoon's avatar
Jiwon Yoon committed
6

7
let list = []
Kim, Subin's avatar
Kim, Subin committed
8
9

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

    function handleChange(event) {
        const { name, value } = event.target
kusang96's avatar
kusang96 committed
14
15
        console.log("file=",event.target.files)
        console.log("name=",name,"value=",value)
Jiwon Yoon's avatar
Jiwon Yoon committed
16
17
18
        setProduct({ ...product, [name]: value })
    }

19
    async function handleSubmit(e) {
Jiwon Yoon's avatar
Jiwon Yoon committed
20
        e.preventDefault()
kusang96's avatar
kusang96 committed
21
22
23
        const formData = new FormData();
        for (let key of Object.keys(product)) {
            formData.append(key, product[key])
24
        }
kusang96's avatar
kusang96 committed
25
26
        console.log("formData=",formData)
        axios.post('/api/product/regist',{data: formData}).then(function(res) {
Jiwon Yoon's avatar
Jiwon Yoon committed
27
28
            console.log("client의 res=", res)
        })
29
    }
30
31
    //     }catch(error) {
    //         catchErrors(error, setError)
Jiwon Yoon's avatar
Jiwon Yoon committed
32

33
34
35
    //     }
        
    // }
36
37
38
    if (success) {
        return <Redirect to='/' />
    }
Kim, Subin's avatar
Kim, Subin committed
39
40
    return (
        <div>
kusang96's avatar
kusang96 committed
41
42
            <MainNav />
            <SubNav />
Jiwon Yoon's avatar
Jiwon Yoon committed
43
44
            <Container>
                <Row className="justify-content-md-center">
45
                    <Col md={8} className="border p-1" style={{ background: '#F7F3F3' }}>
Jiwon Yoon's avatar
Jiwon Yoon committed
46
                        <h2 className="text-center mt-5 font-weight-bold">상품등록</h2>
Jiwon Yoon's avatar
Jiwon Yoon committed
47
                        <Form className="p-5" onSubmit={handleSubmit}>
Jiwon Yoon's avatar
Jiwon Yoon committed
48
49
                            <Form.Group controlId="productNameform">
                                <Form.Label>상품명</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
50
                                <Form.Control type="text" name="pro_name" placeholder="상품명" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
51
52
                            </Form.Group>
                            <Form.Group controlId="productAmountform">
Jiwon Yoon's avatar
Jiwon Yoon committed
53
54
                                <Form.Label>재고</Form.Label>
                                <Form.Control type="text" name="stock" placeholder="숫자만 입력해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
55
56
57
                            </Form.Group>
                            <Form.Group controlId="productPriceform">
                                <Form.Label>가격</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
58
                                <Form.Control type="text" name="price" placeholder="숫자만 입력해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
59
60
61
62
                            </Form.Group>
                            <Form.Group>
                                <Form.Label>분류</Form.Label>
                                <Row>
63
                                    <Col md={4}>
Jiwon Yoon's avatar
Jiwon Yoon committed
64
                                        <Form.Control as="select" name="main_category" placeholder="상위분류" onChange={handleChange}>
65
66
67
                                            {mainCategorys.map((main) => (
                                                <option value={main}>{main}</option>
                                            ))}
Jiwon Yoon's avatar
Jiwon Yoon committed
68
69
70
                                        </Form.Control>
                                    </Col>
                                    <Col md={6}>
Jiwon Yoon's avatar
Jiwon Yoon committed
71
                                        <Form.Control as="select" name="sub_category" placeholder="하위분류" onChange={handleChange}>
72
73
74
                                            {subCategorys[categoryNum].map((sub) => (
                                                <option value={sub}>{sub}</option>
                                            ))}
Jiwon Yoon's avatar
Jiwon Yoon committed
75
76
                                        </Form.Control>
                                    </Col>
77
78
79
                                    <Col md={2}>
                                        <Button style={{ background: '#91877F', borderColor: '#91877F' }} onClick={addCategory}>추가</Button>
                                    </Col>
Jiwon Yoon's avatar
Jiwon Yoon committed
80
                                </Row>
81
                                {list.map((element) => element)}
Jiwon Yoon's avatar
Jiwon Yoon committed
82
83
84
                            </Form.Group>
                            <Form.Group controlId="productDescriptionform">
                                <Form.Label>상품설명</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
85
                                <Form.Control as="textarea" name="description" rows={3} placeholder="상품을 설명해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
86
87
88
                            </Form.Group>
                            <Form.Group>
                                <Form.Label>대표이미지</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
89
90
91
92
93
                                <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
94
                            </Form.Group>
95
                            <Button className="float-right" type="submit" style={{ background: '#91877F', borderColor: '#91877F' }}>등록</Button>
Jiwon Yoon's avatar
Jiwon Yoon committed
96
97
98
99
                        </Form>
                    </Col>
                </Row>
            </Container>
Kim, Subin's avatar
Kim, Subin committed
100
101
102
103
104
        </div>
    )
}

export default ProductsRegist