import React, { useState, useEffect, useRef } from 'react'; import { Link, Redirect } from 'react-router-dom'; import { Card, Button, Container, Row, Col } from 'react-bootstrap'; import axios from 'axios'; import catchErrors from '../utils/catchErrors'; import { isAuthenticated } from '../utils/auth' import CartCard from '../Components/CartCard'; function ShoppingCart() { const [num, setNum] = useState() const [error, setError] = useState('') const [cart, setCart] = useState([]) const [finalPrice, setFinalPrice] = useState(0) const user = isAuthenticated() useEffect(() => { getCart() }, [user]) function plusNum() { num = num + 1 setNum(num + 1) } function minusNum() { if (num === 0) { setNum(0) } else { setNum(num - 1) } } async function deleteCart(e) { //장바구니 DB에서 해당 항목 삭제 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) } console.log('카트에 담긴 항목을 삭제했습니다.') } async function getCart() { try { const response = await axios.get(`/api/cart/showcart/${user}`) console.log(response.data) setCart(response.data) const finalPrices = response.data.map((e) => { return e.count * e.productId.price }) setFinalPrice(finalPrices.reduce((a, b) => (a + b))) } catch (error) { catchErrors(error, setError) } } return (

장바구니

주문상품정보

{cart.length >0 ? :
장바구니에 담긴 상품이 없습니다.
}
  • 총 상품금액 {finalPrice}원
  • 배송비 2500원
결제금액{finalPrice + 2500}원
) } export default ShoppingCart