/
CheckEditor.tsx
183 lines (172 loc) · 6.17 KB
/
CheckEditor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React, { FC, useState, useMemo } from 'react';
import { css } from 'emotion';
import { Button, ConfirmModal, Field, Input, HorizontalGroup, Select, Legend, Alert, useStyles } from '@grafana/ui';
import { useAsyncCallback } from 'react-async-hook';
import { Check, CheckType, OrgRole, CheckFormValues, SubmissionError } from 'types';
import { SMDataSource } from 'datasource/DataSource';
import { hasRole } from 'utils';
import { getDefaultValuesFromCheck, getCheckFromFormValues } from './checkFormTransformations';
import { validateJob, validateTarget } from 'validation';
import CheckTarget from 'components/CheckTarget';
import { Subheader } from 'components/Subheader';
import { HorizonalCheckboxField } from 'components/HorizonalCheckboxField';
import { CheckSettings } from './CheckSettings';
import { ProbeOptions } from './ProbeOptions';
import { CHECK_TYPE_OPTIONS, fallbackCheck } from 'components/constants';
import { useForm, FormContext, Controller } from 'react-hook-form';
import { GrafanaTheme } from '@grafana/data';
import { CheckUsage } from '../CheckUsage';
interface Props {
check?: Check;
instance: SMDataSource;
onReturn: (reload: boolean) => void;
}
const getStyles = (theme: GrafanaTheme) => ({
formBody: css`
margin-bottom: ${theme.spacing.sm};
`,
enabledField: css`
display: flex;
align-items: flex-start;
margin-bottom: ${theme.spacing.md};
`,
enabledCheckbox: css`
margin-right: ${theme.spacing.sm};
display: flex;
`,
breakLine: css`
margin-top: ${theme.spacing.lg};
`,
submissionError: css`
margin-top: ${theme.spacing.md};
`,
});
export const CheckEditor: FC<Props> = ({ check, instance, onReturn }) => {
const [showDeleteModal, setShowDeleteModal] = useState(false);
const styles = useStyles(getStyles);
const defaultValues = useMemo(() => getDefaultValuesFromCheck(check), [check]);
const formMethods = useForm<CheckFormValues>({ defaultValues, mode: 'onChange' });
const selectedCheckType = formMethods.watch('checkType').value as CheckType;
const isEditor = hasRole(OrgRole.EDITOR);
const { execute: onSubmit, error, loading: submitting } = useAsyncCallback(async (values: CheckFormValues) => {
const updatedCheck = getCheckFromFormValues(values, defaultValues);
if (check?.id) {
await instance.updateCheck({
id: check.id,
tenantId: check.tenantId,
...updatedCheck,
});
} else {
await instance.addCheck(updatedCheck);
}
onReturn(true);
});
const submissionError = error as SubmissionError;
const onRemoveCheck = async () => {
const id = check?.id;
if (!id) {
return;
}
await instance.deleteCheck(id);
onReturn(true);
};
const target = formMethods.watch('target', '') as string;
return (
<FormContext {...formMethods}>
<form onSubmit={formMethods.handleSubmit(onSubmit)}>
<Legend>{check?.id ? 'Edit Check' : 'Add Check'}</Legend>
<div className={styles.formBody}>
<Subheader>Check Details</Subheader>
<Field label="Check type" disabled={check?.id ? true : false}>
<Controller
name="checkType"
placeholder="Check type"
control={formMethods.control}
as={Select}
options={CHECK_TYPE_OPTIONS}
width={30}
/>
</Field>
<HorizonalCheckboxField
disabled={!isEditor}
name="enabled"
id="check-form-enabled"
label="Enabled"
description="If a check is enabled, metrics and logs are published to your Grafana Cloud stack."
/>
<Field
label="Job name"
description="Name used for job label"
disabled={!isEditor}
invalid={Boolean(formMethods.errors.job)}
error={formMethods.errors.job?.message}
>
<Input
id="check-editor-job-input"
ref={formMethods.register({
required: true,
validate: validateJob,
})}
name="job"
type="string"
placeholder="jobName"
/>
</Field>
<Controller
name="target"
as={CheckTarget}
control={formMethods.control}
target={target}
valueName="target"
typeOfCheck={selectedCheckType}
invalid={Boolean(formMethods.errors.target)}
error={formMethods.errors.target?.message}
rules={{
required: true,
validate: target => validateTarget(selectedCheckType, target),
}}
disabled={!isEditor}
/>
<hr className={styles.breakLine} />
<ProbeOptions
isEditor={isEditor}
timeout={check?.timeout ?? fallbackCheck.timeout}
frequency={check?.frequency ?? fallbackCheck.frequency}
probes={check?.probes ?? fallbackCheck.probes}
/>
<CheckUsage />
<CheckSettings typeOfCheck={selectedCheckType} isEditor={isEditor} />
</div>
<HorizontalGroup>
<Button
type="submit"
disabled={formMethods.formState.isSubmitting || submitting || Object.keys(formMethods.errors).length > 0}
>
Save
</Button>
{check?.id && (
<Button variant="destructive" onClick={() => setShowDeleteModal(true)} disabled={!isEditor} type="button">
Delete Check
</Button>
)}
<ConfirmModal
isOpen={showDeleteModal}
title="Delete check"
body="Are you sure you want to delete this check?"
confirmText="Delete check"
onConfirm={onRemoveCheck}
onDismiss={() => setShowDeleteModal(false)}
/>
<a onClick={() => onReturn(true)}>Back</a>
</HorizontalGroup>
{submissionError && (
<div className={styles.submissionError}>
<Alert title="Save failed" severity="error">
{`${submissionError.status}: ${submissionError.message}`}
</Alert>
</div>
)}
</form>
</FormContext>
);
};