NewNotificationChannelPage.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { config } from '@grafana/runtime';
  4. import { Form } from '@grafana/ui';
  5. import Page from 'app/core/components/Page/Page';
  6. import { getNavModel } from 'app/core/selectors/navModel';
  7. import { NotificationChannelDTO, StoreState } from '../../types';
  8. import { NotificationChannelForm } from './components/NotificationChannelForm';
  9. import { createNotificationChannel, loadNotificationTypes, testNotificationChannel } from './state/actions';
  10. import { resetSecureField } from './state/reducers';
  11. import {
  12. defaultValues,
  13. mapChannelsToSelectableValue,
  14. transformSubmitData,
  15. transformTestData,
  16. } from './utils/notificationChannels';
  17. class NewNotificationChannelPage extends PureComponent<Props> {
  18. componentDidMount() {
  19. this.props.loadNotificationTypes();
  20. }
  21. onSubmit = (data: NotificationChannelDTO) => {
  22. this.props.createNotificationChannel(transformSubmitData({ ...defaultValues, ...data }));
  23. };
  24. onTestChannel = (data: NotificationChannelDTO) => {
  25. this.props.testNotificationChannel(transformTestData({ ...defaultValues, ...data }));
  26. };
  27. render() {
  28. const { navModel, notificationChannelTypes } = this.props;
  29. return (
  30. <Page navModel={navModel}>
  31. <Page.Contents>
  32. <h2 className="page-sub-heading">New notification channel</h2>
  33. <Form onSubmit={this.onSubmit} validateOn="onChange" defaultValues={defaultValues} maxWidth={600}>
  34. {({ register, errors, control, getValues, watch }) => {
  35. const selectedChannel = notificationChannelTypes.find((c) => c.value === getValues().type.value);
  36. return (
  37. <NotificationChannelForm
  38. selectableChannels={mapChannelsToSelectableValue(notificationChannelTypes, true)}
  39. selectedChannel={selectedChannel}
  40. onTestChannel={this.onTestChannel}
  41. register={register}
  42. errors={errors}
  43. getValues={getValues}
  44. control={control}
  45. watch={watch}
  46. imageRendererAvailable={config.rendererAvailable}
  47. resetSecureField={this.props.resetSecureField}
  48. secureFields={{}}
  49. />
  50. );
  51. }}
  52. </Form>
  53. </Page.Contents>
  54. </Page>
  55. );
  56. }
  57. }
  58. const mapStateToProps = (state: StoreState) => ({
  59. navModel: getNavModel(state.navIndex, 'channels'),
  60. notificationChannelTypes: state.notificationChannel.notificationChannelTypes,
  61. });
  62. const mapDispatchToProps = {
  63. createNotificationChannel,
  64. loadNotificationTypes,
  65. testNotificationChannel,
  66. resetSecureField,
  67. };
  68. const connector = connect(mapStateToProps, mapDispatchToProps);
  69. type Props = ConnectedProps<typeof connector>;
  70. export default connector(NewNotificationChannelPage);