Skip to content

Commit 4fa7fc9

Browse files
updated shop data with new data, updated cart utils to group similar items
1 parent 9d32b3f commit 4fa7fc9

File tree

6 files changed

+40
-10
lines changed

6 files changed

+40
-10
lines changed

src/components/collection-item/collection-item.component.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import React from 'react'
2+
import { connect } from 'react-redux'
3+
import { addItem } from '../../redux/cart/cart.actions'
24
import CustomButton from '../custom-button/custom-button.component'
35
import './collection-item.styles.scss'
4-
export default function CollectionItem({ id, name, price, imageUrl }) {
6+
function CollectionItem({ item, addItem }) {
7+
const { name, price, imageUrl } = item
58
return (
69
<div className="collection-item">
710
<div className="image"
@@ -13,7 +16,13 @@ export default function CollectionItem({ id, name, price, imageUrl }) {
1316
<span className="name">{name}</span>
1417
<span className="price">{price}</span>
1518
</div>
16-
<CustomButton inverted> Add to Cart </CustomButton>
19+
<CustomButton onClick={() => addItem(item)} inverted> Add to Cart </CustomButton>
1720
</div>
1821
)
1922
}
23+
24+
const mapDispatchToProps = dispatch => ({
25+
addItem: item => dispatch(addItem(item))
26+
})
27+
28+
export default connect(null, mapDispatchToProps)(CollectionItem)

src/components/collection-preview/Collection-preview.component.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export default function CollectionPreview({ title, items }) {
99
{
1010
items
1111
.filter((item, idx) => idx < 4)
12-
.map(({ id, ...otherItemProps }) => (
13-
<CollectionItem key={id} {...otherItemProps} />
12+
.map( item => (
13+
<CollectionItem key={item.id} item={item} />
1414
))
1515
}
1616
</div>

src/redux/cart/cart.actions.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ import CartActionTypes from './cart.types'
22

33
export const toggleCartHidden = () => ({
44
type: CartActionTypes.TOGGLE_CART_HIDDEN
5-
})
5+
})
6+
export const addItem = (item) => ({
7+
type: CartActionTypes.ADD_ITEM,
8+
payload: item
9+
})

src/redux/cart/cart.reducer.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import CartActionTypes from './cart.types'
2-
2+
import { addItemToCart } from './cart.utils'
33

44
const INITIAL_STATE = {
5-
hidden: true
5+
hidden: true,
6+
cartItems: []
67
}
78

89

@@ -14,8 +15,13 @@ const cartReducer = (state = INITIAL_STATE, action) => {
1415
...state,
1516
hidden: !state.hidden
1617
}
17-
default:
18-
return state
18+
case CartActionTypes.ADD_ITEM:
19+
return {
20+
...state,
21+
cartItems: addItemToCart(state.cartItems, action.payload)
22+
}
23+
default:
24+
return state
1925
}
2026
}
2127

src/redux/cart/cart.types.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const CartActionTypes = {
2-
TOGGLE_CART_HIDDEN: 'TOGGLE_CART_HIDDEN'
2+
TOGGLE_CART_HIDDEN: 'TOGGLE_CART_HIDDEN',
3+
ADD_ITEM: 'ADD_ITEM'
34
}
45

56
export default CartActionTypes

src/redux/cart/cart.utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const addItemToCart = (cartItems, cartItemToAdd) => {
2+
const existingCartItem = cartItems.find(cartItem => cartItem.id === cartItemToAdd.id)
3+
4+
if (existingCartItem) {
5+
return cartItems.map(cartItem => cartItem.id === cartItemToAdd.id ?
6+
{ ...cartItem, quantity: cartItem.quantity + 1 } : cartItem)
7+
}
8+
9+
return [...cartItems, { ...cartItemToAdd, quantity: 1 }]
10+
}

0 commit comments

Comments
 (0)