Product.js 9.56 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';
Jiwon Yoon's avatar
Jiwon Yoon committed
4
import { Redirect } from 'react-router-dom';
5
import catchErrors from '../utils/catchErrors';
kusang96's avatar
kusang96 committed
6
7


8
function Product({ match, location }) {
kusang96's avatar
0115    
kusang96 committed
9
    const [product, setProduct] = useState(location.state)
Jiwon Yoon's avatar
Jiwon Yoon committed
10
11
12
    const [color, setColor] = useState("")
    const [size, setSize] = useState("")
    const [cart, setCart] = useState([])
13
    const [error, setError] = useState('')
14
    const [selected, setSelected] = useState({ sizes: false, colors: false })
Jiwon Yoon's avatar
Jiwon Yoon committed
15
    const [count, setCount] = useState(1)
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
16
    const [price, setPrice] = useState(0)
Jiwon Yoon's avatar
Jiwon Yoon committed
17

18
    useEffect(() => {
Jiwon Yoon's avatar
Jiwon Yoon committed
19
        if (size && color) {
20
            pushOptions()
Jiwon Yoon's avatar
Jiwon Yoon committed
21
            console.log(cart)
Jiwon Yoon's avatar
Jiwon Yoon committed
22
        }
Jiwon Yoon's avatar
Jiwon Yoon committed
23
    }, [size, color])
kusang96's avatar
kusang96 committed
24

Jiwon Yoon's avatar
Jiwon Yoon committed
25

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

31
    function pushOptions() {
Jiwon Yoon's avatar
Jiwon Yoon committed
32
        setCart([...cart, { color, size, productId: product.id }])
33
34
        selected.sizes = false
        selected.colors = false
Jiwon Yoon's avatar
Jiwon Yoon committed
35
36
37
        setColor("")
        setSize("")
        setPrice(product.price + price)
38
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
39

kusang96's avatar
kusang96 committed
40
41
    function handleChange(e) {
        const { name, value } = e.target
Jiwon Yoon's avatar
Jiwon Yoon committed
42
43
44
        if (name === "sizes") {
            // setPreCart({ ...preCart, [name]: value })
            setSize(value)
45
            selected.sizes = true
Jiwon Yoon's avatar
Jiwon Yoon committed
46
47
48
        } else if (name === "colors") {
            // setPreCart({ ...preCart, [name]: value })
            setColor(value)
49
50
            selected.colors = true
        }
kusang96's avatar
kusang96 committed
51
52
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
53
    function deleteOption(e) {
kusang96's avatar
kusang96 committed
54
        e.preventDefault()
Jiwon Yoon's avatar
Jiwon Yoon committed
55
56
        const asd = cart.filter((el) => el.color !== e.target.id || el.size !== e.target.name)
        setCart(asd)
kusang96's avatar
kusang96 committed
57
58
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    function handleCount(e) {
        e.preventDefault()
        // const asd = cart.filter((el) => el.color !== e.target.id || el.size !== e.target.name)
        const asd= cart.map((el)=>{
            if(el.color !== e.target.id || el.size !== e.target.name){
                return {el}
            } else {
                return {...el, count : e.target.value}
            }
        })
        // const index = product["sub_category"].findIndex((item)=>{return item === e.target.name})
        // product["sub_category"].splice(index, 1)
        setCart(asd)
        setCount(e.value)
kusang96's avatar
kusang96 committed
73
74
    }

75
    async function addCart() {
Jiwon Yoon's avatar
Jiwon Yoon committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        console.log(cart)
        if (localStorage.getItem('id')) {
            // preCart(color, size, count), productId(productlist에서 props), userId(로컬) 를 보내줌
            try {
                setError('')
                const response = await axios.put('/api/cart/addcart', {
                    userId: localStorage.getItem('id'),
                    // productId: product.id,
                    products: cart
                })
                console.log(response)
            } catch (error) {
                catchErrors(error, setError)
            }
        } else {
            alert("로그인을 해주세요.")
            return <Redirect to='/login' />
93
94
95
        }
    }

kusang96's avatar
kusang96 committed
96
97
98

    return (
        <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
99
            {/* {console.log("match=", match.params, "location=", location.state, "product=", product)} */}
kusang96's avatar
kusang96 committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
            <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
asdf    
Jiwon Yoon committed
115
                    <img src={`/images/${product.main_img}`} style={{ objectFit: "contain", width: "100%" }} />
kusang96's avatar
kusang96 committed
116
117
                </Col>
                <Col sm={11} md={4} className="align-middle mt-4">
kusang96's avatar
0115    
kusang96 committed
118
119
                    <h3 className="mb-4">{product.name}</h3>
                    <h5 className="mb-4">가격 : {product.price}</h5>
kusang96's avatar
kusang96 committed
120
121
122
                    <Form style={{ borderBottom: "1px solid" }}>
                        <Form.Group style={{ borderBottom: "1px solid", paddingBottom: "2rem" }}>
                            <Form.Label>색상</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
123
                            <Form.Control as="select" className="mb-2" name="colors" value={color} defaultValue="옵션 선택" onChange={handleChange}>
kusang96's avatar
kusang96 committed
124
                                <option>옵션선택</option>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
125
126
127
                                {product.colors.map((e) => (
                                    <option>{e}</option>
                                ))}
kusang96's avatar
kusang96 committed
128
129
                            </Form.Control>
                            <Form.Label>사이즈</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
130
                            <Form.Control as="select" className="mb-2" name="sizes" value={size} defaultValue="옵션 선택" onChange={handleChange}>
kusang96's avatar
kusang96 committed
131
                                <option>옵션선택</option>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
132
133
134
                                {product.sizes.map((e) => (
                                    <option>{e}</option>
                                ))}
kusang96's avatar
kusang96 committed
135
136
                            </Form.Control>
                        </Form.Group>
Jiwon Yoon's avatar
Jiwon Yoon committed
137
138
139
140
141
142
143
144
145
146
                        {cart.map((e) => (
                            <div>
                                <span>{e.color}/{e.size}</span>
                                <input onClick={deleteOption} id={e.color} name={e.size} type="image" alt="삭제버튼" src="https://img.icons8.com/fluent-systems-regular/24/000000/close-window.png" className="float-right align-middle" />
                                <span>{e.price}</span>
                                <span className="float-right mx-2">
                                    <input type='number' id={e.color} name={e.size} onChange={handleCount} value={count} style={{ width: '3rem' }} className="text-center" />
                                </span>
                            </div>

147
                        ))}
kusang96's avatar
kusang96 committed
148
149
                        <Row className="justify-content-between mx-0 my-3" style={{ width: "100%" }}>
                            <Col> 금액</Col>
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
150
                            <Col className="text-right">{price}</Col>
kusang96's avatar
kusang96 committed
151
152
                        </Row>
                        <Row className="justify-content-between mx-0 my-3" style={{ width: "100%" }}>
Jiwon Yoon's avatar
Jiwon Yoon committed
153
                            <Button type='button' onClick={addCart} style={{ width: "49%" }}>장바구니</Button>
kusang96's avatar
kusang96 committed
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                            <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