mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 18:30:41 -06:00
Dashboard: rewrite useDashboardSave
to not use useEffect
(#65602)
rewrite useDashboardSave to not use useEffect
This commit is contained in:
parent
1d0e74f998
commit
2e2c989530
@ -1,5 +1,4 @@
|
||||
import { useEffect } from 'react';
|
||||
import useAsyncFn from 'react-use/lib/useAsyncFn';
|
||||
import { useAsyncFn } from 'react-use';
|
||||
|
||||
import { locationUtil } from '@grafana/data';
|
||||
import { locationService, reportInteraction } from '@grafana/runtime';
|
||||
@ -27,20 +26,13 @@ const saveDashboard = async (saveModel: any, options: SaveDashboardOptions, dash
|
||||
};
|
||||
|
||||
export const useDashboardSave = (dashboard: DashboardModel, isCopy = false) => {
|
||||
const [state, onDashboardSave] = useAsyncFn(
|
||||
async (clone: any, options: SaveDashboardOptions, dashboard: DashboardModel) =>
|
||||
await saveDashboard(clone, options, dashboard),
|
||||
[]
|
||||
);
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const notifyApp = useAppNotification();
|
||||
useEffect(() => {
|
||||
if (state.error && !state.loading) {
|
||||
notifyApp.error(state.error.message ?? 'Error saving dashboard');
|
||||
}
|
||||
if (state.value) {
|
||||
dashboard.version = state.value.version;
|
||||
const [state, onDashboardSave] = useAsyncFn(
|
||||
async (clone: any, options: SaveDashboardOptions, dashboard: DashboardModel) => {
|
||||
try {
|
||||
const result = await saveDashboard(clone, options, dashboard);
|
||||
dashboard.version = result.version;
|
||||
dashboard.clearUnsavedChanges();
|
||||
|
||||
// important that these happen before location redirect below
|
||||
@ -49,17 +41,17 @@ export const useDashboardSave = (dashboard: DashboardModel, isCopy = false) => {
|
||||
if (isCopy) {
|
||||
reportInteraction('grafana_dashboard_copied', {
|
||||
name: dashboard.title,
|
||||
url: state.value.url,
|
||||
url: result.url,
|
||||
});
|
||||
} else {
|
||||
reportInteraction(`grafana_dashboard_${dashboard.id ? 'saved' : 'created'}`, {
|
||||
name: dashboard.title,
|
||||
url: state.value.url,
|
||||
url: result.url,
|
||||
});
|
||||
}
|
||||
|
||||
const currentPath = locationService.getLocation().pathname;
|
||||
const newUrl = locationUtil.stripBaseFromUrl(state.value.url);
|
||||
const newUrl = locationUtil.stripBaseFromUrl(result.url);
|
||||
|
||||
if (newUrl !== currentPath) {
|
||||
setTimeout(() => locationService.replace(newUrl));
|
||||
@ -73,8 +65,16 @@ export const useDashboardSave = (dashboard: DashboardModel, isCopy = false) => {
|
||||
})
|
||||
);
|
||||
}
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
notifyApp.error(error.message ?? 'Error saving dashboard');
|
||||
}
|
||||
}, [dashboard, isCopy, state, notifyApp, dispatch]);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[dispatch, notifyApp]
|
||||
);
|
||||
|
||||
return { state, onDashboardSave };
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user