Product.js 11.2 KB
Newer Older
1
import axios from 'axios';
kusang96's avatar
kusang96 committed
2
import React, { useState, useEffect, useRef } from 'react';
박상호's avatar
박상호 committed
3
import { Row, Col, Form, Card, Button, Image } 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)
박상호's avatar
박상호 committed
17

박상호's avatar
0126    
박상호 committed
18
19
    const replace = product.description.replaceAll('{\n\n}', '<br />')
    console.log("replaceALL Description= ", replace)
Jiwon Yoon's avatar
Jiwon Yoon committed
20

21
    useEffect(() => {
Jiwon Yoon's avatar
Jiwon Yoon committed
22
        if (size && color) {
23
            pushOptions()
Jiwon Yoon's avatar
Jiwon Yoon committed
24
            console.log(cart)
Jiwon Yoon's avatar
Jiwon Yoon committed
25
        }
Jiwon Yoon's avatar
Jiwon Yoon committed
26
    }, [size, color])
kusang96's avatar
kusang96 committed
27
28
29
30
31
32

    function handleClick(e) {
        const box = e.target.parentNode.parentNode
        box.style.display = "none"
    }

33
    function pushOptions() {
박상호's avatar
박상호 committed
34
        setCart([...cart, { color, size, productId: product.id, count: 1 }])
35
36
        selected.sizes = false
        selected.colors = false
37
        console.log(product)
Jiwon Yoon's avatar
Jiwon Yoon committed
38
39
40
        setColor("")
        setSize("")
        setPrice(product.price + price)
41
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
42

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

Jiwon Yoon's avatar
Jiwon Yoon committed
54
    function deleteOption(e) {
kusang96's avatar
kusang96 committed
55
        e.preventDefault()
56
        let preprice = 0
Jiwon Yoon's avatar
Jiwon Yoon committed
57
        const asd = cart.filter((el) => el.color !== e.target.id || el.size !== e.target.name)
박상호's avatar
박상호 committed
58
59
        asd.map((el) => {
            preprice = preprice + el.count * product.price
60
        })
Jiwon Yoon's avatar
Jiwon Yoon committed
61
        setCart(asd)
62
        setPrice(Number(preprice))
kusang96's avatar
kusang96 committed
63
64
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
65
    function handleCount(e) {
66
67
68
        const addCount = cart.map((el) => {
            if (el.color !== e.target.id || el.size !== e.target.name) {
                return { ...el }
Jiwon Yoon's avatar
Jiwon Yoon committed
69
            } else {
70
                return { ...el, count: e.target.value }
Jiwon Yoon's avatar
Jiwon Yoon committed
71
72
            }
        })
73
        let preprice = 0
박상호's avatar
박상호 committed
74
75
        addCount.map((el) => {
            preprice = preprice + el.count * product.price
76
77
        })
        setPrice(Number(preprice))
Jiwon Yoon's avatar
Jiwon Yoon committed
78
        setCart(addCount)
Jiwon Yoon's avatar
Jiwon Yoon committed
79
        setCount(e.value)
kusang96's avatar
kusang96 committed
80
81
    }

82
    async function addCart() {
Jiwon Yoon's avatar
Jiwon Yoon committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        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'),
                    products: cart
                })
                console.log(response)
            } catch (error) {
                catchErrors(error, setError)
            }
        } else {
            alert("로그인을 해주세요.")
            return <Redirect to='/login' />
99
100
101
        }
    }

kusang96's avatar
kusang96 committed
102
103
104

    return (
        <div>
105
            {console.log(cart)}
kusang96's avatar
kusang96 committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
            <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
121
                    <img src={`/images/${product.main_img}`} style={{ objectFit: "contain", width: "100%" }} />
kusang96's avatar
kusang96 committed
122
123
                </Col>
                <Col sm={11} md={4} className="align-middle mt-4">
kusang96's avatar
0115    
kusang96 committed
124
125
                    <h3 className="mb-4">{product.name}</h3>
                    <h5 className="mb-4">가격 : {product.price}</h5>
kusang96's avatar
kusang96 committed
126
127
128
                    <Form style={{ borderBottom: "1px solid" }}>
                        <Form.Group style={{ borderBottom: "1px solid", paddingBottom: "2rem" }}>
                            <Form.Label>색상</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
129
                            <Form.Control as="select" className="mb-2" name="colors" value={color} defaultValue="옵션 선택" onChange={handleChange}>
kusang96's avatar
kusang96 committed
130
                                <option>옵션선택</option>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
131
132
133
                                {product.colors.map((e) => (
                                    <option>{e}</option>
                                ))}
kusang96's avatar
kusang96 committed
134
135
                            </Form.Control>
                            <Form.Label>사이즈</Form.Label>
Jiwon Yoon's avatar
Jiwon Yoon committed
136
                            <Form.Control as="select" className="mb-2" name="sizes" value={size} defaultValue="옵션 선택" onChange={handleChange}>
kusang96's avatar
kusang96 committed
137
                                <option>옵션선택</option>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
138
139
140
                                {product.sizes.map((e) => (
                                    <option>{e}</option>
                                ))}
kusang96's avatar
kusang96 committed
141
142
                            </Form.Control>
                        </Form.Group>
Jiwon Yoon's avatar
Jiwon Yoon committed
143
                        {cart.map((e) => (
144
145
146
                            <Row className="mx-1">
                                <Col xs={6}>{e.color}/{e.size}</Col>
                                <Col xs={4} className="text-right" >
Jiwon Yoon's avatar
Jiwon Yoon committed
147
                                    <input type='number' id={e.color} name={e.size} onChange={handleCount} value={count} style={{ width: '3rem' }} className="text-center" />
148
149
150
151
152
                                </Col>
                                <Col xs={2} className="text-right">
                                    <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="align-middle" />
                                </Col>
                            </Row>
Jiwon Yoon's avatar
Jiwon Yoon committed
153

154
                        ))}
kusang96's avatar
kusang96 committed
155
156
                        <Row className="justify-content-between mx-0 my-3" style={{ width: "100%" }}>
                            <Col> 금액</Col>
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
157
                            <Col className="text-right">{price}</Col>
kusang96's avatar
kusang96 committed
158
159
                        </Row>
                        <Row className="justify-content-between mx-0 my-3" style={{ width: "100%" }}>
Jiwon Yoon's avatar
Jiwon Yoon committed
160
                            <Button type='button' onClick={addCart} style={{ width: "49%" }}>장바구니</Button>
kusang96's avatar
kusang96 committed
161
162
163
164
165
166
167
                            <Button style={{ width: "49%" }}>구매하기</Button>
                        </Row>
                    </Form>
                </Col>
            </Row>
            <Row className="justify-content-center mt-5 mx-0">
                <Col sm={11} md={8}>
박상호's avatar
박상호 committed
168
169
170
                    <h3 style={{ borderBottom: "1px solid #91877F", paddingBottom: "5px", marginBottom: "1em" }} className="p-3">
                        설명
                        </h3>
박상호's avatar
0126    
박상호 committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
                    <Col className='text-center' style={{ fontSize: '1px' }}>
                        <div className='p-2 text-center border' style={{ background: '#CDC5C2', width: '50%', margin: 'auto', fontSize: '3.5vmin' }} >
                            {product.name}
                        </div>
                        <Image src={`/images/${product.main_img}`} className='d-flex justify-content-center p-4' style={{ objectFit: "contain", maxWidth: "100%", margin: 'auto' }} />

                        <Card style={{ width: '70%', margin: 'auto' }} className='my-4' >
                            <Card.Header className='text-center' style={{ background: '#CDC5C2' }}>
                                <h5 className='m-0' style={{ whiteSpace: 'nowrap' }}> [ Description ]</h5>
                            </Card.Header>
                            <Card.Body className='text-center m-4' style={{ whiteSpace: "pre-line", background: '#F7F3F3', fontSize: '1vw' }}>
                                <small>{replace}</small>
                            </Card.Body>
                        </Card>
                        <Col className='p-5'>
                            <div className='border p-2' style={{ width: '60%', margin: 'auto', fontSize: '3.5vmin' }}>[ Detail Images ]</div>
                            <Image src={`/images/${product.detail_imgs}`} style={{ objectFit: "contain", maxWidth: "100%", margin: 'auto' }} className='p-4 d-flex justify-content-center' />

                        </Col>
박상호's avatar
박상호 committed
190
                    </Col>
kusang96's avatar
kusang96 committed
191
192
193
194
195
                </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" }}>회원님이 선호할만한 상품 추천
박상호's avatar
0120    
박상호 committed
196
                        <a className="close float-right" onClick={(e) => handleClick(e)} style={{ fontSize: "1rem", cursor: "pointer" }}>X</a>
kusang96's avatar
kusang96 committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
                    </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