ShoppingCart.js 3.49 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

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
9
10
11
12
13
14
15
16
17
18
const INIT_PRODUCT = [{
    pro_name: '체크셔츠',
    price: 21000,
    count: 1,
    main_category: 'TOP',
    sub_category: ['SHIRT'],
    sizes: ['XL', 'L', 'M', 'S'],
    colors: ['베이지', '블랙', '카키'],
    main_image: 'e46b641ae11e6568c86f689d3dce7748'
}]
Kim, Subin's avatar
Kim, Subin committed
19
20

function ShoppingCart() {
21
    const [num, setNum] = useState(0)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
22
23
24
25
26
27
28
    const [error, setError] = useState('')
    const [cart, setCart] = useState(INIT_PRODUCT)
    const user = isAuthenticated()

    useEffect(() => {
        getCart()
    }, [user])
29
30
31
32
33
34
35
36
37
38
39
40
41

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

        }
    }
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
42
    async function deleteCart(e) {
43
        //장바구니 DB에서 해당 항목 삭제 
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
44
45
46
47
48
49
50
51
        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)
        }

52
53
54
        console.log('카트에 담긴 항목을 삭제했습니다.')
    }

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
55
56
57
58
59
60
61
62
63
64
    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)
        }
    }
65

Kim, Subin's avatar
Kim, Subin committed
66
67
    return (
        <div>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
68
69
70
            {/* {getCart()} */}
            {/* {console.log(user)} */}
            {/* {console.log(cart)} */}
71
            <Container className="justify-content-center">
Jiwon Yoon's avatar
Jiwon Yoon committed
72
                <h3 className="my-5 font-weight-bold text-center">장바구니</h3>
73
                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
74
                    <h4 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>주문상품정보</h4>
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
75
76
                    <CartCard cart={cart} deleteCart={deleteCart} minusNum={minusNum} plusNum={plusNum} num={num} />

77
                </div>
78
                <div className="p-5 m-5" style={{ background: '#F7F3F3' }}>
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
                    <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
94
                    <Button className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} href="/payment" block>결제하기</Button>
95
96
97
                </div>
            </Container>

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

export default ShoppingCart