import React, { PureComponent } from 'react'; import { connect, MapDispatchToProps, MapStateToProps } from 'react-redux'; import { NavModel, SelectableValue } from '@grafana/data'; import { config } from '@grafana/runtime'; import { Form } from '@grafana/ui'; import Page from 'app/core/components/Page/Page'; import { NewNotificationChannelForm } from './components/NewNotificationChannelForm'; import { getNavModel } from 'app/core/selectors/navModel'; import { createNotificationChannel, loadNotificationTypes, testNotificationChannel } from './state/actions'; import { NotificationChannel, NotificationChannelDTO, StoreState } from '../../types'; interface OwnProps {} interface ConnectedProps { navModel: NavModel; notificationChannels: NotificationChannel[]; } interface DispatchProps { createNotificationChannel: typeof createNotificationChannel; loadNotificationTypes: typeof loadNotificationTypes; testNotificationChannel: typeof testNotificationChannel; } type Props = OwnProps & ConnectedProps & DispatchProps; const defaultValues: NotificationChannelDTO = { name: '', type: { value: 'email', label: 'Email' }, sendReminder: false, disableResolveMessage: false, frequency: '15m', settings: { uploadImage: config.rendererAvailable, autoResolve: true, httpMethod: 'POST', severity: 'critical', }, isDefault: false, }; class NewAlertNotificationPage extends PureComponent { componentDidMount() { this.props.loadNotificationTypes(); } onSubmit = (data: NotificationChannelDTO) => { /* Some settings can be options in a select, in order to not save a SelectableValue we need to use check if it is a SelectableValue and use its value. */ const settings = Object.fromEntries( Object.entries(data.settings).map(([key, value]) => { return [key, value.hasOwnProperty('value') ? value.value : value]; }) ); this.props.createNotificationChannel({ ...defaultValues, ...data, type: data.type.value, settings: { ...defaultValues.settings, ...settings }, }); }; onTestChannel = (data: NotificationChannelDTO) => { this.props.testNotificationChannel({ name: data.name, type: data.type.value, frequency: data.frequency ?? defaultValues.frequency, settings: { ...Object.assign(defaultValues.settings, data.settings) }, }); }; render() { const { navModel, notificationChannels } = this.props; /* Need to transform these as we have options on notificationChannels, this will render a dropdown within the select. TODO: Memoize? */ const selectableChannels: Array> = notificationChannels.map(channel => ({ value: channel.value, label: channel.label, description: channel.description, })); return (

New Notification Channel

{({ register, errors, control, getValues, watch }) => { const selectedChannel = notificationChannels.find(c => c.value === getValues().type.value); return ( ); }}
); } } const mapStateToProps: MapStateToProps = state => { return { navModel: getNavModel(state.navIndex, 'channels'), notificationChannels: state.alertRules.notificationChannels, }; }; const mapDispatchToProps: MapDispatchToProps = { createNotificationChannel, loadNotificationTypes, testNotificationChannel, }; export default connect(mapStateToProps, mapDispatchToProps)(NewAlertNotificationPage);