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

Kim, Subin's avatar
checked    
Kim, Subin committed
20
21
22
23
    useEffect(() => {
        price()
    }, [cart])

Jiwon Yoon's avatar
Jiwon Yoon committed
24
25
26
    function plusNum(e) {
        const addCount = cart.map((el) => {
            if (el._id === e.target.name) {
kusang96's avatar
kusang96 committed
27
                return { ...el, count: el.count + 1 }
Jiwon Yoon's avatar
Jiwon Yoon committed
28
29
30
31
32
            } else {
                return { ...el }
            }
        })
        setCart(addCount)
33
    }
Kim, Subin's avatar
checked    
Kim, Subin committed
34

Jiwon Yoon's avatar
Jiwon Yoon committed
35
36
37
    function minusNum(e) {
        const addCount = cart.map((el) => {
            if (el._id === e.target.name) {
kusang96's avatar
kusang96 committed
38
                return { ...el, count: el.count - 1 }
Jiwon Yoon's avatar
Jiwon Yoon committed
39
40
41
42
43
44
45
            } else {
                return { ...el }
            }
        })
        setCart(addCount)
    }

Kim, Subin's avatar
checked    
Kim, Subin committed
46
    function price() {
Jiwon Yoon's avatar
Jiwon Yoon committed
47
        let price = 0
Kim, Subin's avatar
checked    
Kim, Subin committed
48
49
50
51
52
53
54
55
56
        const list = cart.filter((el) => el.checked === true)
        list.map((el) => {
            price = el.count * el.productId.price + price
        })
        setFinalPrice(price)
        setFinalCart(list)
    }

    function checkedCart(e) {
Jiwon Yoon's avatar
Jiwon Yoon committed
57
58
59
60
61
62
63
64
        const cartCheck = cart.map((el) => {
            if (el._id === e.target.name) {
                return { ...el, checked: !el.checked }
            } else {
                return { ...el }
            }
        })
        setCart(cartCheck)
65
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
66

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
67
68
    async function deleteCart(e) {
        try {
kusang96's avatar
kusang96 committed
69
            setError('')
Jiwon Yoon's avatar
Jiwon Yoon committed
70
71
72
73
            const response = await axios.post('/api/cart/deletecart', {
                userId: user,
                cartId: e.target.name
            })
Jiwon Yoon's avatar
Jiwon Yoon committed
74
            setCart(response.data.products)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
75
76
77
        } catch (error) {
            catchErrors(error, setError)
        }
78
79
    }

Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
80
81
    async function getCart() {
        try {
Jiwon Yoon's avatar
Jiwon Yoon committed
82
            setError('')
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
83
            const response = await axios.get(`/api/cart/showcart/${user}`)
Jiwon Yoon's avatar
Jiwon Yoon committed
84
            const addChecked = response.data.map((el) => {
Kim, Subin's avatar
checked    
Kim, Subin committed
85
                return { ...el, checked: true }
Jiwon Yoon's avatar
0113    
Jiwon Yoon committed
86
            })
Jiwon Yoon's avatar
Jiwon Yoon committed
87
            setCart(addChecked)
Jiwon Yoon's avatar
qwr    
Jiwon Yoon committed
88
89
90
91
        } catch (error) {
            catchErrors(error, setError)
        }
    }
92

kusang96's avatar
kusang96 committed
93
    function putCheckedCart() {
Jiwon Yoon's avatar
Jiwon Yoon committed
94
95
        try {
            setError('')
kusang96's avatar
kusang96 committed
96
97
            const response = axios.post(`/api/cart/changecart`, {
                userId: user,
Jiwon Yoon's avatar
Jiwon Yoon committed
98
99
100
101
102
103
104
                products: cart
            })
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Kim, Subin's avatar
Kim, Subin committed
105
    return (
Kim, Subin's avatar
margin    
Kim, Subin committed
106
        <Container className="justify-content-center mb-5">
kusang96's avatar
card    
kusang96 committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
            <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>
127
                </div>
kusang96's avatar
card    
kusang96 committed
128
129
130
131
132
133
134
135
            </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
136
137
138
139
    )
}

export default ShoppingCart