mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 12:53:03 -05:00
40 lines
1.2 KiB
TypeScript
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...
|
|
}
|
|
});
|
|
}
|
|
}
|