Product.js 8.66 KB
Newer Older
1
import axios from 'axios';
kusang96's avatar
kusang96 committed
2
3
import React, { useState, useEffect, useRef } from 'react';
import { Row, Col, Form, Card, Button } from 'react-bootstrap';
4
import catchErrors from '../utils/catchErrors';
kusang96's avatar
kusang96 committed
5

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
6
7
8
9
10
11
12
13
14
15
16
const INIT_PRODUCT = {
    pro_name: '스키니진',
    price: 12000,
    count:1,
    main_category: 'PANTS',
    sub_category: ['SKINNY JEANS'],
    sizes: [ 'L', 'M'],
    colors: ['연청', '진청'],
    main_image: "a8f4d63ead77717f940a2b27deb707a6"
}

kusang96's avatar
kusang96 committed
17
function Product() {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
18
19
    const [product, setProduct] = useState(INIT_PRODUCT)
    const [cart, setCart] = useState(INIT_PRODUCT)
20
    const [error, setError] = useState('')
kusang96's avatar
kusang96 committed
21

Jiwon Yoon's avatar
Jiwon Yoon committed
22

kusang96's avatar
kusang96 committed
23
24
25
26
27
28
29
    function handleClick(e) {
        const box = e.target.parentNode.parentNode
        box.style.display = "none"
    }

    function handleChange(e) {
        const { name, value } = e.target
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
30
31
        // handleCreate()
        setCart({ ...cart, [name]: value })
kusang96's avatar
kusang96 committed
32
33
34
35
36
37
38
39
40
    }

    function listDelete(e) {
        e.preventDefault()
        const parent = e.target.parentNode
        parent.remove()
    }

    function handleCreate() {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        console.log("실행", "cart=", product)
        if (product !== undefined) {
            if (product.colors !== "" && product.sizes !== "") {
                cart.push(
                    <div className="d-flex justify-content-between my-2" >
                        <p>{product.color}  {product.size} </p>  
                        <input name="count" type="number" min="0" max="10" style="width: 40px" onChange={handleChange} />
                        <p style="margin-bottom: 0px">{product.price}</p>
                    </div>
                )
                // const list = document.getElementById('list')
                // list.style.borderBottom = "1px solid"
                // const shopping = document.createElement('div')
                // shopping.className = "d-flex justify-content-between my-2"
                // shopping.innerHTML = `${product.color} / ${product.size}
                // <input type="number" min="0" max="10" value="1" style="width: 40px" />
                // <p style="margin-bottom: 0px">14,000원</p>`
                // const deleteA = document.createElement('a')
                // deleteA.innerText = 'X'
                // deleteA.addEventListener('click', listDelete)
                // shopping.appendChild(deleteA)
                // list.appendChild(shopping)
kusang96's avatar
kusang96 committed
63
64
65
66
            }
        }
    }

67
    async function addCart() {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
68
        // color, size, count, productId(productlist에서 props), userId(로컬) 를 보내줌
69
70
        try {
            // setError('')
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
71
72
73
            const response = await axios.put('/api/cart/addcart', {
                count: cart.count,
                productId: "5ffd153b41bada58d8b12d92",
74
75
            })
            console.log(response)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
76
77
        } catch (error) {
            catchErrors(error, setError)
78
79
80
81
        }

    }

kusang96's avatar
kusang96 committed
82

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
83
84
85
    // useEffect(() => {
    //     handleCreate()
    // }, [product])
kusang96's avatar
kusang96 committed
86
87
88

    return (
        <div>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
89
            {console.log(cart)}
kusang96's avatar
kusang96 committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
            <style type="text/css">
                {`
                .btn {
                    background-color: #CDC5C2;
                    border-color: #CDC5C2;
                }

                .btn:hover, .btn:active, .btn:focus {
                    background-color: #91877F;
                    border-color: #91877F;
                }
                `}
            </style>
            <Row className="justify-content-center mt-5 mx-0">
                <Col sm={11} md={4}>
                    <img src="https://img.sonyunara.com/files/goods/65976/1601953605_0.jpg" style={{ objectFit: "contain", width: "100%" }} />
                </Col>
                <Col sm={11} md={4} className="align-middle mt-4">
                    <h3 className="mb-4">sop682 리본끈셋원피스</h3>
                    <h5 className="mb-4">가격 : 14,000</h5>
                    <Form style={{ borderBottom: "1px solid" }}>
                        <Form.Group style={{ borderBottom: "1px solid", paddingBottom: "2rem" }}>
                            <Form.Label>색상</Form.Label>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
113
                            <Form.Control as="select" className="mb-2" name="colors" defaultValue="옵션 선택" onChange={handleChange}>
kusang96's avatar
kusang96 committed
114
                                <option>옵션선택</option>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
115
116
117
                                {product.colors.map((e) => (
                                    <option>{e}</option>
                                ))}
kusang96's avatar
kusang96 committed
118
119
                            </Form.Control>
                            <Form.Label>사이즈</Form.Label>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
120
                            <Form.Control as="select" className="mb-2" name="sizes" defaultValue="옵션 선택" onChange={handleChange}>
kusang96's avatar
kusang96 committed
121
                                <option>옵션선택</option>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
122
123
124
                                {product.sizes.map((e) => (
                                    <option>{e}</option>
                                ))}
kusang96's avatar
kusang96 committed
125
126
                            </Form.Control>
                        </Form.Group>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
127
128
                        {/* {cart.map((e)=>(<div>{e}</div>
                        ))} */}
kusang96's avatar
kusang96 committed
129
130
131
132
133
                        <Row className="justify-content-between mx-0 my-3" style={{ width: "100%" }}>
                            <Col> 금액</Col>
                            <Col className="text-right">14,000</Col>
                        </Row>
                        <Row className="justify-content-between mx-0 my-3" style={{ width: "100%" }}>
134
                            <Button onClick={addCart} style={{ width: "49%" }}>장바구니</Button>
kusang96's avatar
kusang96 committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
187
                            <Button style={{ width: "49%" }}>구매하기</Button>
                        </Row>
                    </Form>
                </Col>
            </Row>
            <Row className="justify-content-center mt-5 mx-0">
                <Col sm={11} md={8}>
                    <h3 style={{ borderBottom: "1px solid #91877F", paddingBottom: "5px", marginBottom: "1em" }}>설명</h3>
                    <div></div>
                </Col>
            </Row>
            <Row className="justify-content-center mx-0 pt-3 px-2" style={{ position: "fixed", bottom: "0", width: "100%", backgroundColor: "#fff" }}>
                <Col sm={12} md={9}>
                    <h6 style={{ borderBottom: "1px solid", paddingBottom: "5px", marginBottom: "1em" }}>회원님이 선호할만한 상품 추천
                        <a className="close float-right" onClick={(e) => handleClick(e)} style={{ fontSize: "1rem" }}>X</a>
                    </h6>
                    <Row className="justify-content-space mx-0" style={{ flexWrap: "nowrap", width: "100%", overflowX: "auto" }}>
                        <Col as={Card} style={{ minWidth: "10rem", marginRight: "1rem" }}>
                            <Card.Img variant="top" src="https://img.sonyunara.com/files/goods/67504/1607328307_0.jpg" style={{ objectFit: "contain" }} />
                            <Card.Body className="px-0">
                                <Card.Title>클로타탄원피스</Card.Title>
                                <Card.Text>구매자 : 30</Card.Text>
                            </Card.Body>
                        </Col>
                        <Col as={Card} style={{ minWidth: "10rem", marginRight: "1rem" }}>
                            <Card.Img variant="top" src="https://img.sonyunara.com/files/goods/67504/1607328307_0.jpg" style={{ objectFit: "contain" }} />
                            <Card.Body className="px-0">
                                <Card.Title>클로타탄원피스</Card.Title>
                                <Card.Text>구매자 : 30</Card.Text>
                            </Card.Body>
                        </Col>
                        <Col as={Card} style={{ minWidth: "10rem", marginRight: "1rem" }}>
                            <Card.Img variant="top" src="https://img.sonyunara.com/files/goods/67504/1607328307_0.jpg" style={{ objectFit: "contain" }} />
                            <Card.Body className="px-0">
                                <Card.Title>클로타탄원피스</Card.Title>
                                <Card.Text>구매자 : 30</Card.Text>
                            </Card.Body>
                        </Col>
                        <Col as={Card} style={{ minWidth: "10rem", marginRight: "1rem" }}>
                            <Card.Img variant="top" src="https://img.sonyunara.com/files/goods/67504/1607328307_0.jpg" style={{ objectFit: "contain" }} />
                            <Card.Body className="px-0">
                                <Card.Title>클로타탄원피스</Card.Title>
                                <Card.Text>구매자 : 30</Card.Text>
                            </Card.Body>
                        </Col>
                    </Row>
                </Col>
            </Row>
        </div>
    )
}

export default Product