I've been facing that situation too and you've got in my opinion 2 options there:
- first one and definitely the most simple, you create the ID directly on the frontend using UUID library, but maybe that your IDs are being generated on the backend and you can't do that
- second option, a bit less forward but that's working great too:
Generate an ID and pass it within the payload of the action (in addition of your current payload if needed). You might call it for example actionId
.
From your effect, when you map to the action CREATE_BOOK_SUCCESS
, also pass that ID you've got access to! (because we're in the piece of code that's handling the CREATE_BOOK
action).
From the (smart) component, you can subscribe to actions, just like you do within your effects! And so you can do something like that:
class MyComponent {
// ...
createBook(book: Book) {
// first, generate a unique ID to track the action
// you can call directly the uuid function or even better create
// a uuidService that you inject but for simplicity here I'm not doing it
const actionId: string = uuid();
// then watch for an action of type CREATE_BOOK_SUCCESS with that ID
this.actions.pipe(
ofType<BooksActions.CreateBookSuccess>(BooksActions.CREATE_BOOK_SUCCESS),
filter(({payload}) => payload.actionId === actionId),
first(),
tap(({payload}) => {
// do what you want here
// you've got access to the payload of the CREATE_BOOK_SUCCESS action where
// you have the book returned from the backend (I think)
})
);
// finally, dispatch the action
// it's important to do it after you've subscribed to actions otherwise it might happen
// too fast and you wouldn't get the notification
this.actions.dispatch(new BooksActions.CreateBook({ ...book, actionId }));
}
// ...
}
Note that I'm using first
here when subscribing to the action so even if you add 10 books reaaaally quickly you'll have 10 different subscriptions which is totally fine.