import { Action , Reducer } from 'redux' ; import { Cart , CartItem , Product } from '../types' ; import { actions } from '../actions/constants' ; const initialState = { cart : [ ] , isLoading : false , error : false } ; const addItemToCart = ( cart , product ) : CartItem [ ] => { const { quantity , ... item } = product ; let foundItem = cart . find ( ( item ) => item . id === product . id ) ; if ( foundItem ) return cart . map ( ( item ) => item . id === product . id ? { ... item , quantityBought : item . quantityBought + 1 } : item ) ; return [ ... cart , { ... item , quantityBought : 1 } ] ; } ; export const cartOld = ( state = initialState , action ) => { switch ( action . type ) { case actions . ADD_TO_CART_OLD : return { ... state , cart : addItemToCart ( state . cart , action . payload ) } ; default : return { ... state } ; } } ;