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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,7 +14,27 @@ import { DashboardSavedEvent } from 'app/types/events';
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;
if (folderUid === undefined) {
folderUid = dashboard.meta.folderUid ?? saveModel.folderUid;
@ -33,15 +53,7 @@ export const useDashboardSave = (dashboard: DashboardModel, isCopy = false) => {
const [state, onDashboardSave] = useAsyncFn(
async (clone: DashboardModel, options: SaveDashboardOptions, dashboard: DashboardModel) => {
try {
const queryResult = config.featureToggles.nestedFolders
? 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;
const result = await saveDashboard(clone, options, dashboard, saveDashboardRtkQuery);
dashboard.version = result.version;
dashboard.clearUnsavedChanges();