Skip to content

Commit 0bfbd38

Browse files
author
WebDeveloperGuide
committed
Allow to Add to Cart item from Home Page > Featured Products
Allow to Add to Cart item from Products Page > Product listing Fix: All item quantity updated when add existing product in cart.
1 parent ce5c19d commit 0bfbd38

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

web_panel/src/components/Product.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import React from 'react';
22
import {Link} from 'react-router-dom';
3+
import {useDispatch} from 'react-redux';
4+
import {addToCart} from '../redux/actions/cartActions';
35

46
const Products = (props) => {
57

68
const {id,thumbnail,price,title} = props.detail;
9+
const dispatch = useDispatch();
10+
11+
const addToCartHandle = (product) => {
12+
dispatch(addToCart(product,1));
13+
}
14+
715
return(
816
<>
917
<article className="product">
1018
<div className="product-container">
1119
<img src={thumbnail} className="product-img img" alt={title} />
1220
<div className="product-icons">
13-
<Link to={`/product/${id}`} className="product-icon">
21+
<Link to={`/product/${id}`} className="product-icon" title="View">
1422
<i className="fas fa-search" />
1523
</Link>
16-
<button className="product-cart-btn product-icon" data-id={id}>
24+
<button className="product-cart-btn product-icon" title="Add to Cart" data-id={id} onClick={()=>addToCartHandle(props.detail)}>
1725
<i className="fas fa-shopping-cart" />
1826
</button>
1927
</div>

web_panel/src/redux/reducers/cartReducer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ export const cartReducer = (state = initialState,{type,payload}) => {
2121
case ActionTypes.ADD_ITEM_TO_CART:
2222
const newItem = payload;
2323
const existItem = state.cartItems.find((x) => x.id === newItem.id);
24-
24+
2525
if (existItem) {
26-
const tempNewCart = state.cartItems.map((item) => {
27-
if (newItem.id === existItem.id) {
26+
const tempNewCart = state.cartItems.map((item) => {
27+
if (item.id === existItem.id) {
2828
let newQty = parseInt(newItem.qty) + parseInt(existItem.qty)
2929
return { ...item, qty: newQty }
3030
}

0 commit comments

Comments
 (0)