Files
grafana/public/app/features/dashboard/components/PanelEditor/state/actions.ts
Torkel Ödegaard e6f2b10a36 ChangeTracker: Unified unsaved changes handling with library panels (#34989)
* UnsavedChanges: Move Change tracker to use Prompt

* Fix a lot of race conditions and stacking of changes in onConfirm and onDismiss

* Listen to save event

* add missing delay argument

* migrated the change tracker unit tests

* Updated snapshot

* Removed unessary action

* removed updateSourcePanel

* Fix hiding save library panel modal prompt when clicking discard

* change saved libray panel title and buttons so they are a bit different as Prompt and when used from save button

* Fixed issue with saving new dashboard

* Now all scenarios work

* increase wait time

* Fixed one more race condition
2021-06-02 12:24:19 +02:00

131 lines
4.0 KiB
TypeScript

import { DashboardModel, PanelModel } from '../../../state';
import { ThunkResult } from 'app/types';
import {
closeCompleted,
PANEL_EDITOR_UI_STATE_STORAGE_KEY,
PanelEditorUIState,
setPanelEditorUIState,
updateEditorInitState,
setDiscardChanges,
} from './reducers';
import { cleanUpEditPanel, panelModelAndPluginReady } from '../../../state/reducers';
import store from 'app/core/store';
import { pick } from 'lodash';
import { locationService } from '@grafana/runtime';
export function initPanelEditor(sourcePanel: PanelModel, dashboard: DashboardModel): ThunkResult<void> {
return (dispatch) => {
const panel = dashboard.initEditPanel(sourcePanel);
dispatch(
updateEditorInitState({
panel,
sourcePanel,
})
);
};
}
export function discardPanelChanges(): ThunkResult<void> {
return async (dispatch, getStore) => {
const { getPanel } = getStore().panelEditor;
getPanel().configRev = 0;
dispatch(setDiscardChanges(true));
};
}
export function exitPanelEditor(): ThunkResult<void> {
return async (dispatch, getStore) => {
locationService.partial({ editPanel: null, tab: null });
};
}
function updateDuplicateLibraryPanels(modifiedPanel: PanelModel, dashboard: DashboardModel | null, dispatch: any) {
if (modifiedPanel.libraryPanel?.uid === undefined || !dashboard) {
return;
}
const modifiedSaveModel = modifiedPanel.getSaveModel();
for (const panel of dashboard.panels) {
if (panel.libraryPanel?.uid !== modifiedPanel.libraryPanel!.uid) {
continue;
}
panel.restoreModel({
...modifiedSaveModel,
...pick(panel, 'gridPos', 'id'),
});
// Loaded plugin is not included in the persisted properties
// So is not handled by restoreModel
const pluginChanged = panel.plugin?.meta.id !== modifiedPanel.plugin?.meta.id;
panel.plugin = modifiedPanel.plugin;
panel.configRev++;
if (pluginChanged) {
dispatch(panelModelAndPluginReady({ panelId: panel.id, plugin: panel.plugin! }));
}
// Resend last query result on source panel query runner
// But do this after the panel edit editor exit process has completed
setTimeout(() => {
panel.getQueryRunner().useLastResultFrom(modifiedPanel.getQueryRunner());
}, 20);
}
}
export function panelEditorCleanUp(): ThunkResult<void> {
return (dispatch, getStore) => {
const dashboard = getStore().dashboard.getModel();
const { getPanel, getSourcePanel, shouldDiscardChanges } = getStore().panelEditor;
if (!shouldDiscardChanges) {
const panel = getPanel();
const modifiedSaveModel = panel.getSaveModel();
const sourcePanel = getSourcePanel();
const panelTypeChanged = sourcePanel.type !== panel.type;
updateDuplicateLibraryPanels(panel, dashboard, dispatch);
// restore the source panel ID before we update source panel
modifiedSaveModel.id = sourcePanel.id;
sourcePanel.restoreModel(modifiedSaveModel);
sourcePanel.configRev++; // force check the configs
// Loaded plugin is not included in the persisted properties
// So is not handled by restoreModel
sourcePanel.plugin = panel.plugin;
if (panelTypeChanged) {
dispatch(panelModelAndPluginReady({ panelId: sourcePanel.id, plugin: panel.plugin! }));
}
// Resend last query result on source panel query runner
// But do this after the panel edit editor exit process has completed
setTimeout(() => {
sourcePanel.getQueryRunner().useLastResultFrom(panel.getQueryRunner());
}, 20);
}
if (dashboard) {
dashboard.exitPanelEditor();
}
dispatch(cleanUpEditPanel());
dispatch(closeCompleted());
};
}
export function updatePanelEditorUIState(uiState: Partial<PanelEditorUIState>): ThunkResult<void> {
return (dispatch, getStore) => {
const nextState = { ...getStore().panelEditor.ui, ...uiState };
dispatch(setPanelEditorUIState(nextState));
try {
store.setObject(PANEL_EDITOR_UI_STATE_STORAGE_KEY, nextState);
} catch (error) {
console.error(error);
}
};
}