ShoppingCart.js 3.68 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
0113    
Jiwon Yoon committed
10
    const [num, setNum] = useState()
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
11
    const [error, setError] = useState('')
Jiwon Yoon's avatar
Jiwon Yoon committed
12
    const [cart, setCart] = 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
18
    const user = isAuthenticated()

    useEffect(() => {
        getCart()
    }, [user])
19
20

    function plusNum() {
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
21
        num = num + 1
22
23
24
25
26
27
28
29
30
31
        setNum(num + 1)
    }
    function minusNum() {
        if (num === 0) {
            setNum(0)
        }
        else {
            setNum(num - 1)
        }
    }
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
32
    async function deleteCart(e) {
33
        //장바구니 DB에서 해당 항목 삭제 
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
34
35
        console.log(e.target.name)
        try {
Jiwon Yoon's avatar
Jiwon Yoon committed
36
37
38
            const response = await axios.post('/api/cart/deletecart', { 
                userId : user,
                cartId: e.target.name })
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
39
            console.log(response.data)
Jiwon Yoon's avatar
Jiwon Yoon committed
40
            setCart(response.data.products)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
41
42
43
44
        } catch (error) {
            catchErrors(error, setError)
        }

45
46
47
        console.log('카트에 담긴 항목을 삭제했습니다.')
    }

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
48
49
50
51
52
    async function getCart() {
        try {
            const response = await axios.get(`/api/cart/showcart/${user}`)
            console.log(response.data)
            setCart(response.data)
Jiwon Yoon's avatar
Jiwon Yoon committed
53
            const finalPrices = response.data.map((e) => {
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
54
55
                return e.count * e.productId.price
            })
Jiwon Yoon's avatar
Jiwon Yoon committed
56
            setFinalPrice(finalPrices.reduce((a, b) => (a + b)))
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
57
58
59
60
        } catch (error) {
            catchErrors(error, setError)
        }
    }
61

Kim, Subin's avatar
Kim, Subin committed
62
63
    return (
        <div>
64
            <Container className="justify-content-center">
Jiwon Yoon's avatar
Jiwon Yoon committed
65
                <h1 className="my-5 font-weight-bold text-center">장바구니</h1>
66
                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
67
                    <h4 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>주문상품정보</h4>
Jiwon Yoon's avatar
Jiwon Yoon committed
68
69
70
                    {cart.length >0
                        ? <CartCard cart={cart} deleteCart={deleteCart} minusNum={minusNum} plusNum={plusNum} num={num} />
                        : <div className="text-center my-5">장바구니에 담긴 상품이 없습니다.</div>}
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
71

72
                </div>
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
73
                <div className="p-5 m-3" style={{ background: '#F7F3F3' }}>
74
75
76
                    <ul className="pl-0" style={{ listStyle: 'none' }}>
                        <li>
                            <span className="text-secondary"> 상품금액</span>
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
77
                            <span className="text-secondary float-right">{finalPrice}</span>
78
79
80
                        </li>
                        <li>
                            <span className="text-secondary">배송비</span>
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
81
                            <span className="text-secondary float-right">2500</span>
82
83
84
                        </li>
                    </ul>
                    <div className="my-1 pt-2 border-top font-weight-bold">
Jiwon Yoon's avatar
Jiwon Yoon committed
85
                        결제금액<span className="float-right">{finalPrice + 2500}</span>
86
87
88
                    </div>
                </div>
                <div className="text-center">
Jiwon Yoon's avatar
Jiwon Yoon committed
89
90
91
92
                    <Button as={Link} to={{
                            pathname: `/payment`,
                            state: { cart }
                        }} className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} block>결제하기</Button>
93
94
95
                </div>
            </Container>

Kim, Subin's avatar
Kim, Subin committed
96
97
98
99
100
        </div>
    )
}

export default ShoppingCart