When utilizing Ngrx for data storage, it is necessary to create three actions for each asynchronous event. For example, when retrieving a profile, the actions would be GetProfile, GetProfileSuccess, and GetProfileFailed. Typically, effects are used to interact with APIs, resulting in a combination of reducers, actions, and effects for managing profile data. During an asynchronous event, either an error action is triggered or a success action is provided. Here is an example from an effects file:
public loadProfile$: Observable<Action> = createEffect(() =>
this.actions$.pipe(
ofType(ProfileApiActionTypes.GetProfile),
mergeMap(() =>
this.profileDataService.getProfile().pipe(
map(({data}) => getProfileSuccessAction({ data })),
catchError((error) => of(getProfileFaildAction({ payload: error }))),
),
),
),
);
Furthermore, within the same file, we handle error actions in the effect as follows:
public profileError$: Observable<Action> = createEffect(() =>
this.actions$.pipe(
ofType(
ProfileApiActionTypes.GetProfileFailed,
),
tap(({ payload }) => {
// your logic with snackbar
},
),
{ dispatch: false },
);