ShoppingCart.js 4.52 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import React, { useState, useEffect, useRef } from 'react';
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { Link, Redirect } from 'react-router-dom';
3
import { Card, Button, Container, Row, Col } from 'react-bootstrap';
4
import axios from 'axios';
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
5
6
7
import catchErrors from '../utils/catchErrors';
import { isAuthenticated } from '../utils/auth'
import CartCard from '../Components/CartCard';
8

Kim, Subin's avatar
Kim, Subin committed
9
function ShoppingCart() {
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
10
    const [error, setError] = useState('')
Jiwon Yoon's avatar
Jiwon Yoon committed
11
    const [cart, setCart] = useState([])
Jiwon Yoon's avatar
Jiwon Yoon committed
12
    const [finalCart, setFinalCart] = useState([])
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
13
    const [finalPrice, setFinalPrice] = useState(0)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
14
15
16
17
    const user = isAuthenticated()

    useEffect(() => {
        getCart()
Jiwon Yoon's avatar
Jiwon Yoon committed
18
        console.log(cart)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
19
    }, [user])
20

Jiwon Yoon's avatar
Jiwon Yoon committed
21
22
23
24
25
26
27
28
29
    function plusNum(e) {
        const addCount = cart.map((el) => {
            if (el._id === e.target.name) {
                return { ...el, count: el.count+1}
            } else {
                return { ...el }
            }
        })
        setCart(addCount)
30
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    function minusNum(e) {
        const addCount = cart.map((el) => {
            if (el._id === e.target.name) {
                return { ...el, count: el.count-1 }
            } else {
                return { ...el }
            }
        })
        setCart(addCount)
    }

    function checkedCart(e) {
        let price = 0
        const cartCheck = cart.map((el) => {
            if (el._id === e.target.name) {
                return { ...el, checked: !el.checked }
            } else {
                return { ...el }
            }
        })
        const asd = cartCheck.filter((el) => el.checked === true)
        asd.map((el)=>{
            price = el.count*el.productId.price  + price
        })
        setFinalPrice(price)
        setCart(cartCheck)
        setFinalCart(asd)
58
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
59

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
60
    async function deleteCart(e) {
61
        //장바구니 DB에서 해당 항목 삭제 
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
62
63
        console.log(e.target.name)
        try {
Jiwon Yoon's avatar
Jiwon Yoon committed
64
65
66
67
            const response = await axios.post('/api/cart/deletecart', {
                userId: user,
                cartId: e.target.name
            })
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
68
            console.log(response.data)
Jiwon Yoon's avatar
Jiwon Yoon committed
69
            setCart(response.data.products)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
70
71
72
        } catch (error) {
            catchErrors(error, setError)
        }
73
74
75
        console.log('카트에 담긴 항목을 삭제했습니다.')
    }

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
76
77
78
    async function getCart() {
        try {
            const response = await axios.get(`/api/cart/showcart/${user}`)
Jiwon Yoon's avatar
Jiwon Yoon committed
79
80
            const addChecked = response.data.map((el) => {
                    return { ...el, checked: false }
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
81
            })
Jiwon Yoon's avatar
Jiwon Yoon committed
82
83
            console.log("addchecked=",addChecked)
            setCart(addChecked)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
84
85
86
87
        } catch (error) {
            catchErrors(error, setError)
        }
    }
88

Kim, Subin's avatar
Kim, Subin committed
89
90
    return (
        <div>
91
            <Container className="justify-content-center">
Jiwon Yoon's avatar
Jiwon Yoon committed
92
                <h1 className="my-5 font-weight-bold text-center">장바구니</h1>
93
                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
94
                    <h4 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>주문상품정보</h4>
Jiwon Yoon's avatar
Jiwon Yoon committed
95
96
                    {cart.length > 0
                        ? <CartCard cart={cart} deleteCart={deleteCart} minusNum={minusNum} plusNum={plusNum} checkedCart={checkedCart} />
Jiwon Yoon's avatar
Jiwon Yoon committed
97
                        : <div className="text-center my-5">장바구니에 담긴 상품이 없습니다.</div>}
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
98

99
                </div>
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
100
                <div className="p-5 m-3" style={{ background: '#F7F3F3' }}>
101
102
103
                    <ul className="pl-0" style={{ listStyle: 'none' }}>
                        <li>
                            <span className="text-secondary"> 상품금액</span>
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
104
                            <span className="text-secondary float-right">{finalPrice}</span>
105
106
107
                        </li>
                        <li>
                            <span className="text-secondary">배송비</span>
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
108
                            <span className="text-secondary float-right">2500</span>
109
110
111
                        </li>
                    </ul>
                    <div className="my-1 pt-2 border-top font-weight-bold">
Jiwon Yoon's avatar
Jiwon Yoon committed
112
                        결제금액<span className="float-right">{finalPrice + 2500}</span>
113
114
115
                    </div>
                </div>
                <div className="text-center">
Jiwon Yoon's avatar
Jiwon Yoon committed
116
                    <Button as={Link} to={{
Jiwon Yoon's avatar
Jiwon Yoon committed
117
118
119
                        pathname: `/payment`,
                        state: finalCart 
                    }} className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} block>결제하기</Button>
120
121
122
                </div>
            </Container>

Kim, Subin's avatar
Kim, Subin committed
123
124
125
126
127
        </div>
    )
}

export default ShoppingCart