From be11dd7a7555915c7917a4e21864cc0de1982959 Mon Sep 17 00:00:00 2001 From: Julien Corny Date: Tue, 10 Sep 2024 11:43:12 +0200 Subject: [PATCH] fix(frontend): 400 and 404 errors display --- frontend/app/services/hierarchy.service.ts | 18 +++++++---------- frontend/app/services/zh-data.service.ts | 11 +++++----- .../hierarchy/hierarchy.component.html | 4 ++++ .../app/zh-details/zh-details.component.html | 2 +- .../app/zh-details/zh-details.component.ts | 20 ++++++++++++++++++- .../tabs/tab9/zh-form-tab9.component.html | 5 ++++- 6 files changed, 41 insertions(+), 19 deletions(-) diff --git a/frontend/app/services/hierarchy.service.ts b/frontend/app/services/hierarchy.service.ts index 16aefa0d..c0f5e149 100644 --- a/frontend/app/services/hierarchy.service.ts +++ b/frontend/app/services/hierarchy.service.ts @@ -26,6 +26,7 @@ export class HierarchyService { public items: ItemModel[]; public rb_name: string; public isLoading: boolean = false; + public warning: string = ""; constructor( private _dataService: ZhDataService, @@ -40,8 +41,11 @@ export class HierarchyService { // get current zone humides getHierarchy(zhId, rb_name) { this.isLoading = true; + this.warning = ""; this.rb_name = rb_name; - this._dataService.getHierZh(zhId).subscribe( + this._dataService.getHierZh(zhId, { + "not-to-handle": "1" + }).subscribe( (data: HierarchyModel) => { this.items = this.setItems(data); }, @@ -49,17 +53,9 @@ export class HierarchyService { this.isLoading = false; this.items = []; if (error.status === 404) { - this._toastr.warning("La ZH n'est présente dans aucun bassin versant", '', { - closeButton: true, - }); + this.warning = "La ZH n'est présente dans aucun bassin versant"; } else if (error.status === 400) { - this._toastr.warning( - this._error['errors'].filter((i) => error.error['message'] === i.api)[0].front, - '', - { - closeButton: true, - } - ); + this.warning = this._error['errors'].filter((i) => error.error['message'] === i.api)[0].front } }, () => { diff --git a/frontend/app/services/zh-data.service.ts b/frontend/app/services/zh-data.service.ts index bcca0ad7..003be2b4 100644 --- a/frontend/app/services/zh-data.service.ts +++ b/frontend/app/services/zh-data.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; import { BehaviorSubject } from 'rxjs'; import { map } from 'rxjs/operators'; import { ConfigService } from '@geonature/services/config.service'; @@ -115,10 +115,11 @@ export class ZhDataService { return this._api.get(`${this.config.API_ENDPOINT}/zones_humides/${zhId}/taxa`); } - getHierZh(zhId: string) { - return this._api.get( - `${this.config.API_ENDPOINT}/zones_humides/${zhId}/hierarchy` - ); + getHierZh(zhId: string, headers?: HttpHeaders | { [header: string]: string | string[]; }) { + return this._api.get( + `${this.config.API_ENDPOINT}/zones_humides/${zhId}/hierarchy`, { + headers + }); } getPdf(zhId: number) { diff --git a/frontend/app/zh-details/hierarchy/hierarchy.component.html b/frontend/app/zh-details/hierarchy/hierarchy.component.html index a5a77ac4..5bddb6f5 100644 --- a/frontend/app/zh-details/hierarchy/hierarchy.component.html +++ b/frontend/app/zh-details/hierarchy/hierarchy.component.html @@ -1,5 +1,9 @@

Bassin versant : {{ data?.river_basin_name }}

+
+ {{ hierarchy.warning }} +
- + diff --git a/frontend/app/zh-details/zh-details.component.ts b/frontend/app/zh-details/zh-details.component.ts index c0909619..78609fdc 100644 --- a/frontend/app/zh-details/zh-details.component.ts +++ b/frontend/app/zh-details/zh-details.component.ts @@ -6,10 +6,11 @@ import { ZhDataService } from '../services/zh-data.service'; import { ErrorTranslatorService } from '../services/error-translator.service'; import { Rights } from '../models/rights'; import { ToastrService } from 'ngx-toastr'; -import { GeoJSON } from 'leaflet'; +import { Subscription } from 'rxjs'; import * as L from 'leaflet'; import { DetailsModel } from './models/zh-details.model'; +import { HierarchyService } from '../services/hierarchy.service'; @Component({ selector: 'zh-details', @@ -24,10 +25,13 @@ export class ZhDetailsComponent implements OnInit, AfterViewInit { public rights: Rights; public expanded: boolean = false; public onError: boolean = false; + private $_currentZhSub: Subscription; + public currentZh: any; constructor( private _mapService: MapService, private _zhService: ZhDataService, + public hierarchy: HierarchyService, private _route: ActivatedRoute, private _toastr: ToastrService, private _error: ErrorTranslatorService @@ -37,6 +41,16 @@ export class ZhDetailsComponent implements OnInit, AfterViewInit { this.id_zh = this._route.snapshot.params['id']; this.getRights(this.id_zh); this.getData(); + this.getCurrentZh(); + } + + getCurrentZh() { + this._zhService.getZhById(this.id_zh).subscribe((zh: any) => { + if (zh) { + this.currentZh = zh; + this.hierarchy.getHierarchy(zh.id, zh.properties.bassin_versant); + } + }); } getRights(idZh: number) { @@ -107,4 +121,8 @@ export class ZhDetailsComponent implements OnInit, AfterViewInit { onWrapAll() { this.expanded = !this.expanded; } + + ngOnDestroy() { + if (this.$_currentZhSub) this.$_currentZhSub.unsubscribe(); + } } diff --git a/frontend/app/zh-forms/tabs/tab9/zh-form-tab9.component.html b/frontend/app/zh-forms/tabs/tab9/zh-form-tab9.component.html index e2e63751..ebbe48c4 100755 --- a/frontend/app/zh-forms/tabs/tab9/zh-form-tab9.component.html +++ b/frontend/app/zh-forms/tabs/tab9/zh-form-tab9.component.html @@ -3,8 +3,11 @@

Hiérarchisation

Bassin versant : {{ hierarchy.rb_name }}

- +
+ {{ hierarchy.warning }} +