Skip to content

Commit

Permalink
fix(frontend): 400 and 404 errors display
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Corny committed Sep 10, 2024
1 parent edeb6db commit be11dd7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
18 changes: 7 additions & 11 deletions frontend/app/services/hierarchy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -40,26 +41,21 @@ 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);
},
(error) => {
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
}
},
() => {
Expand Down
11 changes: 6 additions & 5 deletions frontend/app/services/zh-data.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<HierarchyModel>(
`${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) {
Expand Down
4 changes: 4 additions & 0 deletions frontend/app/zh-details/hierarchy/hierarchy.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<h4 class="tabsSubtitle">Bassin versant : {{ data?.river_basin_name }}</h4>
<div *ngIf="hierarchy.warning" class="alert alert-warning mat-alert">
{{ hierarchy.warning }}
</div>
<zh-table
*ngIf="hierarchy.items?.length"
[tableCols]="hierarchy.hierTableCols"
[data]="hierarchy.items"
[bold_row_values]="hierarchy.bold_row_values"
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/zh-details/zh-details.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
[title]="'9. Hiérarchisation'"
[expanded]="expanded"
>
<zh-details-hierarchy [data]="zhDetails.hierarchy"></zh-details-hierarchy>
<zh-details-hierarchy [data]="hierarchy"></zh-details-hierarchy>
</collapse>
</mat-accordion>
</div>
Expand Down
20 changes: 19 additions & 1 deletion frontend/app/zh-details/zh-details.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -107,4 +121,8 @@ export class ZhDetailsComponent implements OnInit, AfterViewInit {
onWrapAll() {
this.expanded = !this.expanded;
}

ngOnDestroy() {
if (this.$_currentZhSub) this.$_currentZhSub.unsubscribe();
}
}
5 changes: 4 additions & 1 deletion frontend/app/zh-forms/tabs/tab9/zh-form-tab9.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ <h2 class="tabsTitle">Hiérarchisation</h2>

<div *ngIf="!hierarchy.isLoading">
<h4 class="tabsSubtitle">Bassin versant : {{ hierarchy.rb_name }}</h4>

<div *ngIf="hierarchy.warning" class="alert alert-warning mat-alert">
{{ hierarchy.warning }}
</div>
<zh-table
*ngIf="hierarchy.items?.length"
[tableCols]="hierarchy.hierTableCols"
[data]="hierarchy.items"
[bold_row_values]="hierarchy.bold_row_values"
Expand Down

0 comments on commit be11dd7

Please sign in to comment.