Files
angular-challenges/apps/ngrx/notification/src/app/data-access/notification.service.ts
2024-01-12 12:53:02 -05:00

40 lines
1.2 KiB
TypeScript

import { PushService } from '@angular-challenges/ngrx-notification/backend';
import {
isSchool,
isStudent,
isTeacher,
Push,
} from '@angular-challenges/ngrx-notification/model';
import { inject, Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { filter } from 'rxjs';
import { studentActions } from '../student/store/student.actions';
import { teacherActions } from '../teacher/store/teacher.actions';
@Injectable({ providedIn: 'root' })
export class NotificationService {
private pushService = inject(PushService);
private store = inject(Store);
init() {
this.pushService.notification$
.pipe(filter(Boolean))
.subscribe((notification: Push) => {
if (isTeacher(notification)) {
this.store.dispatch(
teacherActions.addOneTeacher({ teacher: notification }),
);
}
if (isStudent(notification)) {
this.store.dispatch(
studentActions.addOneStudent({ student: notification }),
);
}
if (isSchool(notification)) {
// SchoolStore is a ComponentStore. We can't dispatch a school action here.
// We are stuck. We must have done something wrong and need to refactor...
}
});
}
}