I just start using normalizr to manage my state. I've gotten to the point where the below code updates the needed objects but I haven't been able to merge them back into the normalized state in a clean, dry way. What is the preferred way of doing this?
Pardon my noobness and thank you for your time.
// normalized state
{
"entities": {
"items": {
"1": {
"id": 1,
"title": "Item 1",
},
"2": {
"id": 2,
"title": "Item 2",
},
"3": {
"id": 3,
"title": "Item 3",
},
},
"groups": {
"20": {
"id": 20,
"title": "Group 1",
"items": [ 1, 2 ]
},
"21": {
"id": 21,
"title": "Group 2",
"items": [ 3 ]
}
}
},
"result": [ 20, 21 ]
}
// actions
export const addItem = values => ({
type: ADD_ITEM,
payload: {...values, itemID: uuid.v1() }
// values come from a form
})
// reducer
const items = (state, action) => {
switch(action.type) {
case ADD_ITEM:
let { items } = state.entities;
let { itemID } = action.payload;
return {
...items,
[itemID]: {
id: itemID,
...item,
subItems:[]
}
}
default:
return state;
}
}
const groups = (state = initialState, action) => {
switch(action.type) {
case ADD_ITEM:
let { payload } = action;
let { groupID, itemID } = payload; // GroupID comes from form submit
let { groups } = state.entities;
return {
...groups,
[groupID]: [
...groups[groupID],
groups[groupID].items = [
...groups[groupID].items,
itemID
]
]
}
default:
return state;
}
}
export default groups;