ngrx: How to call a reducer before and after an effect

27 Views Asked by At

I want to fix the following warning:ESLint: Calling 'store.dispatch' in 'Effect' is forbidden.

The effect in question does generally this:

  • lock data in the store using a reducer
  • update the backend data
  • unlock the data

Code:

    this.actions$.pipe(
              ofType(actions.update),
              switchMap(action => {
                this.store.dispatch(actions.lockData(action.data.id));
                return this.backendService.update$(action.data)
                 .pipe(
                    map(...),
                    catchError(...),
                    finalize(()=> this.store.dispatch(actions.unlockData(action.data.id))  
                    )

How to do it properly?

1

There are 1 best solutions below

0
timdeschryver On BEST ANSWER

You can lock/unlock the data with the action that triggers the effect, and with the action that is returned from the effect.

In your case this means actions.update, and the action returned in the map method.