-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
170 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/components/WithTemplateConfig/WarningSavedDashboard/WarningSavedDashboard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React, { FC } from "react"; | ||
|
||
import { Alert, Button, HorizontalGroup } from "@grafana/ui"; | ||
|
||
type Props = { | ||
onClose: () => void; | ||
onAccept: () => void | ||
} | ||
|
||
const WarningNewDashboard: FC<Props> = ({ onClose, onAccept }) => ( | ||
<Alert | ||
title="Please save dashboard before using WITH templates" | ||
severity="error" | ||
elevated | ||
> | ||
<div> | ||
This dashboard is not saved yet. Please save it before using WITH templates. | ||
<p>Otherwise, the templates will not be saved</p> | ||
</div> | ||
<HorizontalGroup justify="flex-end" spacing="xs"> | ||
<Button fill={"text"} onClick={onClose}> | ||
Cancel | ||
</Button> | ||
<Button variant={"destructive"} fill={"text"} onClick={onAccept}> | ||
Proceed with WITH templates | ||
</Button> | ||
</HorizontalGroup> | ||
</Alert> | ||
) | ||
|
||
export default WarningNewDashboard; |
98 changes: 98 additions & 0 deletions
98
src/components/WithTemplateConfig/WithTemplateBody/WithTemplateBody.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React, { FC, useCallback, useEffect, useState } from 'react' | ||
|
||
import { Badge, Button, useStyles2 } from "@grafana/ui"; | ||
|
||
import TemplateEditor from "../TemplateEditor/TemplateEditor"; | ||
import useUpdateDatasource from "../hooks/useUpdateDatasource"; | ||
import useValidateExpr from "../hooks/useValidateExpr"; | ||
import { WithTemplateConfigProps } from "../index"; | ||
|
||
import getStyles from "./style"; | ||
|
||
interface Props extends WithTemplateConfigProps { | ||
handleClose: () => void; | ||
} | ||
|
||
const WithTemplateBody: FC<Props> = ({ datasource, dashboardUID, template, setTemplate, handleClose }) => { | ||
const styles = useStyles2(getStyles); | ||
|
||
const { validateResult, isValidExpr } = useValidateExpr(datasource.id) | ||
const { updateDatasource } = useUpdateDatasource({ | ||
datasourceUID: datasource.uid, | ||
dashboardUID | ||
}) | ||
|
||
const [value, setValue] = useState(template?.expr || "") | ||
const [isLoading, setIsLoading] = useState(false) | ||
|
||
const handleSave = useCallback(async () => { | ||
setIsLoading(true) | ||
const isValid = await isValidExpr(value) | ||
if (!isValid) { | ||
setIsLoading(false) | ||
return | ||
} | ||
try { | ||
const templates = await updateDatasource(value) | ||
datasource.withTemplatesUpdate(templates) | ||
setTemplate(templates.find(t => t?.uid === dashboardUID)) | ||
handleClose() | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
setIsLoading(false) | ||
}, [value, isValidExpr, updateDatasource, datasource, dashboardUID, setTemplate, handleClose]) | ||
|
||
useEffect(() => { | ||
setTemplate(datasource.withTemplates.find(t => t.uid === dashboardUID)) | ||
}, [setTemplate, datasource, dashboardUID]) | ||
|
||
useEffect(() => { | ||
value && isValidExpr(value) | ||
}, [value, isValidExpr]) | ||
|
||
useEffect(() => { | ||
setValue(template?.expr || "") | ||
}, [template]) | ||
|
||
return ( | ||
<div className={styles.body}> | ||
<TemplateEditor | ||
initialValue={value} | ||
datasource={datasource} | ||
onChange={setValue} | ||
/> | ||
<Badge | ||
icon={validateResult.icon || "info"} | ||
color={validateResult.color || "blue"} | ||
text={validateResult.error || validateResult.title} | ||
/> | ||
<div className={styles.button}> | ||
<a | ||
className="text-link" | ||
target="_blank" | ||
href={"https://github.com/VictoriaMetrics/grafana-datasource#how-to-use-with-templates"} | ||
rel="noreferrer" | ||
> | ||
<Button | ||
variant={'secondary'} | ||
fill={"text"} | ||
icon={"book"} | ||
size={"sm"} | ||
> | ||
How it works? | ||
</Button> | ||
</a> | ||
<Button | ||
variant={'success'} | ||
onClick={handleSave} | ||
disabled={isLoading} | ||
> | ||
Save | ||
</Button> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default WithTemplateBody; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters