ProductRegist.js 4.86 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'
Jiwon Yoon's avatar
Jiwon Yoon committed
4

5
let list = []
Kim, Subin's avatar
Kim, Subin committed
6
7

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

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

17
    async function handleSubmit(e) {
Jiwon Yoon's avatar
Jiwon Yoon committed
18
        e.preventDefault()
kusang96's avatar
kusang96 committed
19
20
21
        const formData = new FormData();
        for (let key of Object.keys(product)) {
            formData.append(key, product[key])
22
        }
kusang96's avatar
kusang96 committed
23
24
        console.log("formData=",formData)
        axios.post('/api/product/regist',{data: formData}).then(function(res) {
Jiwon Yoon's avatar
Jiwon Yoon committed
25
26
            console.log("client의 res=", res)
        })
27
    }
Jiwon Yoon's avatar
22    
Jiwon Yoon committed
28
29
    // if (success) {
    //     return <Redirect to='/' />
30
    // }
Kim, Subin's avatar
Kim, Subin committed
31
32
    return (
        <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
33
34
            <Container>
                <Row className="justify-content-md-center">
35
                    <Col md={8} className="border p-1" style={{ background: '#F7F3F3' }}>
Jiwon Yoon's avatar
Jiwon Yoon committed
36
                        <h2 className="text-center mt-5 font-weight-bold">상품등록</h2>
Jiwon Yoon's avatar
Jiwon Yoon committed
37
                        <Form className="p-5" onSubmit={handleSubmit}>
Jiwon Yoon's avatar
Jiwon Yoon committed
38
39
                            <Form.Group controlId="productNameform">
                                <Form.Label>상품명</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
40
                                <Form.Control type="text" name="pro_name" placeholder="상품명" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
41
42
                            </Form.Group>
                            <Form.Group controlId="productAmountform">
Jiwon Yoon's avatar
Jiwon Yoon committed
43
44
                                <Form.Label>재고</Form.Label>
                                <Form.Control type="text" name="stock" placeholder="숫자만 입력해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
45
46
47
                            </Form.Group>
                            <Form.Group controlId="productPriceform">
                                <Form.Label>가격</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
48
                                <Form.Control type="text" name="price" placeholder="숫자만 입력해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
49
50
51
52
                            </Form.Group>
                            <Form.Group>
                                <Form.Label>분류</Form.Label>
                                <Row>
53
                                    <Col md={4}>
Jiwon Yoon's avatar
Jiwon Yoon committed
54
                                        <Form.Control as="select" name="main_category" placeholder="상위분류" onChange={handleChange}>
Jiwon Yoon's avatar
22    
Jiwon Yoon committed
55
                                            {/* {mainCategorys.map((main) => (
56
                                                <option value={main}>{main}</option>
Jiwon Yoon's avatar
22    
Jiwon Yoon committed
57
                                            ))} */}
Jiwon Yoon's avatar
Jiwon Yoon committed
58
59
60
                                        </Form.Control>
                                    </Col>
                                    <Col md={6}>
Jiwon Yoon's avatar
Jiwon Yoon committed
61
                                        <Form.Control as="select" name="sub_category" placeholder="하위분류" onChange={handleChange}>
Jiwon Yoon's avatar
22    
Jiwon Yoon committed
62
                                            {/* {subCategorys[categoryNum].map((sub) => (
63
                                                <option value={sub}>{sub}</option>
Jiwon Yoon's avatar
22    
Jiwon Yoon committed
64
                                            ))} */}
Jiwon Yoon's avatar
Jiwon Yoon committed
65
66
                                        </Form.Control>
                                    </Col>
Jiwon Yoon's avatar
22    
Jiwon Yoon committed
67
                                    {/* <Col md={2}>
68
                                        <Button style={{ background: '#91877F', borderColor: '#91877F' }} onClick={addCategory}>추가</Button>
Jiwon Yoon's avatar
22    
Jiwon Yoon committed
69
                                    </Col> */}
Jiwon Yoon's avatar
Jiwon Yoon committed
70
                                </Row>
71
                                {list.map((element) => element)}
Jiwon Yoon's avatar
Jiwon Yoon committed
72
73
74
                            </Form.Group>
                            <Form.Group controlId="productDescriptionform">
                                <Form.Label>상품설명</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
75
                                <Form.Control as="textarea" name="description" rows={3} placeholder="상품을 설명해주세요" onChange={handleChange} />
Jiwon Yoon's avatar
Jiwon Yoon committed
76
77
78
                            </Form.Group>
                            <Form.Group>
                                <Form.Label>대표이미지</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
79
80
81
82
83
                                <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
84
                            </Form.Group>
85
                            <Button className="float-right" type="submit" style={{ background: '#91877F', borderColor: '#91877F' }}>등록</Button>
Jiwon Yoon's avatar
Jiwon Yoon committed
86
87
88
89
                        </Form>
                    </Col>
                </Row>
            </Container>
Kim, Subin's avatar
Kim, Subin committed
90
91
92
93
94
        </div>
    )
}

export default ProductsRegist