QUESTION: What's the best way to push a new payload object, to a nested redux reducers state?
I would like to push a new item location to the current lists array within the reducers function, using the current state index values.
MY CURRENT REDUX ACTION:
export const addLocation = () => {
return (dispatch) => {
axios.post("/inventory/lists/addlocation", locationData).then((res) => {
dispatch({
type: ADD_POSTED_LOCATION,
payload: {location: "test", count: 0, _id: "6a5e4f6ae51g6ea5r1ga6e1rf"},
});
});
};
};
MY CURRENT REDUCER:
const initialState = {
lists: [],
currentIndex: {
listIndex: null,
itemIndex: null,
locationIndex: null
},
};
export default function(state = initialState, action) {
switch (action.type) {
case ADD_POSTED_LOCATION:
const listIndex = state.currentIndex.listIndex;
const itemIndex = state.currentIndex.listIndex;
return {
...state,
lists: state.lists[listIndex ].items[itemIndex].locations.push(action.payload)
};
lists: [a number, excluding the list objects and values]
DESIRED RESULT:
Lists: [
{
_id: "L1",
items: [{
number: "n1",
locations: [{
location: "L1"
}, {
location: "L2"
}]
},
{
number: "n2",
locations: [{
location: "L1"
}, {
location: "L2"
},
{
location: "L3"
},
{
location: "L4"
}, {
location: "L5"
}
]
}
]
]