fzwael
fzwael

Reputation: 103

Ngrx handle API failure

This question concerns the example provided by ngrx example-app (I didn't know if I can post this to their Github page or not...)

So when creating a new project I wanted to follow the best guidelines, that's why I'm checking the example app as a reference. In the application when for example they try to delete a book entity the do the following :

  1. Component dispatches a request with the action 'removeBook' and 'book' object as payload (selected-book-page.component.ts:40)
  2. The effect handles that and if success it calls the 'removeBookSuccess' action which will remove the book from the store (collection.effects.ts:65)

Now the second case is when it fails it will call 'removeBookFailure' also with book object and actually that action will be handled exactly like 'addBookSuccess'

on(
    CollectionApiActions.addBookSuccess,
    CollectionApiActions.removeBookFailure,
    (state, { book }) => {
      if (state.ids.indexOf(book.id) > -1) {
        return state;
      }
      return {
        ...state,
        ids: [...state.ids, book.id],
      };
    }
  ),

Do we really need to do that? I mean the book in question was never removed to begin with. Or is there something I'm missing here.

Thanks for your help!

Upvotes: 1

Views: 141

Answers (1)

timdeschryver
timdeschryver

Reputation: 15505

It might seem weird, but it serves API failures. What I do expect, and what is missing, is the removal of the book when the removeBook action is dispatched.

If the API call succeeds, everything is already handled correctly - but when it fails we should revert the removal of the book.

Upvotes: 1

Related Questions