ShoppingCart.js 3.22 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';
이재연's avatar
이재연 committed
8

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

    useEffect(() => {
        getCart()
    }, [user])
18
19
20
21
22
23
24
25
26
27
28
29
30

    function plusNum() {
        setNum(num + 1)
    }
    function minusNum() {
        if (num === 0) {
            setNum(0)
        }
        else {
            setNum(num - 1)

        }
    }
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
31
    async function deleteCart(e) {
32
        //장바구니 DB에서 해당 항목 삭제 
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
33
34
35
36
37
38
39
40
        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)
        }

41
42
43
        console.log('카트에 담긴 항목을 삭제했습니다.')
    }

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
44
45
46
47
48
49
50
51
52
53
    async function getCart() {
        // const userId= localStorage.getItem('loginStatus')
        try {
            const response = await axios.get(`/api/cart/showcart/${user}`)
            console.log(response.data)
            setCart(response.data)
        } catch (error) {
            catchErrors(error, setError)
        }
    }
54

Kim, Subin's avatar
Kim, Subin committed
55
56
    return (
        <div>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
57
58
59
            {/* {getCart()} */}
            {/* {console.log(user)} */}
            {/* {console.log(cart)} */}
60
            <Container className="justify-content-center">
Jiwon Yoon's avatar
Jiwon Yoon committed
61
                <h3 className="my-5 font-weight-bold text-center">장바구니</h3>
62
                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
63
                    <h4 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>주문상품정보</h4>
64
                    {cart?<CartCard cart={cart} deleteCart={deleteCart} minusNum={minusNum} plusNum={plusNum} num={num} />:<div></div>}
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
65

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

Kim, Subin's avatar
Kim, Subin committed
87
88
89
90
91
        </div>
    )
}

export default ShoppingCart