From bc202faca3cfe2dd86a07464670ae4395ee9c7fd Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Fri, 5 Jan 2024 11:52:16 +0100 Subject: [PATCH] fix: Match DTO of backend fix: Convert string from backend to enum in frontend --- src/app/dashboard/dashboard.component.html | 2 +- .../create-timetable.component.html | 2 +- .../create-timetable.component.ts | 8 +++++++- src/app/timetables/model/timetable.ts | 16 ++++++++++++---- src/app/timetables/service/timetable.service.ts | 16 ++++++++++++++-- src/app/timetables/store/timetables.actions.ts | 6 ++++++ src/app/timetables/store/timetables.effects.ts | 12 +++++++++--- src/app/timetables/store/timetables.reducer.ts | 5 +++-- src/app/timetables/timetables.component.html | 2 +- .../update-timetable.component.html | 2 +- 10 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/app/dashboard/dashboard.component.html b/src/app/dashboard/dashboard.component.html index 934b791..f29fb3e 100644 --- a/src/app/dashboard/dashboard.component.html +++ b/src/app/dashboard/dashboard.component.html @@ -62,7 +62,7 @@ {{ timetable.name }} - {{ timetable.route.name }} + {{ timetable.routeName }} {{ timetable.date | date }} {{ TimetableStateTexts[timetable.state] }} {{ timetable.numberOfServices }} diff --git a/src/app/timetables/create-timetable/create-timetable.component.html b/src/app/timetables/create-timetable/create-timetable.component.html index 4a24189..6f5e100 100644 --- a/src/app/timetables/create-timetable/create-timetable.component.html +++ b/src/app/timetables/create-timetable/create-timetable.component.html @@ -45,7 +45,7 @@ name="route" interface="popover" [compareWith]="compareWithRoute" - [(ngModel)]="timetable.route" + [(ngModel)]="route" > diff --git a/src/app/timetables/create-timetable/create-timetable.component.ts b/src/app/timetables/create-timetable/create-timetable.component.ts index 4332912..e5b8f41 100644 --- a/src/app/timetables/create-timetable/create-timetable.component.ts +++ b/src/app/timetables/create-timetable/create-timetable.component.ts @@ -34,6 +34,7 @@ import {pencilOutline, pencilSharp, trashOutline, trashSharp} from "ionicons/ico import {DEFAULT_TIMETABLE} from "../model/timetable"; import {addTimetableAction} from "../store/timetables.actions"; import {AsyncPipe, NgForOf} from "@angular/common"; +import {DEFAULT_ROUTE, Route} from "../../routes/model/route"; @Component({ selector: 'app-create-timetable', @@ -52,6 +53,7 @@ export class CreateTimetableComponent extends TimetableComponent { @ViewChild(IonModal) modal: IonModal | undefined; + route: Route = {...DEFAULT_ROUTE}; minDate = new Date(); minDateString: string; @@ -74,7 +76,11 @@ export class CreateTimetableComponent extends TimetableComponent { confirm() { this.store.dispatch(addTimetableAction({ - payload: {...this.timetable} + payload: { + ...this.timetable, + routeId: this.route.id, + routeName: this.route.name + } })) this.dismissed.emit(true); this.timetable = {...DEFAULT_TIMETABLE}; diff --git a/src/app/timetables/model/timetable.ts b/src/app/timetables/model/timetable.ts index a44fde2..cea2842 100644 --- a/src/app/timetables/model/timetable.ts +++ b/src/app/timetables/model/timetable.ts @@ -1,9 +1,8 @@ -import {DEFAULT_ROUTE, Route} from "../../routes/model/route"; - export interface Timetable { id: string; name: string; - route: Route; + routeId: string; + routeName: string; date: string; state: TimetableState; numberOfServices: number; @@ -17,6 +16,14 @@ export enum TimetableState { READY_FOR_USAGE } +export const TimetableStateIndices: { [name: string]: number } = { + NEW: 0, + PROCESSING: 1, + ENTER_FORMATIONS: 2, + LINK_SERVICES: 3, + READY_FOR_USAGE: 4 +} + export const TimetableStateTexts: { [name: number]: string } = { 0: $localize`New`, 1: $localize`Processing`, @@ -28,7 +35,8 @@ export const TimetableStateTexts: { [name: number]: string } = { export const DEFAULT_TIMETABLE: Timetable = { id: '', name: '', - route: {...DEFAULT_ROUTE}, + routeId: '', + routeName: '', date: '', state: TimetableState.NEW, numberOfServices: 0 diff --git a/src/app/timetables/service/timetable.service.ts b/src/app/timetables/service/timetable.service.ts index ad05108..e87d34f 100644 --- a/src/app/timetables/service/timetable.service.ts +++ b/src/app/timetables/service/timetable.service.ts @@ -1,9 +1,9 @@ import {Injectable} from '@angular/core'; import {HttpClient, HttpHeaders} from "@angular/common/http"; import {environment} from "../../../environments/environment"; -import {catchError, Observable, of} from "rxjs"; +import {catchError, map, Observable, of} from "rxjs"; import {ErrorService} from "../../errors/error.service"; -import {Timetable} from "../model/timetable"; +import {Timetable, TimetableStateIndices} from "../model/timetable"; @Injectable({ providedIn: 'root' @@ -33,6 +33,12 @@ export class TimetableService { return this.http.get(this.timetablesURL + '/' + encodeURIComponent(userId) + '/', this.httpOptions) .pipe( + map(timetables => timetables.map(timetable => { + return { + ...timetable, + state: TimetableStateIndices[timetable.state] + } + })), catchError(this.errorService.handleError('Timetables', 'fetchTimetables', environment.fallbackToMock ? this.timetables : [])) ); @@ -52,6 +58,12 @@ export class TimetableService { timetable, this.httpOptions ).pipe( + map(timetable => { + return { + ...timetable, + state: TimetableStateIndices[timetable.state] + } + }), catchError(this.errorService.handleError('Timetables', 'storeTimetable', timetable)) ) diff --git a/src/app/timetables/store/timetables.actions.ts b/src/app/timetables/store/timetables.actions.ts index 1840b57..8491fa9 100644 --- a/src/app/timetables/store/timetables.actions.ts +++ b/src/app/timetables/store/timetables.actions.ts @@ -18,6 +18,7 @@ export enum ActionTypes { AddTimetable = '[Timetables] Add Timetable', UpdateTimetable = '[Timetables] Update Timetable', + UpdateTimetableFromBackend = '[Timetables] Update Timetable From Backend', DeleteTimetable = '[Timetables] Delete Timetable', } @@ -70,6 +71,11 @@ export const updateTimetableAction = createAction( props<{ payload: Timetable }>() ); +export const updateTimetableFromBackendAction = createAction( + ActionTypes.UpdateTimetableFromBackend, + props<{ payload: Timetable }>() +); + export const deleteTimetableAction = createAction( ActionTypes.DeleteTimetable, props<{ payload: Timetable }>() diff --git a/src/app/timetables/store/timetables.effects.ts b/src/app/timetables/store/timetables.effects.ts index d5a1976..1a805d6 100644 --- a/src/app/timetables/store/timetables.effects.ts +++ b/src/app/timetables/store/timetables.effects.ts @@ -1,7 +1,12 @@ import {Actions, createEffect, ofType} from "@ngrx/effects"; import {inject} from "@angular/core"; import {combineLatestWith, map, switchMap} from "rxjs"; -import {addTimetableAction, deleteTimetableAction, updateTimetableAction} from "./timetables.actions"; +import { + addTimetableAction, + deleteTimetableAction, + updateTimetableAction, + updateTimetableFromBackendAction +} from "./timetables.actions"; import {TimetableService} from "../service/timetable.service"; import {AuthService} from "../../auth/service/auth.service"; @@ -14,10 +19,11 @@ export const storeTimetable = createEffect(( ofType(addTimetableAction, updateTimetableAction), map(action => action.payload), combineLatestWith(authService.getUser$()), - switchMap(([timetable, user]) => timetableService.storeTimetable(timetable, user.id)) + switchMap(([timetable, user]) => timetableService.storeTimetable(timetable, user.id)), + map(timetable => updateTimetableFromBackendAction({payload: timetable})) ); }, - {functional: true, dispatch: false}); + {functional: true}); export const deleteTimetable = createEffect(( actions$ = inject(Actions), diff --git a/src/app/timetables/store/timetables.reducer.ts b/src/app/timetables/store/timetables.reducer.ts index cf245e8..75afda6 100644 --- a/src/app/timetables/store/timetables.reducer.ts +++ b/src/app/timetables/store/timetables.reducer.ts @@ -9,7 +9,8 @@ import { loadAllServicesFinishedAction, loadAllTimetablesCancelledAction, loadAllTimetablesFinishedAction, - updateTimetableAction + updateTimetableAction, + updateTimetableFromBackendAction } from "./timetables.actions"; import {Service} from "../model/service"; import {Rotation} from "../model/rotation"; @@ -71,7 +72,7 @@ export const timetablesReducer = createReducer( ...state, timetables: [...state.timetables, action.payload] })), - on(updateTimetableAction, (state, action) => ({ + on(updateTimetableAction, updateTimetableFromBackendAction, (state, action) => ({ ...state, timetables: state.timetables.map((oldTimetable) => { if (oldTimetable.id == action.payload.id) { diff --git a/src/app/timetables/timetables.component.html b/src/app/timetables/timetables.component.html index 4f539d6..a5ef7f1 100644 --- a/src/app/timetables/timetables.component.html +++ b/src/app/timetables/timetables.component.html @@ -23,7 +23,7 @@ {{ timetable.name }} - {{ timetable.route.name }} + {{ timetable.routeName }} {{ timetable.date | date }} {{ TimetableStateTexts[timetable.state] }} {{ timetable.numberOfServices }} diff --git a/src/app/timetables/update-timetable/update-timetable.component.html b/src/app/timetables/update-timetable/update-timetable.component.html index db6887d..3c42298 100644 --- a/src/app/timetables/update-timetable/update-timetable.component.html +++ b/src/app/timetables/update-timetable/update-timetable.component.html @@ -38,7 +38,7 @@ labelPlacement="stacked" type="text" name="route" - [value]="timetable.route.name" + [value]="timetable.routeName" [readonly]="true" i18n-helperText helperText="Cannot be modified"