#redux cases

1 messages · Page 1 of 1 (latest)

wet walrusBOT
#

Hi @supple root. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the provided knowledge sources, there isn't any specific information about handling Redux cases for removing products or removing all products. However, there are some general principles that can be applied from the knowledge sources.

The error message you're seeing suggests that there's a type mismatch between the id property of your product objects and the payload of your action. The id is a string, while the payload is a number. You'll need to ensure that these types match.

If your product IDs are indeed strings, you should change your PayloadAction<number> to PayloadAction<string> in both removeProduct and removeAllProducts actions.

Here's how you might adjust your code:

removeProduct: (state, action: PayloadAction<string>) => {
  const indexToRemove = state.products.findIndex(
    (product) => product.id === action.payload
  );
  if (indexToRemove !== -1) {
    if (state.products[indexToRemove].quantity > 1) {
      state.products[indexToRemove].quantity -= 1;
    } else {
      state.products.splice(indexToRemove, 1);
    }
  }
},
removeAllProducts: (state, action: PayloadAction<string>) => {
  state.products = state.products.filter(
    (product) => product.id !== action.payload
  );
},

Please note that this advice is based on general JavaScript and Redux principles, and not on the specific knowledge sources provided.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: