From a9b8a6459e449beb995f0d16d15f31145331988b Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Thu, 13 Jul 2023 20:04:25 +0300 Subject: [PATCH] UI: Load home dashboards from asset resources --- .../home-links/home-links-routing.module.ts | 43 ++++++++++--------- .../dashboard/customer_user_home_page.json} | 0 .../dashboard/sys_admin_home_page.json} | 0 .../dashboard/tenant_admin_home_page.json} | 0 4 files changed, 22 insertions(+), 21 deletions(-) rename ui-ngx/src/{app/modules/home/pages/home-links/customer_user_home_page.raw => assets/dashboard/customer_user_home_page.json} (100%) rename ui-ngx/src/{app/modules/home/pages/home-links/sys_admin_home_page.raw => assets/dashboard/sys_admin_home_page.json} (100%) rename ui-ngx/src/{app/modules/home/pages/home-links/tenant_admin_home_page.raw => assets/dashboard/tenant_admin_home_page.json} (100%) diff --git a/ui-ngx/src/app/modules/home/pages/home-links/home-links-routing.module.ts b/ui-ngx/src/app/modules/home/pages/home-links/home-links-routing.module.ts index 5edc9cb7fc..c1c1c9bc9e 100644 --- a/ui-ngx/src/app/modules/home/pages/home-links/home-links-routing.module.ts +++ b/ui-ngx/src/app/modules/home/pages/home-links/home-links-routing.module.ts @@ -25,20 +25,19 @@ import { DashboardService } from '@core/http/dashboard.service'; import { select, Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; import { map } from 'rxjs/operators'; -import { - getCurrentAuthUser, - selectHasRepository, - selectPersistDeviceStateToTelemetry -} from '@core/auth/auth.selectors'; -import sysAdminHomePageDashboardJson from '!raw-loader!./sys_admin_home_page.raw'; -import tenantAdminHomePageDashboardJson from '!raw-loader!./tenant_admin_home_page.raw'; -import customerUserHomePageDashboardJson from '!raw-loader!./customer_user_home_page.raw'; +import { getCurrentAuthUser, selectPersistDeviceStateToTelemetry } from '@core/auth/auth.selectors'; import { EntityKeyType } from '@shared/models/query/query.models'; +import { ResourcesService } from '@core/services/resources.service'; + +const sysAdminHomePageJson = '/assets/dashboard/sys_admin_home_page.json'; +const tenantAdminHomePageJson = '/assets/dashboard/tenant_admin_home_page.json'; +const customerUserHomePageJson = '/assets/dashboard/customer_user_home_page.json'; @Injectable() export class HomeDashboardResolver implements Resolve { constructor(private dashboardService: DashboardService, + private resourcesService: ResourcesService, private store: Store) { } @@ -50,13 +49,13 @@ export class HomeDashboardResolver implements Resolve { const authority = getCurrentAuthUser(this.store).authority; switch (authority) { case Authority.SYS_ADMIN: - dashboard$ = of(JSON.parse(sysAdminHomePageDashboardJson)); + dashboard$ = this.resourcesService.loadJsonResource(sysAdminHomePageJson); break; case Authority.TENANT_ADMIN: - dashboard$ = this.updateDeviceActivityKeyFilterIfNeeded(JSON.parse(tenantAdminHomePageDashboardJson)); + dashboard$ = this.updateDeviceActivityKeyFilterIfNeeded(this.resourcesService.loadJsonResource(tenantAdminHomePageJson)); break; case Authority.CUSTOMER_USER: - dashboard$ = this.updateDeviceActivityKeyFilterIfNeeded(JSON.parse(customerUserHomePageDashboardJson)); + dashboard$ = this.updateDeviceActivityKeyFilterIfNeeded(this.resourcesService.loadJsonResource(customerUserHomePageJson)); break; } if (dashboard$) { @@ -73,18 +72,20 @@ export class HomeDashboardResolver implements Resolve { ); } - private updateDeviceActivityKeyFilterIfNeeded(dashboard: HomeDashboard): Observable { + private updateDeviceActivityKeyFilterIfNeeded(dashboard$: Observable): Observable { return this.store.pipe(select(selectPersistDeviceStateToTelemetry)).pipe( - map((persistToTelemetry) => { - if (persistToTelemetry) { - for (const filterId of Object.keys(dashboard.configuration.filters)) { - if (['Active Devices', 'Inactive Devices'].includes(dashboard.configuration.filters[filterId].filter)) { - dashboard.configuration.filters[filterId].keyFilters[0].key.type = EntityKeyType.TIME_SERIES; + mergeMap((persistToTelemetry) => dashboard$.pipe( + map((dashboard) => { + if (persistToTelemetry) { + for (const filterId of Object.keys(dashboard.configuration.filters)) { + if (['Active Devices', 'Inactive Devices'].includes(dashboard.configuration.filters[filterId].filter)) { + dashboard.configuration.filters[filterId].keyFilters[0].key.type = EntityKeyType.TIME_SERIES; + } + } } - } - } - return dashboard; - }) + return dashboard; + }) + )) ); } } diff --git a/ui-ngx/src/app/modules/home/pages/home-links/customer_user_home_page.raw b/ui-ngx/src/assets/dashboard/customer_user_home_page.json similarity index 100% rename from ui-ngx/src/app/modules/home/pages/home-links/customer_user_home_page.raw rename to ui-ngx/src/assets/dashboard/customer_user_home_page.json diff --git a/ui-ngx/src/app/modules/home/pages/home-links/sys_admin_home_page.raw b/ui-ngx/src/assets/dashboard/sys_admin_home_page.json similarity index 100% rename from ui-ngx/src/app/modules/home/pages/home-links/sys_admin_home_page.raw rename to ui-ngx/src/assets/dashboard/sys_admin_home_page.json diff --git a/ui-ngx/src/app/modules/home/pages/home-links/tenant_admin_home_page.raw b/ui-ngx/src/assets/dashboard/tenant_admin_home_page.json similarity index 100% rename from ui-ngx/src/app/modules/home/pages/home-links/tenant_admin_home_page.raw rename to ui-ngx/src/assets/dashboard/tenant_admin_home_page.json