From 213aac98e7276104917a10ca0c5d09e0b76f7ee6 Mon Sep 17 00:00:00 2001 From: Orlando M Guerreiro Date: Sun, 22 Jun 2025 12:03:42 +0100 Subject: [PATCH] Auto-seleccionar o periodo mais recente (fixes #11) --- ...ard-component-accordion-table.component.ts | 11 +++--- .../entities/dashboard/dashboard.routes.ts | 8 +++++ .../preview/dashboard-preview.component.ts | 36 +++++++++---------- .../app/layouts/navbar/navbar.component.html | 4 +-- .../period-selector.component.ts | 9 +++++ 5 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/main/webapp/app/entities/dashboard-component/factory/widgets/dashboard-component-accordion-table.component.ts b/src/main/webapp/app/entities/dashboard-component/factory/widgets/dashboard-component-accordion-table.component.ts index 0083c5c..d7b0983 100644 --- a/src/main/webapp/app/entities/dashboard-component/factory/widgets/dashboard-component-accordion-table.component.ts +++ b/src/main/webapp/app/entities/dashboard-component/factory/widgets/dashboard-component-accordion-table.component.ts @@ -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) => { 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; } diff --git a/src/main/webapp/app/entities/dashboard/dashboard.routes.ts b/src/main/webapp/app/entities/dashboard/dashboard.routes.ts index 676c2f0..493e2f1 100644 --- a/src/main/webapp/app/entities/dashboard/dashboard.routes.ts +++ b/src/main/webapp/app/entities/dashboard/dashboard.routes.ts @@ -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, diff --git a/src/main/webapp/app/entities/dashboard/preview/dashboard-preview.component.ts b/src/main/webapp/app/entities/dashboard/preview/dashboard-preview.component.ts index 56f9353..95de022 100644 --- a/src/main/webapp/app/entities/dashboard/preview/dashboard-preview.component.ts +++ b/src/main/webapp/app/entities/dashboard/preview/dashboard-preview.component.ts @@ -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) => res.body ?? [])) - .subscribe((periods: IPeriod[]) => { - this.periodsSharedCollection = periods; - }); - } - protected setPageTitles(view: string | null): void { this.pageTitle = ''; this.pageSubTitle = ''; diff --git a/src/main/webapp/app/layouts/navbar/navbar.component.html b/src/main/webapp/app/layouts/navbar/navbar.component.html index 0e3caf2..5f93d7b 100644 --- a/src/main/webapp/app/layouts/navbar/navbar.component.html +++ b/src/main/webapp/app/layouts/navbar/navbar.component.html @@ -45,7 +45,7 @@ > (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;