Browse Source

Fixed dashboard keeping a deleted or renamed state as current after editing states in "Manage dashboard states" (#15912)

* Fixed stale dashboard state after state deletion/rename

* Resolved PR comments

* Moved removed-state handling from dashboard page to state controllers
pull/16372/head
Maksym Tsymbarov 2 months ago
committed by GitHub
parent
commit
0325f4e519
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 8
      ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts
  2. 41
      ui-ngx/src/app/modules/home/components/dashboard-page/states/default-state-controller.component.ts
  3. 44
      ui-ngx/src/app/modules/home/components/dashboard-page/states/entity-state-controller.component.ts
  4. 14
      ui-ngx/src/app/modules/home/components/dashboard-page/states/state-controller.component.ts

8
ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts

@ -104,6 +104,7 @@ import {
} from '@home/components/dashboard-page/states/manage-dashboard-states-dialog.component'; } from '@home/components/dashboard-page/states/manage-dashboard-states-dialog.component';
import { ImportExportService } from '@shared/import-export/import-export.service'; import { ImportExportService } from '@shared/import-export/import-export.service';
import { AuthState } from '@app/core/auth/auth.models'; import { AuthState } from '@app/core/auth/auth.models';
import { IStateControllerComponent } from '@home/components/dashboard-page/states/state-controller.models';
import { DashboardReportService } from '@core/http/dashboard-report.service'; import { DashboardReportService } from '@core/http/dashboard-report.service';
import { EntityGroupInfo, resolveGroupParams } from '@shared/models/entity-group.models'; import { EntityGroupInfo, resolveGroupParams } from '@shared/models/entity-group.models';
import { UserPermissionsService } from '@core/http/user-permissions.service'; import { UserPermissionsService } from '@core/http/user-permissions.service';
@ -1198,11 +1199,10 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC
private updateStates(states: {[id: string]: DashboardState }) { private updateStates(states: {[id: string]: DashboardState }) {
this.dashboard.configuration.states = states; this.dashboard.configuration.states = states;
this.dashboardUtils.removeUnusedWidgets(this.dashboard); this.dashboardUtils.removeUnusedWidgets(this.dashboard);
let targetState = this.dashboardCtx.state; if (states[this.dashboardCtx.state]) {
if (!this.dashboard.configuration.states[targetState]) { this.openDashboardState(this.dashboardCtx.state);
targetState = this.dashboardUtils.getRootStateId(this.dashboardConfiguration.states);
} }
this.openDashboardState(targetState); (this.dashboardCtx.stateController as IStateControllerComponent).states = states;
} }
public importWidget($event: Event) { public importWidget($event: Event) {

41
ui-ngx/src/app/modules/home/components/dashboard-page/states/default-state-controller.component.ts

@ -36,9 +36,9 @@ export class DefaultStateControllerComponent extends StateControllerComponent im
protected utils: UtilsService, protected utils: UtilsService,
private entityService: EntityService, private entityService: EntityService,
private mobileService: MobileService, private mobileService: MobileService,
private dashboardUtils: DashboardUtilsService, dashboardUtils: DashboardUtilsService,
private breadcrumbService: BreadcrumbService) { private breadcrumbService: BreadcrumbService) {
super(router, route, utils, window, ngZone, statesControllerService); super(router, route, utils, window, ngZone, statesControllerService, dashboardUtils);
} }
ngOnInit(): void { ngOnInit(): void {
@ -51,7 +51,7 @@ export class DefaultStateControllerComponent extends StateControllerComponent im
public init() { public init() {
if (this.preservedState) { if (this.preservedState) {
this.stateObject = this.preservedState; this.stateObject = this.normalizeStateObject(this.preservedState);
setTimeout(() => { setTimeout(() => {
this.gotoState(this.stateObject[0].id, true); this.gotoState(this.stateObject[0].id, true);
}, 1); }, 1);
@ -71,6 +71,13 @@ export class DefaultStateControllerComponent extends StateControllerComponent im
} }
protected onStatesChanged() { protected onStatesChanged() {
const prevStateId = this.getStateId();
this.stateObject = this.normalizeStateObject(this.stateObject);
const newStateId = this.getStateId();
if (newStateId !== prevStateId) {
this.gotoState(newStateId, false);
this.updateStateParam(objToBase64(this.stateObject), true);
}
} }
protected onStateChanged() { protected onStateChanged() {
@ -204,33 +211,13 @@ export class DefaultStateControllerComponent extends StateControllerComponent im
try { try {
result = base64toObj(stateBase64); result = base64toObj(stateBase64);
} catch (e) { } catch (e) {
result = [ { id: null, params: {} } ]; result = [];
} }
} }
if (!result) { if (result && result.length > 1) {
result = []; result = [ result[result.length - 1] ];
}
if (!result.length) {
result[0] = { id: null, params: {} };
} else if (result.length > 1) {
const newResult = [];
newResult.push(result[result.length - 1]);
result = newResult;
}
const rootStateId = this.dashboardUtils.getRootStateId(this.states);
if (!result[0].id) {
result[0].id = rootStateId;
}
if (!this.states[result[0].id]) {
result[0].id = rootStateId;
}
let i = result.length;
while (i--) {
if (!result[i].id || !this.states[result[i].id]) {
result.splice(i, 1);
}
} }
return result; return this.normalizeStateObject(result);
} }
private gotoState(stateId: string, update: boolean, openRightLayout?: boolean) { private gotoState(stateId: string, update: boolean, openRightLayout?: boolean) {

44
ui-ngx/src/app/modules/home/components/dashboard-page/states/entity-state-controller.component.ts

@ -41,9 +41,9 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
protected utils: UtilsService, protected utils: UtilsService,
private entityService: EntityService, private entityService: EntityService,
private mobileService: MobileService, private mobileService: MobileService,
private dashboardUtils: DashboardUtilsService, dashboardUtils: DashboardUtilsService,
private breadcrumbService: BreadcrumbService) { private breadcrumbService: BreadcrumbService) {
super(router, route, utils, window, ngZone, statesControllerService); super(router, route, utils, window, ngZone, statesControllerService, dashboardUtils);
} }
ngOnInit(): void { ngOnInit(): void {
@ -56,7 +56,7 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
public init() { public init() {
if (this.preservedState) { if (this.preservedState) {
this.stateObject = this.preservedState; this.stateObject = this.normalizeStateObject(this.preservedState);
this.selectedStateIndex = this.stateObject.length - 1; this.selectedStateIndex = this.stateObject.length - 1;
setTimeout(() => { setTimeout(() => {
this.gotoState(this.stateObject[this.stateObject.length - 1].id, true); this.gotoState(this.stateObject[this.stateObject.length - 1].id, true);
@ -78,6 +78,13 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
} }
protected onStatesChanged() { protected onStatesChanged() {
const prevStateId = this.getStateId();
this.stateObject = this.normalizeStateObject(this.stateObject);
this.selectedStateIndex = this.stateObject.length - 1;
const newStateId = this.getStateId();
if (newStateId !== prevStateId) {
this.gotoState(newStateId, true, undefined, true);
}
} }
protected onStateChanged() { protected onStateChanged() {
@ -248,32 +255,13 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
try { try {
result = base64toObj(stateBase64); result = base64toObj(stateBase64);
} catch (e) { } catch (e) {
result = [ { id: null, params: {} } ]; result = [];
}
}
if (!result) {
result = [];
}
if (!result.length) {
result[0] = { id: null, params: {} };
}
const rootStateId = this.dashboardUtils.getRootStateId(this.states);
if (!result[0].id) {
result[0].id = rootStateId;
}
if (!this.states[result[0].id]) {
result[0].id = rootStateId;
}
let i = result.length;
while (i--) {
if (!result[i].id || !this.states[result[i].id]) {
result.splice(i, 1);
} }
} }
return result; return this.normalizeStateObject(result);
} }
private gotoState(stateId: string, update: boolean, openRightLayout?: boolean) { private gotoState(stateId: string, update: boolean, openRightLayout?: boolean, replaceCurrentHistoryUrl?: boolean) {
const isStateIdChanged = this.dashboardCtrl.dashboardCtx.state !== stateId; const isStateIdChanged = this.dashboardCtrl.dashboardCtx.state !== stateId;
this.dashboardCtrl.openDashboardState(stateId, openRightLayout); this.dashboardCtrl.openDashboardState(stateId, openRightLayout);
if (isStateIdChanged) { if (isStateIdChanged) {
@ -283,11 +271,11 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
this.mobileService.handleDashboardStateName(this.getStateName(this.stateObject.length - 1)); this.mobileService.handleDashboardStateName(this.getStateName(this.stateObject.length - 1));
} }
if (update) { if (update) {
this.updateLocation(isStateIdChanged); this.updateLocation(replaceCurrentHistoryUrl ?? !isStateIdChanged);
} }
} }
private updateLocation(isStateIdChanged: boolean) { private updateLocation(replaceCurrentHistoryUrl: boolean) {
if (this.stateObject[this.stateObject.length - 1].id) { if (this.stateObject[this.stateObject.length - 1].id) {
let newState; let newState;
if (this.isDefaultState()) { if (this.isDefaultState()) {
@ -295,7 +283,7 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
} else { } else {
newState = objToBase64(this.stateObject); newState = objToBase64(this.stateObject);
} }
this.updateStateParam(newState, !isStateIdChanged); this.updateStateParam(newState, replaceCurrentHistoryUrl);
} }
} }

14
ui-ngx/src/app/modules/home/components/dashboard-page/states/state-controller.component.ts

@ -4,12 +4,13 @@
import { IStateControllerComponent, StateControllerState } from '@home/components/dashboard-page/states/state-controller.models'; import { IStateControllerComponent, StateControllerState } from '@home/components/dashboard-page/states/state-controller.models';
import { IDashboardController } from '../dashboard-page.models'; import { IDashboardController } from '../dashboard-page.models';
import { DashboardState } from '@app/shared/models/dashboard.models'; import { DashboardState } from '@app/shared/models/dashboard.models';
import { BehaviorSubject, Observable, Subject, Subscription } from 'rxjs'; import { Observable, Subject, Subscription } from 'rxjs';
import { NgZone, OnDestroy, OnInit, Directive } from '@angular/core'; import { NgZone, OnDestroy, OnInit, Directive } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router'; import { ActivatedRoute, Params, Router } from '@angular/router';
import { StatesControllerService } from '@home/components/dashboard-page/states/states-controller.service'; import { StatesControllerService } from '@home/components/dashboard-page/states/states-controller.service';
import { EntityId } from '@app/shared/models/id/entity-id'; import { EntityId } from '@app/shared/models/id/entity-id';
import { StateObject, StateParams } from '@app/core/api/widget-api.models'; import { StateObject, StateParams } from '@app/core/api/widget-api.models';
import { DashboardUtilsService } from '@core/services/dashboard-utils.service';
import { WindowMessage } from '@shared/models/window-message.model'; import { WindowMessage } from '@shared/models/window-message.model';
import { UtilsService } from '@core/services/utils.service'; import { UtilsService } from '@core/services/utils.service';
@ -91,7 +92,8 @@ export abstract class StateControllerComponent implements IStateControllerCompon
protected utils: UtilsService, protected utils: UtilsService,
protected window: Window, protected window: Window,
protected ngZone: NgZone, protected ngZone: NgZone,
protected statesControllerService: StatesControllerService) { protected statesControllerService: StatesControllerService,
protected dashboardUtils: DashboardUtilsService) {
} }
ngOnInit(): void { ngOnInit(): void {
@ -123,6 +125,14 @@ export abstract class StateControllerComponent implements IStateControllerCompon
this.stateChangedSubject.complete(); this.stateChangedSubject.complete();
} }
protected normalizeStateObject(stateObject: StateControllerState): StateControllerState {
const result = (stateObject || []).filter((stateObj) => stateObj.id && this.states[stateObj.id]);
if (!result.length) {
result.push({ id: this.dashboardUtils.getRootStateId(this.states), params: {} });
}
return result;
}
protected updateStateParam(newState: string, replaceCurrentHistoryUrl = false) { protected updateStateParam(newState: string, replaceCurrentHistoryUrl = false) {
this.currentState = newState; this.currentState = newState;
if (this.syncStateWithQueryParam) { if (this.syncStateWithQueryParam) {

Loading…
Cancel
Save