[v10.1.x] usePanelSave: Fix success/error notifications (#74607)
usePanelSave: Fix success/error notifications (#74454)
(cherry picked from commit cca2905ddf)
Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>
This commit is contained in:
co-authored by
kay delaney
parent
48bcaf669e
commit
65e1e9d67f
@@ -1,18 +1,8 @@
|
|||||||
import { createErrorNotification, createSuccessNotification } from '../../core/copy/appNotification';
|
|
||||||
import { AppNotification } from '../../types';
|
|
||||||
import { PanelModel } from '../dashboard/state';
|
import { PanelModel } from '../dashboard/state';
|
||||||
|
|
||||||
import { addLibraryPanel, updateLibraryPanel } from './state/api';
|
import { addLibraryPanel, updateLibraryPanel } from './state/api';
|
||||||
import { LibraryElementDTO } from './types';
|
import { LibraryElementDTO } from './types';
|
||||||
|
|
||||||
export function createPanelLibraryErrorNotification(message: string): AppNotification {
|
|
||||||
return createErrorNotification(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createPanelLibrarySuccessNotification(message: string): AppNotification {
|
|
||||||
return createSuccessNotification(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function saveAndRefreshLibraryPanel(panel: PanelModel, folderUid: string): Promise<LibraryElementDTO> {
|
export async function saveAndRefreshLibraryPanel(panel: PanelModel, folderUid: string): Promise<LibraryElementDTO> {
|
||||||
const panelSaveModel = toPanelSaveModel(panel);
|
const panelSaveModel = toPanelSaveModel(panel);
|
||||||
const savedPanel = await saveOrUpdateLibraryPanel(panelSaveModel, folderUid);
|
const savedPanel = await saveOrUpdateLibraryPanel(panelSaveModel, folderUid);
|
||||||
|
|||||||
@@ -1,50 +1,31 @@
|
|||||||
import { useEffect } from 'react';
|
|
||||||
import useAsyncFn from 'react-use/lib/useAsyncFn';
|
import useAsyncFn from 'react-use/lib/useAsyncFn';
|
||||||
|
|
||||||
import { isFetchError } from '@grafana/runtime';
|
import { isFetchError } from '@grafana/runtime';
|
||||||
import { notifyApp } from 'app/core/actions';
|
import { useAppNotification } from 'app/core/copy/appNotification';
|
||||||
import { t } from 'app/core/internationalization';
|
import { t } from 'app/core/internationalization';
|
||||||
import { PanelModel } from 'app/features/dashboard/state';
|
import { PanelModel } from 'app/features/dashboard/state';
|
||||||
import { useDispatch } from 'app/types';
|
|
||||||
|
|
||||||
import {
|
import { saveAndRefreshLibraryPanel } from '../utils';
|
||||||
createPanelLibraryErrorNotification,
|
|
||||||
createPanelLibrarySuccessNotification,
|
|
||||||
saveAndRefreshLibraryPanel,
|
|
||||||
} from '../utils';
|
|
||||||
|
|
||||||
export const usePanelSave = () => {
|
export const usePanelSave = () => {
|
||||||
const dispatch = useDispatch();
|
const notifyApp = useAppNotification();
|
||||||
const [state, saveLibraryPanel] = useAsyncFn(async (panel: PanelModel, folderUid: string) => {
|
const [state, saveLibraryPanel] = useAsyncFn(async (panel: PanelModel, folderUid: string) => {
|
||||||
try {
|
try {
|
||||||
return await saveAndRefreshLibraryPanel(panel, folderUid);
|
const libEl = await saveAndRefreshLibraryPanel(panel, folderUid);
|
||||||
|
notifyApp.success(t('library-panels.save.success', 'Library panel saved'));
|
||||||
|
return libEl;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (isFetchError(err)) {
|
if (isFetchError(err)) {
|
||||||
err.isHandled = true;
|
err.isHandled = true;
|
||||||
throw new Error(err.data.message);
|
notifyApp.error(
|
||||||
|
t('library-panels.save.error', 'Error saving library panel: "{{errorMsg}}"', {
|
||||||
|
errorMsg: err.message ?? err.data.message,
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (state.error) {
|
|
||||||
const errorMsg = state.error.message;
|
|
||||||
|
|
||||||
dispatch(
|
|
||||||
notifyApp(
|
|
||||||
createPanelLibraryErrorNotification(
|
|
||||||
t('library-panels.save.error', 'Error saving library panel: "{{errorMsg}}"', { errorMsg })
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (state.value) {
|
|
||||||
dispatch(
|
|
||||||
notifyApp(createPanelLibrarySuccessNotification(t('library-panels.save.success', 'Library panel saved')))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}, [dispatch, state]);
|
|
||||||
|
|
||||||
return { state, saveLibraryPanel };
|
return { state, saveLibraryPanel };
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user