Is there a way to combine the subscriptions for this.controls.attendanceDate and this.controls.type? I need to have access to both at the same time in order to call each of this.isAttendanceDateOnOrAfterStartDate() and this.isAttendanceDuplicate() at the same time in the if statement.
onAttendanceFormChanges(): void {
this.subscriptions.push(this.controls.attendanceDate.valueChanges.subscribe(() => {
this.subscriptions.push(this.$spService.clientProfile$.subscribe(client => {
if (this.controls.attendanceDate.value) {
if (this.isAttendanceDateOnOrAfterStartDate(this.controls.attendanceDate.value, client)) {
this.isValidAttendanceDate$.next(false);
this.controls.attendanceDate.setErrors(null);
this.controls.status.enable();
} else {
this.isValidAttendanceDate$.next(true);
this.controls.attendanceDate.setErrors({ 'duplicate': true });
this.controls.status.disable();
}
}
}));
}));
this.subscriptions.push(this.controls.type.valueChanges.subscribe(() => {
this.subscriptions.push(this.$spService.clientAttendance$.subscribe(attendances => {
if (this.controls.attendanceDate.value) {
if (this.isAttendanceDuplicate(this.controls.attendanceDate.value, this.controls.type.value.id, attendances)) {
this.isDuplicateAttendance$.next(true);
this.controls.attendanceDate.setErrors({ 'duplicate': true });
this.controls.status.disable();
} else {
this.isDuplicateAttendance$.next(false);
this.controls.attendanceDate.setErrors(null);
this.controls.status.enable();
}
}
}));
}));
Use
mergeoperator to combine multiple subscriptions. See the example https://www.learnrxjs.io/learn-rxjs/operators/combination/merge