From d4699a5b68de3db52b40177e4b70370c0b10a1f9 Mon Sep 17 00:00:00 2001 From: jdegand <70610011+jdegand@users.noreply.github.com> Date: Mon, 8 Jan 2024 20:44:08 -0500 Subject: [PATCH] fix: better typing fake-db-service --- .../backend/src/lib/fake-db.service.ts | 68 +++++++++++-------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/libs/ngrx-notification/backend/src/lib/fake-db.service.ts b/libs/ngrx-notification/backend/src/lib/fake-db.service.ts index e8ee0cd..6aaf924 100644 --- a/libs/ngrx-notification/backend/src/lib/fake-db.service.ts +++ b/libs/ngrx-notification/backend/src/lib/fake-db.service.ts @@ -7,12 +7,14 @@ import { Injectable } from '@angular/core'; import { randNumber } from '@ngneat/falso'; import { ComponentStore } from '@ngrx/component-store'; -@Injectable({ providedIn: 'root' }) -export class FakeDBService extends ComponentStore<{ +interface AppState { teachers: Teacher[]; students: Student[]; schools: School[]; -}> { +} + +@Injectable({ providedIn: 'root' }) +export class FakeDBService extends ComponentStore { readonly teachers$ = this.select((state) => state.teachers); readonly randomTeacher$ = this.select( this.teachers$, @@ -35,33 +37,45 @@ export class FakeDBService extends ComponentStore<{ super({ teachers: [], students: [], schools: [] }); } - addTeacher = this.updater((state, teacher: Teacher) => ({ - ...state, - teachers: [...state.teachers, teacher], - })); + addTeacher = this.updater( + (state, teacher: Teacher): AppState => ({ + ...state, + teachers: [...state.teachers, teacher], + }), + ); - updateTeacher = this.updater((state, teacher: Teacher) => ({ - ...state, - teachers: state.teachers.map((t) => (t.id === teacher.id ? teacher : t)), - })); + updateTeacher = this.updater( + (state, teacher: Teacher): AppState => ({ + ...state, + teachers: state.teachers.map((t) => (t.id === teacher.id ? teacher : t)), + }), + ); - addStudent = this.updater((state, student: Student) => ({ - ...state, - students: [...state.students, student], - })); + addStudent = this.updater( + (state, student: Student): AppState => ({ + ...state, + students: [...state.students, student], + }), + ); - updateSudent = this.updater((state, student: Student) => ({ - ...state, - students: state.students.map((t) => (t.id === student.id ? student : t)), - })); + updateSudent = this.updater( + (state, student: Student): AppState => ({ + ...state, + students: state.students.map((t) => (t.id === student.id ? student : t)), + }), + ); - addSchool = this.updater((state, school: School) => ({ - ...state, - schools: [...state.schools, school], - })); + addSchool = this.updater( + (state, school: School): AppState => ({ + ...state, + schools: [...state.schools, school], + }), + ); - updateSchool = this.updater((state, school: School) => ({ - ...state, - schools: state.schools.map((t) => (t.id === school.id ? school : t)), - })); + updateSchool = this.updater( + (state, school: School): AppState => ({ + ...state, + schools: state.schools.map((t) => (t.id === school.id ? school : t)), + }), + ); }