I have a component with a form that adds items to a list. After successfully adding an item to the list, I want to use form.resetForm();, but I can't figure out a simple way to know when that action was successful. I was hoping I could subscribe to the action dispatch, but haven't been able to figure that out.
I've tried several approaches I've found online, but they all seem to be outdated. I did get it to work by adding a saving property to my store and subscribing to that, but that seemed like an excessive amount of work for something that should be pretty simple.
Is there a way to just subscribe to something inside my component or container without refactoring my NGRX logic?
Here is the code in my container that adds the item to the list:
addPosition(position: Position) {
this.store.dispatch(new fromStore.CreatePosition(position));
}
Action:
export const CREATE_POSITION = '[Profile] Create Position';
export const CREATE_POSITION_FAIL = '[Profile] Create Position Fail';
export const CREATE_POSITION_SUCCESS = '[Profile] Create Position Success';
export class CreatePositionSuccess implements Action {
readonly type = CREATE_POSITION_SUCCESS;
constructor(public payload: any) {}
}
Effect:
@Effect()
createPosition$ = this.actions$.pipe(
ofType(positionsActions.CREATE_POSITION),
map((action: positionsActions.CreatePosition) => action.payload),
switchMap(position => {
return this.positionService
.addPosition(position)
.pipe(
map(newPosition => new positionsActions.CreatePositionSuccess(newPosition)),
catchError(error => of(new positionsActions.CreatePositionFail(error)))
);
})
);
I'm on version 7 of ngrx and 6.3 of rxjs.