Skip to content

Instantly share code, notes, and snippets.

@fatgy
Created July 3, 2017 16:43
Show Gist options
  • Save fatgy/2e25cdc0cc2871553ac9872dac4a7a3d to your computer and use it in GitHub Desktop.
Save fatgy/2e25cdc0cc2871553ac9872dac4a7a3d to your computer and use it in GitHub Desktop.
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