Dashboards: Fix frontend exception when backend fails to save dashboard (#71144)

This commit is contained in:
Josh Hunt
2023-07-11 14:33:09 +01:00
committed by GitHub
parent 535b5659e4
commit 2e5f733d2d

View File

@@ -14,7 +14,27 @@ import { DashboardSavedEvent } from 'app/types/events';
import { SaveDashboardOptions } from './types'; import { SaveDashboardOptions } from './types';
const saveDashboard = async (saveModel: any, options: SaveDashboardOptions, dashboard: DashboardModel) => { const saveDashboard = async (
saveModel: any,
options: SaveDashboardOptions,
dashboard: DashboardModel,
saveDashboardRtkQuery: ReturnType<typeof useSaveDashboardMutation>[0]
) => {
if (config.featureToggles.nestedFolders) {
const query = await saveDashboardRtkQuery({
dashboard: saveModel,
folderUid: options.folderUid ?? dashboard.meta.folderUid ?? saveModel.meta.folderUid,
message: options.message,
overwrite: options.overwrite,
});
if ('error' in query) {
throw query.error;
}
return query.data;
}
let folderUid = options.folderUid; let folderUid = options.folderUid;
if (folderUid === undefined) { if (folderUid === undefined) {
folderUid = dashboard.meta.folderUid ?? saveModel.folderUid; folderUid = dashboard.meta.folderUid ?? saveModel.folderUid;
@@ -33,15 +53,7 @@ export const useDashboardSave = (dashboard: DashboardModel, isCopy = false) => {
const [state, onDashboardSave] = useAsyncFn( const [state, onDashboardSave] = useAsyncFn(
async (clone: DashboardModel, options: SaveDashboardOptions, dashboard: DashboardModel) => { async (clone: DashboardModel, options: SaveDashboardOptions, dashboard: DashboardModel) => {
try { try {
const queryResult = config.featureToggles.nestedFolders const result = await saveDashboard(clone, options, dashboard, saveDashboardRtkQuery);
? await saveDashboardRtkQuery({
dashboard: clone,
folderUid: options.folderUid ?? dashboard.meta.folderUid ?? clone.meta.folderUid,
message: options.message,
overwrite: options.overwrite,
})
: await saveDashboard(clone, options, dashboard);
const result = config.featureToggles.nestedFolders ? queryResult.data : queryResult;
dashboard.version = result.version; dashboard.version = result.version;
dashboard.clearUnsavedChanges(); dashboard.clearUnsavedChanges();