diff --git a/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx b/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx index 4ffd7472048..094b94573e0 100644 --- a/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx +++ b/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx @@ -113,6 +113,24 @@ describe('DashboardScene', () => { expect(scene.state.isEditing).toBe(true); }); + it('Exiting already saved dashboard should not restore initial state', () => { + scene.setState({ title: 'Updated title' }); + expect(scene.state.isDirty).toBe(true); + + scene.saveCompleted({} as Dashboard, { + id: 1, + slug: 'slug', + uid: 'dash-1', + url: 'sss', + version: 2, + status: 'aaa', + }); + + expect(scene.state.isDirty).toBe(false); + scene.exitEditMode({ skipConfirm: true }); + expect(scene.state.title).toEqual('Updated title'); + }); + it('Should start the detect changes worker', () => { expect(worker.onmessage).toBeDefined(); }); diff --git a/public/app/features/dashboard-scene/scene/DashboardScene.tsx b/public/app/features/dashboard-scene/scene/DashboardScene.tsx index 095115da308..ce824877e79 100644 --- a/public/app/features/dashboard-scene/scene/DashboardScene.tsx +++ b/public/app/features/dashboard-scene/scene/DashboardScene.tsx @@ -235,6 +235,7 @@ export class DashboardScene extends SceneObjectBase { folderUid: folderUid, }, }); + this._changeTracker.startTrackingChanges(); } @@ -245,14 +246,14 @@ export class DashboardScene extends SceneObjectBase { } } - public exitEditMode({ skipConfirm }: { skipConfirm: boolean }) { + public exitEditMode({ skipConfirm, restoreIntialState }: { skipConfirm: boolean; restoreIntialState?: 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(); + this.exitEditModeConfirmed(restoreIntialState || this.state.isDirty); return; } @@ -267,7 +268,7 @@ export class DashboardScene extends SceneObjectBase { ); } - private exitEditModeConfirmed() { + private exitEditModeConfirmed(restoreIntialState = true) { // No need to listen to changes anymore this._changeTracker.stopTrackingChanges(); // Stop url sync before updating url @@ -286,9 +287,13 @@ export class DashboardScene extends SceneObjectBase { }) ); - // locationService.replace({ pathname: this._initialUrlState?.pathname, search: this._initialUrlState?.search }); - // Update state and disable editing - this.setState({ ...this._initialState, isEditing: false }); + if (restoreIntialState) { + // Restore initial state and disable editing + this.setState({ ...this._initialState, isEditing: false }); + } else { + // Do not restore + this.setState({ isEditing: false }); + } // and start url sync again this.startUrlSync(); // Disable grid dragging @@ -315,7 +320,7 @@ export class DashboardScene extends SceneObjectBase { newState.version = versionRsp.version; this._initialState = newState; - this.exitEditMode({ skipConfirm: false }); + this.exitEditMode({ skipConfirm: false, restoreIntialState: true }); return true; };