Skip to content

Commit a4e82cc

Browse files
committed
feat: 12.CartUseReducer: back to first setup
1 parent dd85b34 commit a4e82cc

File tree

4 files changed

+8
-60
lines changed

4 files changed

+8
-60
lines changed

src/projects/12.CartUseReducer/CartContainer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import CartItem from './CartItem'
33
import { useGlobalContext } from './context'
44

55
const CartContainer = () => {
6-
const { cart, clearCart, totalPrice } = useGlobalContext()
6+
const { cart } = useGlobalContext()
77
if (cart.length === 0) {
88
return (
99
<section className='cart'>
@@ -32,12 +32,12 @@ const CartContainer = () => {
3232
<hr />
3333
<div className='cart-total'>
3434
<h4>
35-
total <span>${totalPrice}</span>
35+
total <span>$0.00</span>
3636
</h4>
3737
</div>
3838
<button
3939
className='btn clear-btn'
40-
onClick={clearCart}
40+
onClick={() => console.log('clear cart')}
4141
>
4242
clear cart
4343
</button>

src/projects/12.CartUseReducer/CartItem.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import React from 'react'
22
import { useGlobalContext } from './context'
3-
43
const CartItem = ({ id, img, title, price, amount }) => {
5-
const {updateAmount, removeItem} = useGlobalContext()
64
return (
75
<article className='cart-item'>
86
<img src={img} alt={title} />
@@ -12,22 +10,22 @@ const CartItem = ({ id, img, title, price, amount }) => {
1210
{/* remove button */}
1311
<button
1412
className='remove-btn'
15-
onClick={() => removeItem(id)}
13+
onClick={() => console.log('remove item')}
1614
>
1715
remove
1816
</button>
1917
</div>
2018
<div>
2119
{/* increase amount */}
22-
<button className='amount-btn' onClick={() => updateAmount(id, 1)}>
20+
<button className='amount-btn' onClick={() => console.log('increase')}>
2321
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'>
2422
<path d='M10.707 7.05L10 6.343 4.343 12l1.414 1.414L10 9.172l4.243 4.242L15.657 12z' />
2523
</svg>
2624
</button>
2725
{/* amount */}
2826
<p className='amount'>{amount}</p>
2927
{/* decrease amount */}
30-
<button className='amount-btn' onClick={() => updateAmount(id, -1)}>
28+
<button className='amount-btn' onClick={() => console.log('decrease')}>
3129
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'>
3230
<path d='M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z' />
3331
</svg>

src/projects/12.CartUseReducer/Navbar.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react'
22
import { useGlobalContext } from './context'
33
const Navbar = () => {
4-
const {totalItems} = useGlobalContext()
54
return (
65
<nav>
76
<div className='nav-center'>
@@ -11,7 +10,7 @@ const Navbar = () => {
1110
<path d='M16 6v2h2l2 12H0L2 8h2V6a6 6 0 1 1 12 0zm-2 0a4 4 0 1 0-8 0v2h8V6zM4 10v2h2v-2H4zm10 0v2h2v-2h-2z' />
1211
</svg>
1312
<div className='amount-container'>
14-
<p className='total-amount'>{totalItems}</p>
13+
<p className='total-amount'>3</p>
1514
</div>
1615
</div>
1716
</div>

src/projects/12.CartUseReducer/context.js

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,17 @@ import React, { useState, useContext, useReducer, useEffect } from 'react'
22
import cartItems from './data'
33
import reducer from './reducer'
44

5-
65
const url = 'https://course-api.com/react-useReducer-cart-project'
7-
86
const AppContext = React.createContext()
97

108
const AppProvider = ({ children }) => {
119
const [cart, setCart] = useState(cartItems)
12-
const [totalItems, setTotalItems] = useState(cart.length)
13-
const [totalPrice, setTotalPrice] = useState(0)
14-
15-
// Function that clear all the items of the cart
16-
const clearCart = () =>{
17-
setCart([])
18-
}
19-
20-
// Function that update the amount of items in the cart
21-
const updateAmount = (id, amount) => {
22-
23-
const cartItem = cart.find(item => item.id === id) //Get the item with the given id
24-
const newAmount = cartItem.amount + amount // calculate the new amount
25-
26-
// If the number of items is 0 remove the item from the cart
27-
if(newAmount ===0) {
28-
setCart(cart.filter(item => item.id !== id))
29-
} else {
30-
setCart(cart.map(item => (item.id === id ? { ...item, amount: newAmount } : item)))
31-
}
32-
// update the total amount of items
33-
setTotalItems(prevTotalItems => prevTotalItems + amount)
34-
}
35-
36-
// Function that calculate the totlal price of the items in the cart
37-
const calculateTotalPrice = () => {
38-
let total = 0
39-
cart.forEach(item => total += item.price * item.amount)
40-
setTotalPrice(total.toFixed(2))
41-
}
42-
43-
// Function that remove an item from the cart
44-
const removeItem = (id) => {
45-
const item = cart.find(item => item.id === id)
46-
setTotalItems(prevTotalItems => prevTotalItems - item.amount)
47-
setCart(cart.filter(item => item.id !== id))
48-
}
49-
50-
// Use Effect to update the total price everytime the cart, total items is updated
51-
useEffect(() => {
52-
calculateTotalPrice()
53-
}, [cart, totalItems])
5410

5511
return (
5612
<AppContext.Provider
5713
value={{
5814
cart,
59-
clearCart,
60-
totalItems,
61-
updateAmount,
62-
totalPrice,
63-
removeItem
64-
}}
15+
}}
6516
>
6617
{children}
6718
</AppContext.Provider>

0 commit comments

Comments
 (0)