I have an angular dialog that uses ngrx
to call an API.
The registerEvents
is only about adding two subscriptions to an array, is only called in the constructor
But for some reason the code inside the fat-arrow of the two subscribes is getting called as soon as the dialog opens, I would expect only one "new data" message instead of two.
Is almost like the registerEvents
is executing the arrow functions or that the actions are being dispached automagically
constructor(@Inject(MAT_DIALOG_DATA) public data: any,
private dialogRef: MatDialogRef<ScheduledAppointmentsDialogComponent>,
private orderStore$: Store<OrdersState.State>)
{
this.registerEvents();
}
registerEvents() {
this.subscriptions.push(this.apptsForOrder$.subscribe((apptsForOrder: any[]) => {
console.log("new data");
this.apptsForOrder = apptsForOrder;
}));
this.subscriptions.push(this.updateAttachedAppointment$.subscribe((attachAppointment: boolean) => {
console.log({ attachAppointment: attachAppointment });
this.toogleLinkStatus();
}, (err) => { console.log(err) }));
}
ngOnInit(): void {
// other code
this.loadApptsForOrder();
// other code
}
loadApptsForOrder() {
this.orderStore$.dispatch(OrdersActions.loadApptsForOrder({ orderId: this.orderId }));
}
The loadApptsForOrder
is only being called inside the onInit
but there are two "new data" messages
I have tried changing from mergeMap to switchMap without any effect
attachAppointment$ = createEffect(() => this.actions$.pipe(
ofType(actions.attachAppointment),
mergeMap(args => this.orderService.attachAppointment(args.attachAppointmentRequest)
.pipe(
map(response => actions.attachAppointmentComplete({ attachAppointmentResponse: response.statusCode })),
catchError(() => of(actions.attachAppointmentFailure({ error: 'fail' })))
)
)
));
Could this be an error with the way I have implemented the service/store ? in the way that I'm subscribing or what else ?
apptsForOrder$
and orderStore$
are observables<T>
private apptsForOrder$ = this.orderStore$.select(OrdersSelectors.getApptsForOrder);
private updateAttachedAppointment$ = this.orderStore$.select(OrdersSelectors.updateAttachedAppointment);