I am unable to delete items from the redux state. I have created a pizza app and I am trying to delete items from the cart. I have successfully implemented increased quantity or deleting all items in cart functions. However, I am unable to implement a delete a particular item function. My button on the front end are working properly and passing ID to deletePizza function given below
Here is my reducer
export const cartReducer = (state = [], action) => {
switch (action.type) {
//Required by next-js-reducer library
case HYDRATE:
return { ...state, ...action.id };
//Removes all pizza from the cart
case 'DELETE_ALL':
return []
//Add pizza to cart, if exists increment the pizza
case 'ADD_PIZZA':
//If pizza is already in the cart
const findPizzaIfAdded = state.find(pizza => pizza.id === action.id);
if (findPizzaIfAdded !== undefined) {
const modifiedQuantity = {
...findPizzaIfAdded,
quantity: findPizzaIfAdded.quantity + 1
}
return state.map(pizza => pizza.id === action.id ? modifiedQuantity : pizza);
}
const newPizza = {
id: action.id,
quantity: 1
}
return [
...state,
newPizza
]
//I am facing problem here
case 'DELETE_PIZZA':
return state.filter(pizza => pizza.id !== action.id);
default:
return state
}
}
// I am using this funcntion to call deletion of pizza, I tried testing if particular function is working. It is successfully working and passing the id
export const deletePizza = id => {
return {
type: 'DELETE_PIZZA',
id
}
}