diff --git a/.betterer.results b/.betterer.results index 7a748db1e03..b7e9a5b4b6f 100644 --- a/.betterer.results +++ b/.betterer.results @@ -2071,11 +2071,6 @@ exports[`better eslint`] = { [0, 0, 0, "No untranslated strings. Wrap text with ", "1"], [0, 0, 0, "No untranslated strings. Wrap text with ", "2"] ], - "public/app/features/dashboard-scene/saving/SaveDashboardDrawer.tsx:5381": [ - [0, 0, 0, "No untranslated strings. Wrap text with ", "0"], - [0, 0, 0, "No untranslated strings. Wrap text with ", "1"], - [0, 0, 0, "No untranslated strings. Wrap text with ", "2"] - ], "public/app/features/dashboard-scene/saving/SaveDashboardForm.tsx:5381": [ [0, 0, 0, "No untranslated strings. Wrap text with ", "0"], [0, 0, 0, "No untranslated strings. Wrap text with ", "1"], @@ -2084,6 +2079,11 @@ exports[`better eslint`] = { [0, 0, 0, "No untranslated strings. Wrap text with ", "4"], [0, 0, 0, "No untranslated strings. Wrap text with ", "5"] ], + "public/app/features/dashboard-scene/saving/SaveProvisionedDashboard.tsx:5381": [ + [0, 0, 0, "No untranslated strings. Wrap text with ", "0"], + [0, 0, 0, "No untranslated strings. Wrap text with ", "1"], + [0, 0, 0, "No untranslated strings. Wrap text with ", "2"] + ], "public/app/features/dashboard-scene/saving/SaveProvisionedDashboardForm.tsx:5381": [ [0, 0, 0, "No untranslated strings. Wrap text with ", "0"], [0, 0, 0, "No untranslated strings. Wrap text with ", "1"], @@ -3707,9 +3707,6 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "1"], [0, 0, 0, "Unexpected any. Specify a different type.", "2"] ], - "public/app/features/provisioning/dashboard.ts:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "public/app/features/query/components/QueryEditorRow.tsx:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "1"], diff --git a/public/app/features/dashboard-scene/saving/SaveDashboardDrawer.tsx b/public/app/features/dashboard-scene/saving/SaveDashboardDrawer.tsx index ab6a23e0285..2069179469b 100644 --- a/public/app/features/dashboard-scene/saving/SaveDashboardDrawer.tsx +++ b/public/app/features/dashboard-scene/saving/SaveDashboardDrawer.tsx @@ -53,7 +53,7 @@ export class SaveDashboardDrawer extends SceneObjectBase diff --git a/public/app/features/dashboard-scene/saving/SaveProvisionedDashboard.tsx b/public/app/features/dashboard-scene/saving/SaveProvisionedDashboard.tsx index 9084c635d15..9c8e89736aa 100644 --- a/public/app/features/dashboard-scene/saving/SaveProvisionedDashboard.tsx +++ b/public/app/features/dashboard-scene/saving/SaveProvisionedDashboard.tsx @@ -1,17 +1,39 @@ -import { css } from '@emotion/css'; -import { useEffect, useMemo, useState } from 'react'; +import { useEffect } from 'react'; +import { useForm } from 'react-hook-form'; -import { config, getBackendSrv } from '@grafana/runtime'; +import { AppEvents } from '@grafana/data'; +import { getAppEvents } from '@grafana/runtime'; import { Button, Stack, Box, TextArea, Field, Input, Alert } from '@grafana/ui'; import { AnnoKeyRepoName, AnnoKeyRepoPath } from 'app/features/apiserver/types'; import { DashboardMeta } from 'app/types'; +import { useUpdateRepositoryFilesMutation } from '../../provisioning/api'; import { DashboardScene } from '../scene/DashboardScene'; import { SaveDashboardDrawer } from './SaveDashboardDrawer'; import { SaveDashboardFormCommonOptions } from './SaveDashboardForm'; import { DashboardChangeInfo } from './shared'; +type FormData = { + ref: string; + path: string; + comment?: string; + repo: string; +}; + +function getDefaultValues(meta: DashboardMeta) { + const anno = meta.k8s?.annotations; + let ref = ''; + let path = anno?.[AnnoKeyRepoPath] ?? ''; + const repo = anno?.[AnnoKeyRepoName] ?? ''; + const idx = path.indexOf('#'); + if (idx > 0) { + ref = path.substring(idx + 1); + path = path.substring(0, idx); + } + return { ref, path, repo, comment: '' }; +} + export interface Props { meta: DashboardMeta; dashboard: DashboardScene; @@ -19,55 +41,38 @@ export interface Props { changeInfo: DashboardChangeInfo; } -export function SaveProvisionedDashboard({ meta, drawer, changeInfo }: Props) { - const dashboardJSON = useMemo(() => JSON.stringify(changeInfo.changedSaveModel, null, 2), [changeInfo]); - - const [repo, setRepo] = useState(); - const [path, setPath] = useState(); - const [ref, setRef] = useState(); - const [comment, setComment] = useState(); +export function SaveProvisionedDashboard({ meta, drawer, changeInfo, dashboard }: Props) { + const [saveDashboard, request] = useUpdateRepositoryFilesMutation(); + const { register, handleSubmit } = useForm({ defaultValues: getDefaultValues(meta) }); useEffect(() => { - const anno = meta.k8s?.annotations; - if (!anno) { - setRepo(''); - setPath(''); - setRef(''); + const appEvents = getAppEvents(); + if (request.isSuccess) { + appEvents.publish({ + type: AppEvents.alertSuccess.name, + payload: ['Dashboard saved'], + }); + dashboard.setState({ isDirty: false }); + // TODO Avoid full reload + window.location.reload(); + } else if (request.isError) { + appEvents.publish({ + type: AppEvents.alertError.name, + payload: ['Error saving dashboard', request.error], + }); + } + }, [request.isSuccess, request.isError, request.error, dashboard]); + + const doSave = ({ ref, path, comment, repo }: FormData) => { + if (!repo || !path) { return; } - let ref = ''; - let path = anno[AnnoKeyRepoPath] ?? ''; - const idx = path.indexOf('#'); - if (idx > 0) { - ref = path.substring(idx + 1); - path = path.substring(0, idx); - } - setRepo(anno[AnnoKeyRepoName]); - setPath(path); - setRef(ref); - }, [meta]); - - const doSave = () => { - const url = `apis/provisioning.grafana.app/v0alpha1/namespaces/${config.namespace}/repositories/${repo}/files/${path}`; - const params: Record = {}; - if (ref) { - params['ref'] = ref; - } - if (comment) { - params['comment'] = comment; - } - - getBackendSrv() - .put(url, dashboardJSON, { params }) - .then((v) => { - console.log('WROTE', v); - alert('WROTE value'); - }); + saveDashboard({ ref, name: repo, path, message: comment, body: changeInfo.changedSaveModel }); }; return ( -
- +
+
More warnings here... mostly exploratory interfaces. @@ -77,34 +82,21 @@ export function SaveProvisionedDashboard({ meta, drawer, changeInfo }: Props) { -
{repo}
+
- { - setPath(e.currentTarget.value); - }} - /> + - - { - setRef(e.currentTarget.value); - }} - /> + +