mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 12:53:03 -05:00
feat(challenge7): reorganisation
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, tap, timer } from 'rxjs';
|
||||
import { randStudent } from '../model/student.model';
|
||||
import { randTeacher } from '../model/teacher.model';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class PushService {
|
||||
private notificationSubject = new BehaviorSubject<any>(undefined);
|
||||
notification$ = this.notificationSubject.asObservable();
|
||||
|
||||
init() {
|
||||
this.startTeacherNotification();
|
||||
this.startStudentNotification();
|
||||
}
|
||||
|
||||
private startTeacherNotification() {
|
||||
timer(0, 4000)
|
||||
.pipe(tap(() => this.notificationSubject.next(randTeacher())))
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
private startStudentNotification() {
|
||||
timer(1000, 3000)
|
||||
.pipe(tap(() => this.notificationSubject.next(randStudent())))
|
||||
.subscribe();
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,27 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { filter } from 'rxjs';
|
||||
import { PushService } from '../backend/push.service';
|
||||
import { Push } from '../model/push.model';
|
||||
import { isStudent } from '../model/student.model';
|
||||
import { isTeacher } from '../model/teacher.model';
|
||||
import {
|
||||
isStudent,
|
||||
isTeacher,
|
||||
Push,
|
||||
} from '@angular-challenges/ngrx-notification/model';
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { PUSH_ACTION } from './notification.token';
|
||||
import { StudentStore } from './student.store';
|
||||
import { TeacherStore } from './teacher.store';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class NotificationService {
|
||||
constructor(
|
||||
private pushService: PushService,
|
||||
private teacherStore: TeacherStore,
|
||||
private studentStore: StudentStore
|
||||
) {}
|
||||
private notification$ = inject(PUSH_ACTION);
|
||||
private teacherStore = inject(TeacherStore);
|
||||
private studentStore = inject(StudentStore);
|
||||
|
||||
init() {
|
||||
this.pushService.notification$
|
||||
.pipe(filter(Boolean))
|
||||
.subscribe((notification: Push) => {
|
||||
console.log(notification);
|
||||
if (isTeacher(notification)) {
|
||||
this.teacherStore.addOne(notification);
|
||||
}
|
||||
if (isStudent(notification)) {
|
||||
this.studentStore.addOne(notification);
|
||||
}
|
||||
});
|
||||
this.notification$.subscribe((notification: Push) => {
|
||||
if (isTeacher(notification)) {
|
||||
this.teacherStore.addOne(notification);
|
||||
}
|
||||
if (isStudent(notification)) {
|
||||
this.studentStore.addOne(notification);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { PushService } from '@angular-challenges/ngrx-notification/backend';
|
||||
import { Push } from '@angular-challenges/ngrx-notification/model';
|
||||
import { inject, InjectionToken } from '@angular/core';
|
||||
import { filter, Observable, share } from 'rxjs';
|
||||
|
||||
export const PUSH_ACTION = new InjectionToken<Observable<Push>>(
|
||||
'Push messaging action stream',
|
||||
{
|
||||
factory: () =>
|
||||
inject(PushService).notification$.pipe(filter(Boolean), share()),
|
||||
}
|
||||
);
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Student } from '@angular-challenges/ngrx-notification/model';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
import { Student } from '../model/student.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Teacher } from '@angular-challenges/ngrx-notification/model';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
import { Teacher } from '../model/teacher.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
export type PushType = 'teacher' | 'student';
|
||||
|
||||
export interface Push {
|
||||
type: PushType;
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import {
|
||||
incrementalNumber,
|
||||
randFirstName,
|
||||
randLastName,
|
||||
randWord,
|
||||
} from '@ngneat/falso';
|
||||
import { Push } from './push.model';
|
||||
|
||||
export interface Student extends Push {
|
||||
id: number;
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
school: string;
|
||||
}
|
||||
|
||||
const factoryStudent = incrementalNumber();
|
||||
|
||||
export const randStudent = (): Student => ({
|
||||
id: factoryStudent(),
|
||||
firstname: randFirstName(),
|
||||
lastname: randLastName(),
|
||||
school: randWord(),
|
||||
type: 'student',
|
||||
});
|
||||
|
||||
export const isStudent = (notif: Push): notif is Student => {
|
||||
return notif.type === 'student';
|
||||
};
|
||||
@@ -1,37 +0,0 @@
|
||||
import {
|
||||
incrementalNumber,
|
||||
rand,
|
||||
randFirstName,
|
||||
randLastName,
|
||||
} from '@ngneat/falso';
|
||||
import { Push } from './push.model';
|
||||
|
||||
export const subject = [
|
||||
'Sciences',
|
||||
'History',
|
||||
'English',
|
||||
'Maths',
|
||||
'Sport',
|
||||
] as const;
|
||||
export type Subject = typeof subject[number];
|
||||
|
||||
export interface Teacher extends Push {
|
||||
id: number;
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
subject: Subject;
|
||||
}
|
||||
|
||||
const factoryTeacher = incrementalNumber();
|
||||
|
||||
export const randTeacher = () => ({
|
||||
id: factoryTeacher(),
|
||||
firstname: randFirstName(),
|
||||
lastname: randLastName(),
|
||||
subject: rand(subject),
|
||||
type: 'teacher',
|
||||
});
|
||||
|
||||
export const isTeacher = (notif: Push): notif is Teacher => {
|
||||
return notif.type === 'teacher';
|
||||
};
|
||||
@@ -1,8 +1,8 @@
|
||||
import { APP_INITIALIZER, enableProdMode, inject } from '@angular/core';
|
||||
import { bootstrapApplication } from '@angular/platform-browser';
|
||||
import { of } from 'rxjs';
|
||||
import { PushService } from '../../../libs/ngrx-notification/backend/src/lib/push.service';
|
||||
import { AppComponent } from './app/app.component';
|
||||
import { PushService } from './app/backend/push.service';
|
||||
import { NotificationService } from './app/data-access/notification.service';
|
||||
import { environment } from './environments/environment';
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"include": ["**/*.ts"],
|
||||
"include": [
|
||||
"**/*.ts",
|
||||
"../../libs/ngrx-notification/backend/src/lib/push.service.ts"
|
||||
],
|
||||
"compilerOptions": {
|
||||
"types": []
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user