Skip to content

Commit ce5c19d

Browse files
author
WebDeveloperGuide
committed
Added features for add to cart,
Increament/Decrement Item qty, Remove Item from cart, Cart Icon item count, Cart Total Amount added, Show cart items from localstorage to stay items when reload page.
1 parent b16320d commit ce5c19d

File tree

6 files changed

+125
-20
lines changed

6 files changed

+125
-20
lines changed

web_panel/src/components/CartProduct.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
11
import React from 'react';
2+
import {useDispatch} from 'react-redux';
3+
import {removeFromCart, increaseProductQty, decreaseProductQty} from '../redux/actions/cartActions';
4+
5+
const CartProduct = ({detail}) => {
6+
const {id,image, name, price, qty} = detail;
7+
const dispatch = useDispatch();
8+
const removeItemFromCart = (id) =>{
9+
dispatch(removeFromCart(id));
10+
}
11+
12+
const cartIncreaseItem = (id) => {
13+
dispatch(increaseProductQty(id));
14+
}
15+
16+
const cartDecreaseItem = (id) => {
17+
dispatch(decreaseProductQty(id));
18+
}
19+
220

3-
const CartProduct = ({detail}) => {
4-
5-
const {image, name, price} = detail;
621
return(
722
<>
823
<article className="cart-item">
924
<img src={image} className="cart-item-img" alt="product" />
1025
<div className="cart-item-info">
1126
<h5 className="cart-item-name">{name}</h5>
1227
<span className="cart-item-price">${price}</span>
13-
<button className="cart-item-remove-btn">remove</button>
28+
<button className="cart-item-remove-btn" title="Remove" onClick={()=>{removeItemFromCart(id)}}>
29+
<i className="fa fa-trash" aria-hidden="true"></i>
30+
</button>
1431
</div>
1532
<div>
16-
<button className="cart-item-increase-btn">
33+
<button className="cart-item-increase-btn" onClick={()=>cartIncreaseItem(id)}>
1734
<i className="fas fa-chevron-up" />
1835
</button>
19-
<span className="cart-item-amount">1</span>
20-
<button className="cart-item-decrease-btn">
36+
<span className="cart-item-amount">{qty}</span>
37+
<button className={'cart-item-decrease-btn' + ((qty ===1) ? ' disabled' : '' )} onClick={()=>cartDecreaseItem(id)}>
2138
<i className="fas fa-chevron-down" />
2239
</button>
2340
</div>

web_panel/src/components/Navbar.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {showCart} from '../redux/actions/cartActions';
88
const NavBar = () => {
99
const dispatch = useDispatch();
1010
const showCartStatus = useSelector((state)=> state.cart.showCart);
11-
const cartItemsCount = useSelector((state)=> state.cart.cartItems).length;
11+
const cartItems = useSelector((state)=> state.cart.cartItems);
12+
let cartItemsCount = cartItems.reduce((total, item)=>{
13+
return total + item.qty
14+
}, 0);
15+
1216

1317
const toggleCart = () => {
1418
dispatch(showCart(!showCartStatus))

web_panel/src/index.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,4 +909,17 @@ Cart
909909
.empty-cart{
910910
margin:150px 0;
911911
text-align: center;
912+
}
913+
914+
.cart-item-remove-btn i{
915+
color: red;
916+
font-size: 17px;
917+
}
918+
919+
.hide{
920+
display: none;
921+
}
922+
923+
.disabled {
924+
cursor: not-allowed;
912925
}

web_panel/src/redux/actions/cartActions.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const addToCart = (product,qty) => (dispatch,getState) => {
3737

3838
localStorage.setItem("cartItems", JSON.stringify(getState().cart.cartItems));
3939

40-
toast.error("Added to Cart", ToastObjects);
40+
toast.success("Added to Cart", ToastObjects);
4141

4242
}catch (error){
4343
const message =
@@ -47,4 +47,34 @@ export const addToCart = (product,qty) => (dispatch,getState) => {
4747

4848
toast.error(message, ToastObjects);
4949
}
50-
}
50+
}
51+
52+
// Remove item from cart
53+
export const removeFromCart = (id) => (dispatch, getState) => {
54+
dispatch({
55+
type: ActionTypes.CART_REMOVE_ITEM,
56+
payload: id,
57+
});
58+
toast.success("Item Removed From Cart", ToastObjects);
59+
localStorage.setItem("cartItems", JSON.stringify(getState().cart.cartItems));
60+
};
61+
62+
// Remove item from cart
63+
export const increaseProductQty = (id) => (dispatch, getState) => {
64+
dispatch({
65+
type: ActionTypes.INCREASE_ITEM,
66+
payload: id,
67+
});
68+
69+
localStorage.setItem("cartItems", JSON.stringify(getState().cart.cartItems));
70+
};
71+
72+
// Remove item from cart
73+
export const decreaseProductQty = (id) => (dispatch, getState) => {
74+
dispatch({
75+
type: ActionTypes.DECREASE_ITEM,
76+
payload: id,
77+
});
78+
79+
localStorage.setItem("cartItems", JSON.stringify(getState().cart.cartItems));
80+
};

web_panel/src/redux/constants/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ export const ActionTypes = {
44
RESET_PRODUCT_DETAIL:"RESET_PRODUCT_DETAIL",
55
CART_STATUS:"CART_STATUS",
66
ADD_ITEM_TO_CART:"ADD_ITEM_TO_CART",
7+
CART_REMOVE_ITEM:"CART_REMOVE_ITEM",
8+
INCREASE_ITEM:"INCREASE_ITEM",
9+
DECREASE_ITEM:"DECREASE_ITEM"
710
}

web_panel/src/redux/reducers/cartReducer.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { ActionTypes } from '../constants';
22

3+
const getLocalCartItems = () => {
4+
let cartItems = localStorage.getItem('cartItems')
5+
if (cartItems) {
6+
return JSON.parse(localStorage.getItem('cartItems'))
7+
} else {
8+
return []
9+
}
10+
}
11+
312
const initialState = {
4-
cartItems: [],
13+
cartItems: getLocalCartItems(),
514
showCart:false
615
}
716

@@ -10,22 +19,51 @@ export const cartReducer = (state = initialState,{type,payload}) => {
1019
case ActionTypes.CART_STATUS:
1120
return {...state,showCart:payload};
1221
case ActionTypes.ADD_ITEM_TO_CART:
13-
const item = payload;
14-
const existItem = state.cartItems.find((x) => x.product === item.product);
22+
const newItem = payload;
23+
const existItem = state.cartItems.find((x) => x.id === newItem.id);
1524

1625
if (existItem) {
17-
return {
18-
...state,
19-
cartItems: state.cartItems.map((x) =>
20-
x.id === existItem.id ? item : x
21-
),
22-
};
26+
const tempNewCart = state.cartItems.map((item) => {
27+
if (newItem.id === existItem.id) {
28+
let newQty = parseInt(newItem.qty) + parseInt(existItem.qty)
29+
return { ...item, qty: newQty }
30+
}
31+
return item
32+
});
33+
return { ...state, cartItems: tempNewCart }
34+
2335
} else {
2436
return {
2537
...state,
26-
cartItems: [...state.cartItems, item],
38+
cartItems: [...state.cartItems, newItem],
2739
};
2840
}
41+
case ActionTypes.INCREASE_ITEM:
42+
const tempIncCart = state.cartItems.map((item) => {
43+
if (item.id === payload) {
44+
let newQty = item.qty + 1
45+
return { ...item, qty: newQty }
46+
}
47+
return item
48+
});
49+
return { ...state, cartItems: tempIncCart }
50+
case ActionTypes.DECREASE_ITEM:
51+
const tempDescCart = state.cartItems.map((item) => {
52+
if (item.id === payload) {
53+
let newQty = item.qty - 1
54+
if(newQty === 0){
55+
return item
56+
}
57+
return { ...item, qty: newQty }
58+
}
59+
return item
60+
});
61+
return { ...state, cartItems: tempDescCart }
62+
case ActionTypes.CART_REMOVE_ITEM:
63+
return {
64+
...state,
65+
cartItems: state.cartItems.filter((x) => x.id !== payload),
66+
};
2967
default:
3068
return state;
3169
}

0 commit comments

Comments
 (0)