DashboardScene: Fix dashboard being restored to initial state after successful save (#84183)

* Failing test

* Do not restore initial state after edit mode exit when dashboard is not dirty
This commit is contained in:
Dominik Prokop 2024-03-12 13:54:47 +01:00 committed by GitHub
parent 2a7785c262
commit 39b682e333
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 7 deletions

View File

@ -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();
});

View File

@ -235,6 +235,7 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
folderUid: folderUid,
},
});
this._changeTracker.startTrackingChanges();
}
@ -245,14 +246,14 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
}
}
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<DashboardSceneState> {
);
}
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<DashboardSceneState> {
})
);
// 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<DashboardSceneState> {
newState.version = versionRsp.version;
this._initialState = newState;
this.exitEditMode({ skipConfirm: false });
this.exitEditMode({ skipConfirm: false, restoreIntialState: true });
return true;
};