grafana/public/app/features/alerting/components/NotificationChannelForm.tsx
Alex Khomenko 3b515e650c
Grafana-UI: Update React Hook Form to v7 (#33328)
* Update hook form

* Update Form component

* Update ChangePassword.tsx

* Update custom types

* Update SaveDashboardForm

* Update form story

* Update FieldArray.story.tsx

* Bump hook form version

* Update typescript to v4.2.4

* Update ForgottenPassword.tsx

* Update LoginForm.tsx

* Update SignupPage.tsx

* Update VerifyEmail.tsx

* Update AdminEditOrgPage.tsx

* Update UserCreatePage.tsx

* Update BasicSettings.tsx

* Update NotificationChannelForm.tsx

* Update NotificationChannelOptions.tsx

* Update NotificationSettings.tsx

* Update OptionElement.tsx

* Update AlertRuleForm.tsx

* Update AlertTypeStep.tsx

* Update AnnotationsField.tsx

* Update ConditionField.tsx

* Update ConditionsStep.tsx

* Update GroupAndNamespaceFields.tsx

* Update LabelsField.tsx

* Update QueryStep.tsx

* Update RowOptionsForm.tsx

* Update SaveDashboardAsForm.tsx

* Update NewDashboardsFolder.tsx

* Update ImportDashboardForm.tsx

* Update DashboardImportPage.tsx

* Update NewOrgPage.tsx

* Update OrgProfile.tsx

* Update UserInviteForm.tsx

* Update PlaylistForm.tsx

* Update ChangePasswordForm.tsx

* Update UserProfileEditForm.tsx

* Update TeamSettings.tsx

* Update SignupInvited.tsx

* Expose setValue from the Form

* Update typescript to v4.2.4

* Remove ref from field props

* Fix tests

* Revert TS update

* Use exact version

* Update latest batch of changes

* Reduce the number of strict TS errors

* Fix defaults

* more type error fixes

* Update CreateTeam

* fix folder picker in rule form

* fixes for hook form 7

* Update docs

Co-authored-by: Domas <domasx2@gmail.com>
2021-04-29 16:54:38 +03:00

129 lines
3.9 KiB
TypeScript

import React, { FC, useEffect } from 'react';
import { css } from '@emotion/css';
import { GrafanaTheme, SelectableValue } from '@grafana/data';
import { Button, FormAPI, HorizontalGroup, stylesFactory, useTheme, Spinner } from '@grafana/ui';
import { NotificationChannelType, NotificationChannelDTO, NotificationChannelSecureFields } from '../../../types';
import { NotificationSettings } from './NotificationSettings';
import { BasicSettings } from './BasicSettings';
import { ChannelSettings } from './ChannelSettings';
import config from 'app/core/config';
interface Props extends Omit<FormAPI<NotificationChannelDTO>, 'formState' | 'setValue'> {
selectableChannels: Array<SelectableValue<string>>;
selectedChannel?: NotificationChannelType;
imageRendererAvailable: boolean;
secureFields: NotificationChannelSecureFields;
resetSecureField: (key: string) => void;
onTestChannel: (data: NotificationChannelDTO) => void;
}
export interface NotificationSettingsProps
extends Omit<FormAPI<NotificationChannelDTO>, 'formState' | 'watch' | 'getValues' | 'setValue'> {
currentFormValues: NotificationChannelDTO;
}
export const NotificationChannelForm: FC<Props> = ({
control,
errors,
selectedChannel,
selectableChannels,
register,
watch,
getValues,
imageRendererAvailable,
onTestChannel,
resetSecureField,
secureFields,
}) => {
const styles = getStyles(useTheme());
useEffect(() => {
/*
Finds fields that have dependencies on other fields and removes duplicates.
Needs to be prefixed with settings.
*/
const fieldsToWatch =
new Set(
selectedChannel?.options
.filter((o) => o.showWhen.field)
.map((option) => {
return `settings.${option.showWhen.field}`;
})
) || [];
watch(['type', 'sendReminder', 'uploadImage', ...fieldsToWatch]);
}, [selectedChannel?.options, watch]);
const currentFormValues = getValues();
if (!selectedChannel) {
return <Spinner />;
}
return (
<div className={styles.formContainer}>
<div className={styles.formItem}>
<BasicSettings
selectedChannel={selectedChannel}
channels={selectableChannels}
secureFields={secureFields}
resetSecureField={resetSecureField}
currentFormValues={currentFormValues}
register={register}
errors={errors}
control={control}
/>
</div>
{/* If there are no non-required fields, don't render this section*/}
{selectedChannel.options.filter((o) => !o.required).length > 0 && (
<div className={styles.formItem}>
<ChannelSettings
selectedChannel={selectedChannel}
secureFields={secureFields}
resetSecureField={resetSecureField}
currentFormValues={currentFormValues}
register={register}
errors={errors}
control={control}
/>
</div>
)}
<div className={styles.formItem}>
<NotificationSettings
imageRendererAvailable={imageRendererAvailable}
currentFormValues={currentFormValues}
register={register}
errors={errors}
control={control}
/>
</div>
<div className={styles.formButtons}>
<HorizontalGroup>
<Button type="submit">Save</Button>
<Button type="button" variant="secondary" onClick={() => onTestChannel(getValues())}>
Test
</Button>
<a href={`${config.appSubUrl}/alerting/notifications`}>
<Button type="button" variant="secondary">
Back
</Button>
</a>
</HorizontalGroup>
</div>
</div>
);
};
const getStyles = stylesFactory((theme: GrafanaTheme) => {
return {
formContainer: css``,
formItem: css`
flex-grow: 1;
padding-top: ${theme.spacing.md};
`,
formButtons: css`
padding-top: ${theme.spacing.xl};
`,
};
});