Product.js 9.36 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
const INIT_PRODUCT = {
    pro_name: '스키니진',
    price: 12000,
9
    count: 1,
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
10
11
    main_category: 'PANTS',
    sub_category: ['SKINNY JEANS'],
12
    sizes: ['L', 'M'],
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
13
    colors: ['연청', '진청'],
14
15
    main_image: "a8f4d63ead77717f940a2b27deb707a6",
    productId:"5ffda03428faf35de8319360"
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
16
}
17
const preCart = []
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
18

kusang96's avatar
kusang96 committed
19
function Product() {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
20
21
    const [product, setProduct] = useState(INIT_PRODUCT)
    const [cart, setCart] = useState(INIT_PRODUCT)
22
    const [error, setError] = useState('')
23
    const [selected, setSelected] = useState({ sizes: false, colors: false })
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
24
    const [price, setPrice] = useState(0)
25
26
27
28
29
30
31

    useEffect(() => {
        if (selected.sizes === true && selected.colors === true) {
            pushOptions()
            console.log(preCart)
        }
    }, [cart])
kusang96's avatar
kusang96 committed
32

Jiwon Yoon's avatar
Jiwon Yoon committed
33

kusang96's avatar
kusang96 committed
34
35
36
37
38
    function handleClick(e) {
        const box = e.target.parentNode.parentNode
        box.style.display = "none"
    }

39
40
41
42
    function pushOptions() {
        preCart.push(cart)
        selected.sizes = false
        selected.colors = false
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
43
        setPrice(product.price+price)
44
    }
kusang96's avatar
kusang96 committed
45
46
    function handleChange(e) {
        const { name, value } = e.target
47
48
49
50
51
52
53
54
55
        if (e.target.name === "sizes") {
            setCart({ ...cart, [name]: value })
            selected.sizes = true
        } else if (e.target.name === "colors") {
            setCart({ ...cart, [name]: value })
            selected.colors = true
        }
        // setCart({ ...cart, [name]: value })

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
56
        // handleCreate()
kusang96's avatar
kusang96 committed
57
58
59
60
61
62
63
64
65
    }

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

    function handleCreate() {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
66
        console.log("실행", "cart=", product)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        // 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
90
91
    }

92
    async function addCart() {
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
93
        // preCart(color, size, count), productId(productlist에서 props), userId(로컬) 를 보내줌
94
        try {
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
95
            setError('')
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
96
            const response = await axios.put('/api/cart/addcart', {
97
98
99
                userId: localStorage.getItem('loginStatus'),
                productId: "a8f4d63ead77717f940a2b27deb707a6",
                products: preCart
100
101
            })
            console.log(response)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
102
103
        } catch (error) {
            catchErrors(error, setError)
104
105
106
        }
    }

kusang96's avatar
kusang96 committed
107
108
    return (
        <div>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
109
            {console.log(cart)}
kusang96's avatar
kusang96 committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
            <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}>
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
125
                    <img src="/images/a8f4d63ead77717f940a2b27deb707a6" style={{ objectFit: "contain", width: "100%" }} />
kusang96's avatar
kusang96 committed
126
127
                </Col>
                <Col sm={11} md={4} className="align-middle mt-4">
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
128
129
                    <h3 className="mb-4">스키니진</h3>
                    <h5 className="mb-4">가격 : 12000</h5>
kusang96's avatar
kusang96 committed
130
131
132
                    <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
133
                            <Form.Control as="select" className="mb-2" name="colors" defaultValue="옵션 선택" onChange={handleChange}>
kusang96's avatar
kusang96 committed
134
                                <option>옵션선택</option>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
135
136
137
                                {product.colors.map((e) => (
                                    <option>{e}</option>
                                ))}
kusang96's avatar
kusang96 committed
138
139
                            </Form.Control>
                            <Form.Label>사이즈</Form.Label>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
140
                            <Form.Control as="select" className="mb-2" name="sizes" defaultValue="옵션 선택" onChange={handleChange}>
kusang96's avatar
kusang96 committed
141
                                <option>옵션선택</option>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
142
143
144
                                {product.sizes.map((e) => (
                                    <option>{e}</option>
                                ))}
kusang96's avatar
kusang96 committed
145
146
                            </Form.Control>
                        </Form.Group>
147
148
149
                        {preCart.map((e) => (
                            <div>{e.colors}/{e.sizes}</div>
                        ))}
kusang96's avatar
kusang96 committed
150
151
                        <Row className="justify-content-between mx-0 my-3" style={{ width: "100%" }}>
                            <Col> 금액</Col>
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
152
                            <Col className="text-right">{price}</Col>
kusang96's avatar
kusang96 committed
153
154
                        </Row>
                        <Row className="justify-content-between mx-0 my-3" style={{ width: "100%" }}>
155
                            <Button onClick={addCart} style={{ width: "49%" }}>장바구니</Button>
kusang96's avatar
kusang96 committed
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
                            <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