Created
February 15, 2019 01:57
-
-
Save bietkul/38905d32b088c1e48bdcd37378476152 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import some from 'lodash/some'; | |
| import AppConstants from '../utils/constants'; | |
| const initialProductState = { | |
| items: [], | |
| totalPrice: 0, | |
| }; | |
| const addItems = (items = [], payload) => { | |
| const newItems = items.map(item => item); | |
| if (!some(items, e => e._id === payload._id)) { | |
| newItems.push(payload); | |
| } | |
| return newItems; | |
| }; | |
| const removeItem = (items = [], id) => { | |
| const newItems = items.map(item => item); | |
| newItems.every((e, index) => { | |
| if (e._id === id) { | |
| newItems.splice(index, 1); | |
| return false; | |
| } | |
| return true; | |
| }); | |
| return newItems; | |
| }; | |
| function checkout(state = initialProductState, action) { | |
| switch (action.type) { | |
| case AppConstants.CHECKOUT.ADD_PRODUCT: | |
| return { | |
| ...state, | |
| items: addItems(state.items, action.payload), | |
| }; | |
| case AppConstants.CHECKOUT.REMOVE_PRODUCT: | |
| return { | |
| ...state, | |
| items: removeItem(state.items, action.payload), | |
| }; | |
| default: | |
| return state; | |
| } | |
| } | |
| export default checkout; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment