Scenes: Fix issue with discarding unsaved changes modal in new dashboards (#84369)
This commit is contained in:
@@ -108,8 +108,8 @@ describe('DashboardScenePageStateManager', () => {
|
||||
const dashboard = loader.state.dashboard!;
|
||||
|
||||
expect(dashboard.state.meta.isNew).toBe(true);
|
||||
expect(dashboard.state.isEditing).toBe(true);
|
||||
expect(dashboard.state.isDirty).toBe(true);
|
||||
expect(dashboard.state.isEditing).toBe(undefined);
|
||||
expect(dashboard.state.isDirty).toBe(false);
|
||||
|
||||
dashboard.setState({ title: 'Changed' });
|
||||
|
||||
|
||||
@@ -165,9 +165,15 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
|
||||
window.__grafanaSceneContext = this;
|
||||
|
||||
if (this.state.isEditing) {
|
||||
this._initialUrlState = locationService.getLocation();
|
||||
this._changeTracker.startTrackingChanges();
|
||||
}
|
||||
|
||||
if (this.state.meta.isNew) {
|
||||
this.onEnterEditMode();
|
||||
this.setState({ isDirty: true });
|
||||
}
|
||||
|
||||
if (!this.state.meta.isEmbedded && this.state.uid) {
|
||||
dashboardWatcher.watch(this.state.uid);
|
||||
}
|
||||
@@ -246,14 +252,14 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
|
||||
}
|
||||
}
|
||||
|
||||
public exitEditMode({ skipConfirm, restoreIntialState }: { skipConfirm: boolean; restoreIntialState?: boolean }) {
|
||||
public exitEditMode({ skipConfirm, restoreInitialState }: { skipConfirm: boolean; restoreInitialState?: boolean }) {
|
||||
if (!this.canDiscard()) {
|
||||
console.error('Trying to discard back to a state that does not exist, initialState undefined');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.state.isDirty || skipConfirm) {
|
||||
this.exitEditModeConfirmed(restoreIntialState || this.state.isDirty);
|
||||
this.exitEditModeConfirmed(restoreInitialState || this.state.isDirty);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -268,7 +274,7 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
|
||||
);
|
||||
}
|
||||
|
||||
private exitEditModeConfirmed(restoreIntialState = true) {
|
||||
private exitEditModeConfirmed(restoreInitialState = true) {
|
||||
// No need to listen to changes anymore
|
||||
this._changeTracker.stopTrackingChanges();
|
||||
// Stop url sync before updating url
|
||||
@@ -287,7 +293,7 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
|
||||
})
|
||||
);
|
||||
|
||||
if (restoreIntialState) {
|
||||
if (restoreInitialState) {
|
||||
// Restore initial state and disable editing
|
||||
this.setState({ ...this._initialState, isEditing: false });
|
||||
} else {
|
||||
@@ -328,7 +334,7 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
|
||||
newState.version = versionRsp.version;
|
||||
|
||||
this.setState(newState);
|
||||
this.exitEditMode({ skipConfirm: true, restoreIntialState: false });
|
||||
this.exitEditMode({ skipConfirm: true, restoreInitialState: false });
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -173,9 +173,8 @@ describe('transformSaveModelToScene', () => {
|
||||
it('should initialize the DashboardScene in edit mode and dirty', () => {
|
||||
const rsp = buildNewDashboardSaveModel();
|
||||
const scene = transformSaveModelToScene(rsp);
|
||||
|
||||
expect(scene.state.isEditing).toBe(true);
|
||||
expect(scene.state.isDirty).toBe(true);
|
||||
expect(scene.state.isEditing).toBe(undefined);
|
||||
expect(scene.state.isDirty).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -274,16 +274,15 @@ export function createDashboardSceneFromDashboardModel(oldModel: DashboardModel)
|
||||
}
|
||||
|
||||
const dashboardScene = new DashboardScene({
|
||||
title: oldModel.title,
|
||||
tags: oldModel.tags || [],
|
||||
links: oldModel.links || [],
|
||||
uid: oldModel.uid,
|
||||
id: oldModel.id,
|
||||
description: oldModel.description,
|
||||
editable: oldModel.editable,
|
||||
isDirty: oldModel.meta.isNew,
|
||||
isEditing: oldModel.meta.isNew,
|
||||
id: oldModel.id,
|
||||
isDirty: false,
|
||||
links: oldModel.links || [],
|
||||
meta: oldModel.meta,
|
||||
tags: oldModel.tags || [],
|
||||
title: oldModel.title,
|
||||
uid: oldModel.uid,
|
||||
version: oldModel.version,
|
||||
body: new SceneGridLayout({
|
||||
isLazy: true,
|
||||
|
||||
Reference in New Issue
Block a user