mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Dashboard: Fixes save changes diff after first save * Lots of type issues * better fix * Update some more places to use new function * Fix * Update * Update * remove console.log * Update
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import React, { useMemo, useState } from 'react';
|
|
|
|
import { Stack } from '@grafana/experimental';
|
|
import { Dashboard } from '@grafana/schema';
|
|
import { Button, Form } from '@grafana/ui';
|
|
import { useAppNotification } from 'app/core/copy/appNotification';
|
|
|
|
import { DashboardModel } from '../../state';
|
|
import { SaveDashboardData } from '../SaveDashboard/types';
|
|
|
|
interface SaveDashboardProps {
|
|
dashboard: DashboardModel;
|
|
onCancel: () => void;
|
|
onSubmit?: (clone: Dashboard) => Promise<unknown>;
|
|
onSuccess: () => void;
|
|
saveModel: SaveDashboardData;
|
|
}
|
|
export const SaveDashboardForm = ({ dashboard, onCancel, onSubmit, onSuccess, saveModel }: SaveDashboardProps) => {
|
|
const [saving, setSaving] = useState(false);
|
|
const notifyApp = useAppNotification();
|
|
const hasChanges = useMemo(() => dashboard.hasTimeChanged() || saveModel.hasChanges, [dashboard, saveModel]);
|
|
|
|
const onFormSubmit = async () => {
|
|
if (!onSubmit) {
|
|
return;
|
|
}
|
|
setSaving(true);
|
|
onSubmit(saveModel.clone)
|
|
.then(() => {
|
|
notifyApp.success('Dashboard saved locally');
|
|
onSuccess();
|
|
})
|
|
.catch((error) => {
|
|
notifyApp.error(error.message || 'Error saving dashboard');
|
|
})
|
|
.finally(() => setSaving(false));
|
|
};
|
|
|
|
return (
|
|
<Form onSubmit={onFormSubmit}>
|
|
{() => {
|
|
return (
|
|
<Stack gap={2}>
|
|
<Stack alignItems="center">
|
|
<Button variant="secondary" onClick={onCancel} fill="outline">
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" disabled={!hasChanges} icon={saving ? 'fa fa-spinner' : undefined}>
|
|
Save
|
|
</Button>
|
|
{!hasChanges && <div>No changes to save</div>}
|
|
</Stack>
|
|
</Stack>
|
|
);
|
|
}}
|
|
</Form>
|
|
);
|
|
};
|