feat(debug): Added ability to mock network

This commit is contained in:
Jim Martens 2023-11-18 21:29:56 +01:00
parent 4a20d11163
commit 657e4c4ac4
3 changed files with 29 additions and 2 deletions

View File

@ -16,11 +16,19 @@ export class FormationsService {
};
private formationsURL = environment.backendURL + '/formation';
private knownFormations: Map<string, Formation> = new Map<string, Formation>();
private formations: Formation[] = [];
constructor(private readonly http: HttpClient,
private readonly messageService: MessagesService) {
}
fetchFormations(): Observable<Formation[]> {
if (environment.mockNetwork) {
this.formations = JSON.parse(localStorage.getItem("formations") || '[]');
return of(this.formations);
}
return this.http.get<Formation[]>(this.formationsURL, this.httpOptions)
.pipe(
catchError(this.handleError<Formation[]>('fetchFormations', []))
@ -28,6 +36,12 @@ export class FormationsService {
}
storeFormation(formation: Formation): Observable<Formation> {
if (environment.mockNetwork) {
this.knownFormations.set(formation.id, formation);
this.storeFormationsInLocalStorage();
return of(formation);
}
return this.http.put<Formation>(
this.formationsURL + '/' + encodeURIComponent(formation.id),
formation,
@ -38,6 +52,12 @@ export class FormationsService {
}
deleteFormation(formation: Formation): Observable<ArrayBuffer> {
if (environment.mockNetwork) {
this.knownFormations.delete(formation.id);
this.storeFormationsInLocalStorage();
return of(new ArrayBuffer(0));
}
return this.http.delete<ArrayBuffer>(
this.formationsURL + '/' + encodeURIComponent(formation.id),
this.httpOptions
@ -46,6 +66,11 @@ export class FormationsService {
)
}
private storeFormationsInLocalStorage() {
this.formations = Array.from(this.knownFormations.values());
localStorage.setItem("formations", JSON.stringify(this.formations));
}
/**
* Handle Http operation that failed.
* Let the app continue.

View File

@ -3,5 +3,6 @@ export const environment = {
backendURL: "http://localhost:12000/v1",
keycloakURL: "https://id.2martens.de",
realm: "2martens",
clientId: "tsw-timetable-frontend"
clientId: "tsw-timetable-frontend",
mockNetwork: true
};

View File

@ -2,7 +2,8 @@ export const environment = {
backendURL: "https://api.2martens.de/v1",
keycloakURL: "https://id.2martens.de",
realm: "2martens",
clientId: "tsw-timetable-frontend"
clientId: "tsw-timetable-frontend",
mockNetwork: false
};
/*