Auto-seleccionar o periodo mais recente (fixes #11)

This commit is contained in:
Orlando M Guerreiro 2025-06-22 12:03:42 +01:00
parent 13618db55a
commit 213aac98e7
5 changed files with 42 additions and 26 deletions

View file

@ -91,12 +91,12 @@ export class DashboardComponentAccordionTable implements OnInit {
}
}
});
// Listen to changes in PeriodSelector
// Get the currently selected ORGANIZATION AND listen to changes in PeriodSelector
this.subscriptionPeriod = this
.envService
.selectedPeriod
.pipe( skip(1)) // Ignore the current value. Just want to react to changes
/* .pipe( skip(1)) // Ignore's the current value. If you want to react ONLY to changes */
.subscribe(period => {
if(period){
// Calculate the latest periodVersionId
@ -105,9 +105,10 @@ export class DashboardComponentAccordionTable implements OnInit {
.subscribe({
next: (version: HttpResponse<IPeriodVersion>) => {
if (version.body) {
const periodVersion = version.body;
this.periodVersionId = periodVersion.id;
this.periodVersion = version.body;
this.periodVersionId = this.periodVersion.id;
} else {
this.periodVersion = null;
this.periodVersionId = null;
}

View file

@ -6,6 +6,14 @@ import { DashboardPreviewComponent } from './preview/dashboard-preview.component
// import DashboardResolve from './route/dashboard-routing-resolve.service';
const dashboardRoute: Routes = [
{
path: ':view',
component: DashboardPreviewComponent,
canActivate: [UserRouteAccessService],
data: {
authorities: ['ROLE_ADMIN', 'ROLE_MANAGER', 'ROLE_COORDINATOR', 'ROLE_USER'],
},
},
{
path: ':view/:period',
component: DashboardPreviewComponent,

View file

@ -23,10 +23,9 @@ export class DashboardPreviewComponent implements OnInit, AfterViewInit {
@ViewChild('dashboardContainer', { read: ViewContainerRef }) container!: ViewContainerRef;
dashboardComponents?: IDashboardComponent[];
periodsSharedCollection: IPeriod[] = [];
dashboardView: string | null = null;
periodId: number = 0;
periodId: number | null = null;
periodVersionId: number | null = null;
isLoading = false;
@ -54,8 +53,11 @@ export class DashboardPreviewComponent implements OnInit, AfterViewInit {
this.dashboardView = params.get('view') ?? null;
// used the operator (+) that auto-casts a string to a number
this.periodId = +(params.get('period') ?? 0);
// this.periodId = +(params.get('period') ?? 0);
if (params.get('period') != null) {
this.periodId = +(params.get('period')!);
}
// periodVersion might not be defined. In this case, it will calculate the most recent version
if (params.get('period_version') != null) {
this.periodVersionId = +(params.get('period_version')!);
@ -68,8 +70,12 @@ export class DashboardPreviewComponent implements OnInit, AfterViewInit {
this.build();
});
// Get the latest PeriodVersion for the requested Period
if (this.periodVersionId == null) {
if (this.periodId == null) {
// Get the latest Period + PeriodVersion
// Do nothing. The period selector will auto-select the moust recent period
} else if (this.periodId && this.periodVersionId == null) {
// Get the latest PeriodVersion for the requested Period
this.periodService
.lastVersion(this.periodId)
.subscribe({
@ -89,8 +95,6 @@ export class DashboardPreviewComponent implements OnInit, AfterViewInit {
//Fire Loading of the dashboard
this.build();
}
this.loadRelationshipsOptions();
}
onSearch(): void {
@ -102,11 +106,14 @@ export class DashboardPreviewComponent implements OnInit, AfterViewInit {
}
build(): void {
if (this.periodVersionId == null) {
// Can't load dashboard without a version
// ALWAYS load, even if no period is selected.
/*
if (this.periodId == null || this.periodVersionId == null) {
// Can't load dashboard without a period || periodVersion
this.dashboardComponents = []; // Clear components
return;
}
*/
// Load a list of dashboarComponent's to render
this.loadDashboards().subscribe({
@ -141,15 +148,6 @@ export class DashboardPreviewComponent implements OnInit, AfterViewInit {
}
}
protected loadRelationshipsOptions(): void {
this.periodService
.query()
.pipe(map((res: HttpResponse<IPeriod[]>) => res.body ?? []))
.subscribe((periods: IPeriod[]) => {
this.periodsSharedCollection = periods;
});
}
protected setPageTitles(view: string | null): void {
this.pageTitle = '';
this.pageSubTitle = '';

View file

@ -45,7 +45,7 @@
>
<a
class="nav-link"
routerLink="/dashboard/EMISSIONS/1500"
routerLink="/dashboard/EMISSIONS"
routerLinkActive="active"
[routerLinkActiveOptions]="{ exact: true }"
(click)="collapseNavbar()"
@ -68,7 +68,7 @@
>
<a
class="nav-link"
routerLink="/dashboard/INDICATORS/1500"
routerLink="/dashboard/INDICATORS"
routerLinkActive="active"
[routerLinkActiveOptions]="{ exact: true }"
(click)="collapseNavbar()"

View file

@ -63,6 +63,7 @@ export class PeriodSelectorComponent implements OnInit {
const queryObject: any = {
eagerload: true,
sort: this.sortService.buildSortParam(this.sortState()),
};
return this.periodService.query(queryObject).pipe(tap(() => (this.isLoading = false)));
}
@ -71,6 +72,14 @@ export class PeriodSelectorComponent implements OnInit {
const dataFromBody = this.fillComponentAttributesFromResponseBody(response.body);
this.periods = this.refineData(dataFromBody);
if (this.periods.length >= 1) {
// Sort the periods
this.periods.sort((a, b) => {
if (!a.beginDate && !b.beginDate) return 0;
if (!a.beginDate) return 1; // null dates go last
if (!b.beginDate) return -1;
return b.beginDate.valueOf() - a.beginDate.valueOf();
});
if (this.defaultSelectedPeriodId) {
// Select the provided default Period
this.selectedPeriod = this.periods.find(period => period.id === this.defaultSelectedPeriodId) || undefined;