I have a shopping cart app. When I delete an item from the cart, I want to be able to add it back to the cart if the delete request fails.
I do this by storing my previous array of items before deleting an item. And if the request fails to delete the item, I replace back the entire array of items.
Please note, there are multiple orders because the shopping cart can have multiple vendors, each with their own items to add to the cart.
Here's my reducer, that adds back the previous items to the cart if the delete an item request fails. items
is an array that is a property of an order
object
My Reducer
case 'ADD_PRODUCT_TO_CART_ERROR':
return Object.assign({}, state,
{
orders:
state.orders.slice(0, action.orderIndex)
.concat([{
...state.orders[action.orderIndex],
items: action.previousItems
}])
.concat(state.orders.slice(action.orderIndex + 1))
})
It works, but am I mutating any state here? Or does Object.assign()
take care of that for me?