Skip to content

Commit eeaa370

Browse files
committed
fix: dashboard navigation was messy in front-office, when using templates
1 parent 3638c3d commit eeaa370

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

apps/front-office/src/app/application/application.component.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, HostListener, OnDestroy, OnInit } from '@angular/core';
2-
import { ActivatedRoute, Router } from '@angular/router';
2+
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
33
import { subject } from '@casl/ability';
44
import { TranslateService } from '@ngx-translate/core';
55
import {
@@ -12,7 +12,7 @@ import {
1212
AppAbility,
1313
} from '@oort-front/safe';
1414
import get from 'lodash/get';
15-
import { takeUntil } from 'rxjs/operators';
15+
import { filter, takeUntil } from 'rxjs/operators';
1616

1717
/**
1818
* Front-office Application component.
@@ -67,6 +67,14 @@ export class ApplicationComponent
6767
) {
6868
super();
6969
this.largeDevice = window.innerWidth > 1024;
70+
this.router.events
71+
.pipe(
72+
filter((event) => event instanceof NavigationEnd),
73+
takeUntil(this.destroy$)
74+
)
75+
.subscribe(() => {
76+
console.log('end');
77+
});
7078
}
7179

7280
/**

apps/front-office/src/app/application/pages/dashboard/dashboard.component.ts

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
ViewChild,
1111
} from '@angular/core';
1212
import { Dialog } from '@angular/cdk/dialog';
13-
import { ActivatedRoute, Router } from '@angular/router';
13+
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
1414
import {
1515
GetDashboardByIdQueryResponse,
1616
GET_DASHBOARD_BY_ID,
@@ -24,7 +24,7 @@ import {
2424
ButtonActionT,
2525
} from '@oort-front/safe';
2626
import { TranslateService } from '@ngx-translate/core';
27-
import { map, takeUntil } from 'rxjs/operators';
27+
import { filter, map, startWith, takeUntil } from 'rxjs/operators';
2828
import { Observable, firstValueFrom } from 'rxjs';
2929
import { SnackbarService } from '@oort-front/ui';
3030

@@ -104,49 +104,60 @@ export class DashboardComponent
104104
* Subscribes to the route to load the dashboard accordingly.
105105
*/
106106
ngOnInit(): void {
107-
this.route.params.pipe(takeUntil(this.destroy$)).subscribe((params) => {
108-
this.route.queryParams
109-
.pipe(takeUntil(this.destroy$))
110-
.subscribe((queryParams) => {
111-
if (queryParams.id) {
107+
/** Listen to router events navigation end, to get last version of params & queryParams. */
108+
this.router.events
109+
.pipe(
110+
filter((event) => event instanceof NavigationEnd),
111+
startWith(this.router), // initialize
112+
takeUntil(this.destroy$)
113+
)
114+
.subscribe(() => {
115+
// Reset scroll when changing page
116+
const pageContainer = document.getElementById('appPageContainer');
117+
if (pageContainer) pageContainer.scrollTop = 0;
118+
/** Extract main dashboard id */
119+
const id = this.route.snapshot.paramMap.get('id');
120+
/** Extract query id to load template */
121+
const queryId = this.route.snapshot.queryParamMap.get('id');
122+
if (id) {
123+
if (queryId) {
124+
// Try to load template
112125
this.showName = true;
113-
} else {
114-
this.showName = false;
115-
}
116-
});
117-
// Reset scroll when changing page
118-
const pageContainer = document.getElementById('appPageContainer');
119-
if (pageContainer) pageContainer.scrollTop = 0;
120-
this.route.queryParams
121-
.pipe(takeUntil(this.destroy$))
122-
.subscribe((queryParams) => {
123-
const viewId = queryParams.id;
124-
if (viewId) {
125-
this.initDashboardWithId(params.id).then(() => {
126-
// Find the id of the contextual dashboard and load it
127-
const dashboardsWithContext =
128-
this.dashboard?.page?.contentWithContext;
126+
this.initDashboardWithId(id).then(() => {
127+
const templates = this.dashboard?.page?.contentWithContext;
129128
const type = this.contextType;
130-
// find the contextual dashboard id in the list of dashboards from the parent dashboard
131-
// it's the one where the element or record id matches the one in the query params
132-
const dashboardWithContext = dashboardsWithContext?.find((d) => {
129+
// Find template from parent's templates, based on query params id
130+
const template = templates?.find((d) => {
131+
// If templates use reference data
133132
if (type === 'element')
134-
return 'element' in d && d.element === viewId;
133+
return (
134+
'element' in d && d.element.toString().trim() === queryId
135+
);
136+
// If templates use resource
135137
else if (type === 'record')
136-
return 'record' in d && d.record === viewId;
138+
return (
139+
'record' in d && d.record.toString().trim() === queryId
140+
);
137141
return false;
138142
});
139-
if (dashboardWithContext) {
140-
this.initDashboardWithId(dashboardWithContext.content);
143+
if (template) {
144+
// Load template, it will erase current dashboard
145+
this.initDashboardWithId(template.content).then(
146+
() => (this.loading = false)
147+
);
141148
} else {
149+
// Will use current template
150+
this.loading = false;
142151
return;
143152
}
144153
});
145154
} else {
146-
this.initDashboardWithId(params.id);
155+
// Don't use template, and directly load the dashboard from router's params
156+
this.showName = false;
157+
this.initDashboardWithId(id).then(() => (this.loading = false));
147158
}
148-
});
149-
});
159+
}
160+
});
150161
}
151162

152163
/**
@@ -203,15 +214,14 @@ export class DashboardComponent
203214
},
204215
})
205216
)
206-
.then(({ data, loading }) => {
217+
.then(({ data }) => {
207218
if (data.dashboard) {
208219
this.dashboard = data.dashboard;
209220
this.dashboardService.openDashboard(this.dashboard);
210221
this.widgets = data.dashboard.structure
211222
? data.dashboard.structure
212223
: [];
213224
this.buttonActions = this.dashboard.buttons || [];
214-
this.loading = loading;
215225
this.showFilter = this.dashboard.showFilter;
216226
} else {
217227
this.snackBar.openSnackBar(

0 commit comments

Comments
 (0)