grafana/public/app/features/library-panels/utils/usePanelSave.ts
Leo 27b6b3b3bd
Folder: Replace folderId with folderUid (#58393)
* support folderuid in FolderPicker

* support folderuid in unified alerting

* support folderuid when returning to view mode after editing a panel

* support folderuid when preselecting the folderpicker in dashboard general settings

* support folderuid when saving dashboard

* support folderuid when pre-selecting folderpicker in dashboard form

* support folderuid in routes when loading a dashboard

* support folderuid when saving dashboard json

* support folderuid when validating new dashboard name

* support folderuid when moving dashboard to another folder

* support folderuid on dashboard action buttons

* support folderuid when creating a new dashboard on an empty folder

* support folderuid when showing library panel modal

* support folderuid when saving library panel

* support folderuid when importing dashboard

* fixed broken tests

* use folderuid when importing dashboards

* remove commented line

* fix typo when comparing uid values
2022-11-17 09:22:57 +01:00

51 lines
1.4 KiB
TypeScript

import { useEffect } from 'react';
import useAsyncFn from 'react-use/lib/useAsyncFn';
import { isFetchError } from '@grafana/runtime';
import { notifyApp } from 'app/core/actions';
import { t } from 'app/core/internationalization';
import { PanelModel } from 'app/features/dashboard/state';
import { useDispatch } from 'app/types';
import {
createPanelLibraryErrorNotification,
createPanelLibrarySuccessNotification,
saveAndRefreshLibraryPanel,
} from '../utils';
export const usePanelSave = () => {
const dispatch = useDispatch();
const [state, saveLibraryPanel] = useAsyncFn(async (panel: PanelModel, folderUid: string) => {
try {
return await saveAndRefreshLibraryPanel(panel, folderUid);
} catch (err) {
if (isFetchError(err)) {
err.isHandled = true;
throw new Error(err.data.message);
}
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 };
};