All files / modules/15-notifications/modals/ConfigureNotificationsModal/views/ConfigurePagerDutyNotifications ConfigurePagerDutyNotifications.tsx

83.72% Statements 36/43
58.33% Branches 14/24
100% Functions 6/6
85.71% Lines 36/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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              28x 28x 28x 28x 28x 28x 28x 28x 28x 28x   28x   28x                                   28x       28x 5x 5x 5x 5x 5x   5x 1x       1x 1x 1x           1x       1x 1x             5x   1x                 28x 1x   1x 1x           1x                                             4x                                                     28x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useState } from 'react'
import { Formik } from 'formik'
import * as Yup from 'yup'
import { FormikForm, FormInput, Button, Layout, Icon, Text, Heading, ButtonProps } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import cx from 'classnames'
import { useToaster } from '@common/components'
import UserGroupsInput from '@common/components/UserGroupsInput/UserGroupsInput'
import { useStrings } from 'framework/strings'
import { useTestNotificationSetting, PagerDutySettingDTO } from 'services/notifications'
import type { PagerDutyNotificationConfiguration } from '@notifications/interfaces/Notifications'
import { TestStatus, NotificationType } from '@notifications/interfaces/Notifications'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import css from '../../ConfigureNotificationsModal.module.scss'
 
interface ConfigurePagerDutyNotificationsProps {
  onSuccess: (config: PagerDutyNotificationConfiguration) => void
  hideModal: () => void
  isStep?: boolean
  withoutHeading?: boolean
  onBack?: () => void
  submitButtonText?: string
  config?: PagerDutyNotificationConfiguration
}
 
interface PagerDutyNotificationData {
  key: string
  pagerDutyKey?: string
  userGroups: string[]
}
 
export const TestPagerDutyNotifications: React.FC<{
  data: PagerDutyNotificationData
  onClick?: () => Promise<boolean>
  buttonProps?: ButtonProps
}> = ({ data, onClick, buttonProps }) => {
  const { accountId } = useParams<AccountPathProps>()
  const { getString } = useStrings()
  const [testStatus, setTestStatus] = useState<TestStatus>(TestStatus.INIT)
  const { mutate: testNotificationSetting } = useTestNotificationSetting({})
  const { showSuccess, showError } = useToaster()
 
  const handleTest = async (testData: PagerDutyNotificationData): Promise<void> => {
    Iif (onClick) {
      const success = await onClick()
      if (!success) return
    }
    try {
      setTestStatus(TestStatus.INIT)
      const resp = await testNotificationSetting({
        accountId,
        type: 'PAGERDUTY',
        recipient: testData.key || testData.pagerDutyKey,
        notificationId: 'asd'
      } as PagerDutySettingDTO)
      Iif (resp.status === 'SUCCESS' && resp.data) {
        showSuccess(getString('notifications.pagerDutyTestSuccess'))
        setTestStatus(TestStatus.SUCCESS)
      } else {
        showError(getString('somethingWentWrong'))
        setTestStatus(TestStatus.FAILED)
      }
    } catch (err) {
      showError(getString('notifications.invalidPagerDutyKey'))
      setTestStatus(TestStatus.ERROR)
    }
  }
  return (
    <>
      <Button text={getString('test')} onClick={() => handleTest(data)} {...buttonProps} />
      {testStatus === TestStatus.SUCCESS ? <Icon name="tick" className={cx(css.statusIcon, css.green)} /> : null}
      {testStatus === TestStatus.FAILED || testStatus === TestStatus.ERROR ? (
        <Icon name="cross" className={cx(css.statusIcon, css.red)} />
      ) : null}
    </>
  )
}
 
const ConfigurePagerDutyNotifications: React.FC<ConfigurePagerDutyNotificationsProps> = props => {
  const { getString } = useStrings()
 
  const handleSubmit = (formData: PagerDutyNotificationData): void => {
    props.onSuccess({
      type: NotificationType.PagerDuty,
      ...formData
    })
  }
 
  return (
    <div className={css.body}>
      <Layout.Vertical spacing="large">
        {props.withoutHeading ? null : (
          <>
            <Icon name="service-pagerduty" size={24} />
            <Heading className={css.title}>{getString('notifications.titlePagerDuty')}</Heading>
          </>
        )}
        <Text>{getString('notifications.helpPagerDuty')}</Text>
        <Text>{getString('notifications.infoPagerDuty')}</Text>
        <Formik
          onSubmit={handleSubmit}
          validationSchema={Yup.object().shape({
            key: Yup.string().trim().required(getString('notifications.validationPDKey'))
          })}
          initialValues={{
            key: '',
            userGroups: [],
            ...props.config
          }}
        >
          {formik => {
            return (
              <FormikForm>
                <FormInput.Text name={'key'} label={getString('notifications.labelPDKey')} />
                <Layout.Horizontal margin={{ bottom: 'xxlarge' }} style={{ alignItems: 'center' }}>
                  <TestPagerDutyNotifications data={formik.values} />
                </Layout.Horizontal>
                <UserGroupsInput name="userGroups" label={getString('notifications.labelSlackUserGroups')} />
                {props.isStep ? (
                  <Layout.Horizontal spacing="medium" margin={{ top: 'xlarge' }}>
                    <Button text={getString('back')} onClick={props.onBack} />
                    <Button text={props.submitButtonText || getString('next')} intent="primary" type="submit" />
                  </Layout.Horizontal>
                ) : (
                  <Layout.Horizontal spacing={'medium'} margin={{ top: 'xxlarge' }}>
                    <Button type={'submit'} intent={'primary'} text={props.submitButtonText || getString('submit')} />
                    <Button text={getString('cancel')} onClick={props.hideModal} />
                  </Layout.Horizontal>
                )}
              </FormikForm>
            )
          }}
        </Formik>
      </Layout.Vertical>
    </div>
  )
}
 
export default ConfigurePagerDutyNotifications