Skip to content

Commit

Permalink
fix(frontend): search remote usagers after a reload
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Aug 14, 2024
1 parent 1883ba4 commit cb87fe6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 36 deletions.
25 changes: 17 additions & 8 deletions packages/frontend/src/app/interceptors/server-error.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { Injectable, Injector } from "@angular/core";
import { Observable, throwError, timer } from "rxjs";
import { catchError, retry } from "rxjs/operators";
import { AuthService } from "../modules/shared/services/auth.service";
import { captureException } from "@sentry/angular";
import { captureException, getCurrentScope } from "@sentry/angular";
import { CustomToastService } from "../modules/shared/services";
import { Router } from "@angular/router";

const MAX_RETRIES = 1;
const RETRY_DELAY = 800;
const ERROR_STATUS_CODES_TO_RETRY = [0, 408, 500, 502, 503, 504];
const MAX_RETRIES = 2;
const RETRY_DELAY = 1000;
const ERROR_STATUS_CODES_TO_RETRY = [0, 408, 409, 444, 502, 503, 504, 520];

@Injectable({
providedIn: "root",
Expand All @@ -33,6 +33,16 @@ export class ServerErrorInterceptor implements HttpInterceptor {
const toastr = this.injector.get(CustomToastService);
const router = this.injector.get(Router);

if (authService?.currentUserValue) {
const user = authService.currentUserValue;
getCurrentScope().setTag("structure", user?.structureId?.toString());
getCurrentScope().setUser({
email: user.email,
username:
"STRUCTURE " + user?.structureId?.toString() + " : " + user?.prenom,
});
}

return next.handle(request).pipe(
retry({
count: MAX_RETRIES,
Expand Down Expand Up @@ -66,11 +76,10 @@ export class ServerErrorInterceptor implements HttpInterceptor {
} else if (error.status === 404) {
toastr.error("La page que vous recherchez n'existe pas");
router.navigate(["404"]);
} else if (error.status >= 500) {
} else {
toastr.error(
"Une erreur serveur est survenue. Nos équipes ont été notifiées."
);
captureException(error);
}
}
this.logError(request, error);
Expand All @@ -84,14 +93,14 @@ export class ServerErrorInterceptor implements HttpInterceptor {
}

private logError(request: HttpRequest<any>, error: HttpErrorResponse): void {
captureException(error, { data: request });
console.error("HTTP Error", {
console.error(error.message, {
status: error.status,
statusText: error.statusText,
url: error.url,
message: error.message,
error: error.error,
request,
});
captureException(error, { data: request });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy {
if (!searchPageLoadedUsagersData.dataLoaded) {
this.loadDataFromAPI();
} else {
if (
searchPageLoadedUsagersData.usagersRadiesFirsts.length >=
searchPageLoadedUsagersData.usagersRadiesTotalCount
) {
this.chargerTousRadies$.next(true);
}
this.updateComponentState(searchPageLoadedUsagersData);
}
}
Expand All @@ -167,29 +173,9 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy {
map((value: string) => value.trim()),
filter((value: string) => value !== this.filters.searchString),
withLatestFrom(this.chargerTousRadies$),
switchMap(([searchString, chargerTousRadies]) => {
if (
!chargerTousRadies &&
searchString.length >= 3 &&
(this.filters.statut === "TOUS" || this.filters.statut === "RADIE")
) {
this.searching = true;
return this.usagerService
.getSearchPageRemoteSearchRadies({
searchString,
})
.pipe(
catchError(() => {
this.searching = false;
this.toastr.error(
"La recherche n'a pas abouti, merci de réessayer dans quelques instants"
);
return searchString;
})
);
}
return of(searchString);
}),
switchMap(([searchString, chargerTousRadies]) =>
this.findRemoteUsagers(chargerTousRadies, searchString)
),
tap((searchString: string) => {
this.filters.searchString = searchString ?? null;
this.filters.page = 0;
Expand Down Expand Up @@ -226,12 +212,50 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy {
return this.usagerService.getSearchPageUsagerData({
chargerTousRadies,
});
}),
switchMap(() => this.chargerTousRadies$),
switchMap((chargerTousRadies) => {
// Call remote usagers to update list
if (!chargerTousRadies) {
return this.findRemoteUsagers(
chargerTousRadies,
this.filters.searchString
);
}
return of(chargerTousRadies);
})
)
.subscribe()
);
}

private findRemoteUsagers(
chargerTousRadies: boolean,
searchString: string
): Observable<string> {
if (
!chargerTousRadies &&
searchString.length >= 3 &&
(this.filters.statut === "TOUS" || this.filters.statut === "RADIE")
) {
this.searching = true;
return this.usagerService
.getSearchPageRemoteSearchRadies({
searchString,
})
.pipe(
catchError(() => {
this.searching = false;
this.toastr.error(
"La recherche n'a pas abouti, merci de réessayer dans quelques instants"
);
return searchString;
})
);
}
return of(searchString);
}

private updateComponentState(
searchPageLoadedUsagersData: SearchPageLoadedUsagersData
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class DecisionValideFormComponent implements OnInit, OnDestroy {

this.subscription.add(
this.valideForm.get("customRef")?.valueChanges.subscribe((value) => {
if (value) {
if (value?.trim()) {
this.checkDuplicatesRef(value);
} else {
this.duplicates = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export class StepEtatCivilComponent
}
public isDuplicateName(): void {
if (
this.usagerForm.controls.nom.value &&
this.usagerForm.controls.prenom.value
this.usagerForm.controls.nom?.value?.trim() &&
this.usagerForm.controls.prenom?.value?.trim()
) {
const params: {
nom: string;
Expand All @@ -102,7 +102,7 @@ export class StepEtatCivilComponent
} = {
nom: this.usagerForm.controls.nom.value,
prenom: this.usagerForm.controls.prenom.value,
usagerRef: this.usager.ref || null,
usagerRef: this.usager.ref ?? null,
};

this.subscription.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export class UsagerNotesActionsComponent implements OnDestroy {
})
.subscribe({
next: () => {
this.toastService.success("Note supprimée avec succès");
this.closeModal();
this.toastService.success("Note supprimée avec succès");
},
error: () => {
this.toastService.error("Impossible de supprimer la note");
Expand All @@ -90,6 +90,7 @@ export class UsagerNotesActionsComponent implements OnDestroy {
const message = this.note.archived
? "Note désarchivée avec succès"
: "Note archivée avec succès";

this.subscription.add(
this.usagerNotesService
.archiveNote({
Expand Down

0 comments on commit cb87fe6

Please sign in to comment.