Product.js 8.25 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
6

function Product() {
7
    const [select, setSelect] = useState({ color: "", size: "" })
kusang96's avatar
kusang96 committed
8
    const [cart, setCart] = useState()
9
    const [error, setError] = useState('')
kusang96's avatar
kusang96 committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

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

    function handleChange(e) {
        const { name, value } = e.target
        setSelect({ ...select, [name]: value })
    }

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

    function handleCreate() {
28
        console.log("실행", "cart=", cart)
kusang96's avatar
kusang96 committed
29
30
31
32
33
34
        if (cart !== undefined) {
            if (cart.color !== "") {
                const list = document.getElementById('list')
                list.style.borderBottom = "1px solid"
                const shopping = document.createElement('div')
                shopping.className = "d-flex justify-content-between my-2"
35
                shopping.innerHTML = `${cart.color} / ${cart.size}
kusang96's avatar
kusang96 committed
36
37
38
39
40
41
42
43
44
45
46
                <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)
            }
        }
    }

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    async function addCart() {
        // color, size, count, productObjectId(productlist에서 props), userId(로컬) 를 보내줌
        try {
            // setError('')
            const response = await axios.post('/api/addcart', {
                userId: "jiwon5393",
                productObjectId: "5ff7fd63d41cae4ecce51dd1",
                color: "red",
                size: "free",
                count: "1"
            })
            console.log(response)
        }catch(error){
            // catchErrors(error, setError)
        }

        
        alert("상품등록이 완료되었습니다.")
    }

kusang96's avatar
kusang96 committed
67
68
    useEffect(() => {
        if (Object.keys(select).length == 2) {
69
            setCart({ ...select })
kusang96's avatar
kusang96 committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
            setSelect({})
        }
    }, [select])

    useEffect(() => {
        handleCreate()
    }, [cart])

    return (
        <div>
            <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>
                            <Form.Control as="select" className="mb-2" name="color" defaultValue="옵션 선택" onChange={handleChange}>
                                <option>옵션선택</option>
                                <option>브릭레드(스트랩포함)</option>
                                <option>베이지(스트랩포함)</option>
                                <option>블랙(스트랩포함)</option>
                            </Form.Control>
                            <Form.Label>사이즈</Form.Label>
                            <Form.Control as="select" className="mb-2" name="size" defaultValue="옵션 선택" onChange={handleChange}>
                                <option>옵션선택</option>
                                <option>M</option>
                                <option>L</option>
                                <option>XL</option>
                                <option>FREE</option>
                            </Form.Control>
                        </Form.Group>
                        <div id="list"></div>
                        <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%" }}>
124
                            <Button onClick={addCart} style={{ width: "49%" }}>장바구니</Button>
kusang96's avatar
kusang96 committed
125
126
127
128
129
130
131
132
133
134
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
                            <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