Skip to content

Commit

Permalink
fix(frontend): add update of list every Five minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Aug 14, 2024
1 parent cb87fe6 commit 1270cb1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
16 changes: 16 additions & 0 deletions packages/backend/src/usagers/controllers/usagers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ export class UsagersController {
};
}

@Get("update-manage")
@AllowUserStructureRoles(...USER_STRUCTURE_ROLE_ALL)
public async updateManage(@CurrentUser() user: UserStructureAuthenticated) {
return usagerRepository
.createQueryBuilder()
.select(joinSelectFields(USAGER_LIGHT_ATTRIBUTES))
.where(
`"structureId" = :structureId AND "updatedAt" >= :fiveMinutesAgo`,
{
structureId: user.structureId,
fiveMinutesAgo: new Date(Date.now() - 5 * 60 * 1000),
}
)
.getRawMany();
}

@Post("search-radies")
@AllowUserStructureRoles(...USER_STRUCTURE_ROLE_ALL)
public async searchInRadies(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { UserStructure } from "@domifa/common";
import { MatomoTracker } from "ngx-matomo-client";
import { AuthService, CustomToastService } from "../../../shared/services";

const AUTO_REFRESH_PERIOD = 300000; // 5 minutes
const FIVE_MINUTES = 5 * 60 * 1000;

@Component({
animations: [fadeInOut],
Expand Down Expand Up @@ -152,12 +152,6 @@ 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 @@ -173,9 +167,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]) =>
this.findRemoteUsagers(chargerTousRadies, searchString)
),
switchMap(([searchString, chargerTousRadies]) => {
return this.findRemoteUsagers(chargerTousRadies, searchString);
}),
tap((searchString: string) => {
this.filters.searchString = searchString ?? null;
this.filters.page = 0;
Expand All @@ -189,6 +183,20 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy {
})
);

this.subscription.add(
timer(FIVE_MINUTES, FIVE_MINUTES)
.pipe(
tap(() => {
this.searching = true;
}),
switchMap(() => this.usagerService.updateManage())
)
.subscribe(() => {
this.searching = false;
this.filters$.next(this.filters);
})
);

this.subscription.add(
this.filters$.subscribe((filters) => {
this.applyFilters({
Expand All @@ -201,31 +209,22 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy {

private loadDataFromAPI() {
this.subscription.add(
timer(0, AUTO_REFRESH_PERIOD)
this.chargerTousRadies$
.pipe(
tap(() => {
this.searching = true;
}),
switchMap((chargerTousRadies) =>
this.usagerService.getSearchPageUsagerData({ chargerTousRadies })
),
switchMap(() => this.chargerTousRadies$),
switchMap((chargerTousRadies) => {
this.searching = true;
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);
})
switchMap((chargerTousRadies) =>
this.findRemoteUsagers(chargerTousRadies, this.filters.searchString)
)
)
.subscribe()
.subscribe(() => {
this.searching = false;
})
);
}

Expand All @@ -235,7 +234,7 @@ export class ManageUsagersPageComponent implements OnInit, OnDestroy {
): Observable<string> {
if (
!chargerTousRadies &&
searchString.length >= 3 &&
searchString?.length >= 3 &&
(this.filters.statut === "TOUS" || this.filters.statut === "RADIE")
) {
this.searching = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,16 @@ export class ManageUsagersService {
})
);
}

public updateManage(): Observable<UsagerLight[]> {
return this.http
.get<UsagerLight[]>(`${environment.apiUrl}usagers/update-manage`)
.pipe(
tap((usagers: UsagerLight[]) => {
usagers.forEach((usager) =>
this.store.dispatch(cacheManager.updateUsager({ usager }))
);
})
);
}
}

0 comments on commit 1270cb1

Please sign in to comment.