ShoppingCart.js 4.46 KB
Newer Older
kusang96's avatar
kusang96 committed
1
2
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
kusang96's avatar
card    
kusang96 committed
3
import ListCard from '../Components/ListCard';
4
import axios from 'axios';
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
5
import catchErrors from '../utils/catchErrors';
kusang96's avatar
kusang96 committed
6
import { isAuthenticated } from '../utils/auth';
kusang96's avatar
kusang96 committed
7
import { Button, Container } from 'react-bootstrap';
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
18
    const user = isAuthenticated()

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

Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
22
    function plusNum(e) {
        const addCount = cart.map((el) => {
            if (el._id === e.target.name) {
kusang96's avatar
kusang96 committed
23
                return { ...el, count: el.count + 1 }
Jiwon Yoon's avatar
Jiwon Yoon committed
24
25
26
27
28
            } else {
                return { ...el }
            }
        })
        setCart(addCount)
29
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
30
31
32
    function minusNum(e) {
        const addCount = cart.map((el) => {
            if (el._id === e.target.name) {
kusang96's avatar
kusang96 committed
33
                return { ...el, count: el.count - 1 }
Jiwon Yoon's avatar
Jiwon Yoon committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
            } 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 }
            }
        })
kusang96's avatar
kusang96 committed
50
51
52
        const list = cartCheck.filter((el) => el.checked === true)
        list.map((el) => {
            price = el.count * el.productId.price + price
Jiwon Yoon's avatar
Jiwon Yoon committed
53
54
55
        })
        setFinalPrice(price)
        setCart(cartCheck)
kusang96's avatar
kusang96 committed
56
        setFinalCart(list)
57
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
58

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
59
60
    async function deleteCart(e) {
        try {
kusang96's avatar
kusang96 committed
61
            setError('')
Jiwon Yoon's avatar
Jiwon Yoon committed
62
63
64
65
            const response = await axios.post('/api/cart/deletecart', {
                userId: user,
                cartId: e.target.name
            })
Jiwon Yoon's avatar
Jiwon Yoon committed
66
            setCart(response.data.products)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
67
68
69
        } catch (error) {
            catchErrors(error, setError)
        }
70
71
    }

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

kusang96's avatar
kusang96 committed
85
    function putCheckedCart() {
Jiwon Yoon's avatar
Jiwon Yoon committed
86
87
        try {
            setError('')
kusang96's avatar
kusang96 committed
88
89
            const response = axios.post(`/api/cart/changecart`, {
                userId: user,
Jiwon Yoon's avatar
Jiwon Yoon committed
90
91
92
93
94
95
96
                products: cart
            })
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
97
    return (
Kim, Subin's avatar
margin    
Kim, Subin committed
98
        <Container className="justify-content-center mb-5">
kusang96's avatar
card    
kusang96 committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
            <h1 className="my-5 font-weight-bold text-center">장바구니</h1>
            <div>
                <h4 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>주문상품정보</h4>
                {cart.length > 0
                    ? <ListCard cart={cart} deleteCart={deleteCart} minusNum={minusNum} plusNum={plusNum} checkedCart={checkedCart} status={'cart'} />
                    : <div className="text-center my-5">장바구니에 담긴 상품이 없습니다.</div>}
            </div>
            <div className="p-5 m-3" style={{ background: '#F7F3F3' }}>
                <ul className="pl-0" style={{ listStyle: 'none' }}>
                    <li>
                        <span className="text-secondary"> 상품금액</span>
                        <span className="text-secondary float-right">{finalPrice}</span>
                    </li>
                    <li>
                        <span className="text-secondary">배송비</span>
                        <span className="text-secondary float-right">2500</span>
                    </li>
                </ul>
                <div className="my-1 pt-2 border-top font-weight-bold">
                    결제금액<span className="float-right">{finalPrice + 2500}</span>
119
                </div>
kusang96's avatar
card    
kusang96 committed
120
121
122
123
124
125
126
127
            </div>
            <div className="text-center">
                <Button as={Link} to={{
                    pathname: `/payment`,
                    state: finalCart
                }} className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} onClick={putCheckedCart} block>결제하기</Button>
            </div>
        </Container>
Kim, Subin's avatar
Kim, Subin committed
128
129
130
131
    )
}

export default ShoppingCart