grafana/public/app/features/alerting/components/NotificationChannelForm.tsx
Torkel Ödegaard 0132bca93a
Notifications: UX tweak to redesign of form and sections to make it left aligned and cleaner (#27479)
* UX: Redesign of form and sections to make it left aligned and cleaner

* reduced padding

* design tweaks
2020-09-11 08:10:07 +02:00

115 lines
3.4 KiB
TypeScript

import React, { FC, useEffect } from 'react';
import { css } from 'emotion';
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';
interface Props extends Omit<FormAPI<NotificationChannelDTO>, 'formState'> {
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'> {
currentFormValues: NotificationChannelDTO;
}
export const NotificationChannelForm: FC<Props> = ({
control,
errors,
selectedChannel,
selectableChannels,
register,
watch,
getValues,
imageRendererAvailable,
onTestChannel,
resetSecureField,
secureFields,
}) => {
const styles = getStyles(useTheme());
useEffect(() => {
watch(['type', 'settings.priority', 'sendReminder', 'uploadImage']);
}, []);
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({ nest: true }))}>
Test
</Button>
<a href="/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};
`,
};
});