diff --git a/src/components/checkout-item/checkout-item.component.jsx b/src/components/checkout-item/checkout-item.component.jsx index 06a808d..99155f9 100644 --- a/src/components/checkout-item/checkout-item.component.jsx +++ b/src/components/checkout-item/checkout-item.component.jsx @@ -1,17 +1,34 @@ import React from 'react'; +import { connect } from 'react-redux'; +import { clearItem, addItem, removeItem } from '../../redux/cart/cart.actions'; import './checkout-item.styles.scss'; -const CheckoutItem = ({cartItem:{name, imageUrl, price, quantity}}) => ( +const CheckoutItem = ({cartItem, removeItemFromCart, addItem, removeItem}) => { + const {name, imageUrl, price, quantity} = cartItem; + return (
item
{name} - {quantity} + + +
removeItem(cartItem)} >❮
+ {quantity} +
addItem(cartItem)}>❯
+
{price} -
+
removeItemFromCart(cartItem)}>✕
-) + ) +} -export default CheckoutItem; \ No newline at end of file +const mapDispatchToProps = dispatch => ({ + removeItemFromCart:item => dispatch(clearItem(item)), + addItem: item => dispatch(addItem(item)), + removeItem: item => dispatch(removeItem(item)) +}) + + +export default connect(null, mapDispatchToProps)(CheckoutItem); \ No newline at end of file diff --git a/src/components/checkout-item/checkout-item.styles.scss b/src/components/checkout-item/checkout-item.styles.scss index f9b320e..fe43a7a 100644 --- a/src/components/checkout-item/checkout-item.styles.scss +++ b/src/components/checkout-item/checkout-item.styles.scss @@ -23,7 +23,13 @@ } .quantity { - padding-left: 20px; + display: flex; + .arrow{ + cursor: pointer; + } + .value{ + margin: 0 10px; + } } .remove-button { diff --git a/src/redux/cart/cart.actions.js b/src/redux/cart/cart.actions.js index 1679fbb..d3972b7 100644 --- a/src/redux/cart/cart.actions.js +++ b/src/redux/cart/cart.actions.js @@ -8,4 +8,15 @@ export const addItem = item => ({ type: CartActionTypes.ADD_ITEM, payload: item }) + +export const removeItem = item => ({ + type:CartActionTypes.REMOVE_ITEM, + payload:item +}) + +export const clearItem = item => ({ + type: CartActionTypes.CLEAR_ITEM_FROM_CART, + payload: item +}) + export default addItem; \ No newline at end of file diff --git a/src/redux/cart/cart.reducer.js b/src/redux/cart/cart.reducer.js index b2561a1..6af520c 100644 --- a/src/redux/cart/cart.reducer.js +++ b/src/redux/cart/cart.reducer.js @@ -1,5 +1,5 @@ import CartActionTypes from './cart.types'; -import addItemToCart from './cart.utils' +import { addItemToCart, clearCartItemFromCart, removeCartItem} from './cart.utils' const INITIAL_STATE = { hidden: true, @@ -19,6 +19,16 @@ export const cartReducer = (state = INITIAL_STATE, action) => { ...state, cartItems: addItemToCart(state.cartItems, action.payload) } + case CartActionTypes.REMOVE_ITEM: + return { + ...state, + cartItems: removeCartItem(state.cartItems, action.payload) + } + case CartActionTypes.CLEAR_ITEM_FROM_CART: + return { + ...state, + cartItems: clearCartItemFromCart(state.cartItems, action.payload) + } default: return state; } diff --git a/src/redux/cart/cart.types.js b/src/redux/cart/cart.types.js index 389e21c..2244fac 100644 --- a/src/redux/cart/cart.types.js +++ b/src/redux/cart/cart.types.js @@ -1,6 +1,8 @@ const CartActionTypes = { TOGGLE_CART_HIDDEN: 'TOGGLE_CART_HIDDEN', - ADD_ITEM: 'ADD_ITEM' + ADD_ITEM: 'ADD_ITEM', + REMOVE_ITEM: 'REMOVE_ITEM', + CLEAR_ITEM_FROM_CART: 'CLEAR_ITEM_FROM_CART' } export default CartActionTypes; \ No newline at end of file diff --git a/src/redux/cart/cart.utils.js b/src/redux/cart/cart.utils.js index 0837299..1631bc2 100644 --- a/src/redux/cart/cart.utils.js +++ b/src/redux/cart/cart.utils.js @@ -14,4 +14,28 @@ export const addItemToCart = (cartItems, cartItemToAdd) => { return [...cartItems, {...cartItemToAdd, quantity: 1}]; }; + +export const removeCartItem = (cartItems, cartItemToRemove) => { + const existingCartItem = cartItems.find( + cartItem => cartItem.id === cartItemToRemove.id + ) + + if(existingCartItem.quantity===1){ + return cartItems.filter(ci=>ci.id!==cartItemToRemove.id); + } + return cartItems.map( + cartItem=> + cartItem.id===cartItemToRemove.id? + {...cartItem, quantity:cartItem.quantity-1} + : cartItem + ) +} + +export const clearCartItemFromCart = (cartItems, cartItemToRemove) => { + const cartItemsCleared = cartItems.filter( + cartItem=> cartItem.id!==cartItemToRemove.id + ) + return cartItemsCleared; +} + export default addItemToCart; \ No newline at end of file