Created
July 3, 2017 16:43
-
-
Save fatgy/2e25cdc0cc2871553ac9872dac4a7a3d 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
const initialState = { | |
addedIds: [], | |
quantityById: {}, | |
kitById: {}, | |
itemFormula: { | |
byId: { | |
1: { | |
id: 1, | |
itemId: 1, | |
formulas: [1, 2] | |
}, | |
2: { | |
id: 2, | |
itemId: 1, | |
formulas: [] | |
} | |
}, | |
allIds: [1, 2] | |
}, | |
quantityByItemFormulaId: {} | |
} | |
const itemFormula = (state = initialState.itemFormula, action) => { | |
switch (action.type) { | |
case CART_ADD_ITEM: | |
const idx = state.allIds.find(id => { | |
let pivot = state.byId[id] | |
return pivot.itemId === action.itemsId && | |
isEqual(pivot.formulas, action.formulas.sort()) | |
}) | |
if (typeof idx !== 'undefined') { | |
return state | |
} | |
const newId = state.allIds.slice(-1)[0] + 1 | |
const byId = {...state.byId, [newId]: {}} | |
const allIds = [...state.allIds, newId] | |
return { ...state, byId: byId, allIds: allIds } | |
case CART_DELETE_ITEM: | |
return state.filter(itemId => action.itemId !== itemId ) | |
default: | |
return state | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment