ShoppingCart.js 3.33 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import React, { useState, useEffect, useRef } from 'react';
Kim, Subin's avatar
Kim, Subin committed
2
import { 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
10
11


function ShoppingCart() {
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
12
    const [num, setNum] = useState()
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
13
    const [error, setError] = useState('')
14
    const [cart, setCart] = useState()
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
15
    const [finalPrice, setFinalPrice] = useState(0)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
16
17
18
19
20
    const user = isAuthenticated()

    useEffect(() => {
        getCart()
    }, [user])
21
22

    function plusNum() {
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
23
24
        num = num + 1

25
26
27
28
29
30
31
32
33
34
35
        setNum(num + 1)
    }
    function minusNum() {
        if (num === 0) {
            setNum(0)
        }
        else {
            setNum(num - 1)

        }
    }
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
36
    async function deleteCart(e) {
37
        //장바구니 DB에서 해당 항목 삭제 
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
38
39
40
41
42
43
44
45
        console.log(e.target.name)
        try {
            const response = await axios.post('/api/cart/deletecart', { cartId: e.target.name })
            console.log(response.data)
        } catch (error) {
            catchErrors(error, setError)
        }

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

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

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

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

Kim, Subin's avatar
Kim, Subin committed
92
93
94
95
96
        </div>
    )
}

export default ShoppingCart