All files / modules/75-cd/pages/gitops/NativeArgo/TestConnection TestConnection.tsx

100% Statements 33/33
81.82% Branches 45/55
100% Functions 9/9
100% Lines 33/33

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                4x 4x 4x 4x 4x     4x   4x 4x 4x 4x         4x 10x 9x 9x 9x 9x   9x                 9x 9x 2x     9x 1x 1x     9x 3x   2x 2x     1x 1x       9x 3x     9x                                                                                               1x                                                
/*
 * 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.
 */
 
/* eslint-disable @typescript-eslint/no-unused-vars */
import React, { useState, useEffect } from 'react'
import cx from 'classnames'
import { Layout, Container, StepsProgress, Button, Card, Icon, Text, ButtonVariation } from '@wings-software/uicore'
import { Color, Intent } from '@harness/design-system'
import { String, useStrings } from 'framework/strings'
import type { ConnectedArgoGitOpsInfoDTO } from 'services/cd-ng'
import type { BaseProviderStepProps } from '../../types'
import css from './TestConnection.module.scss'
 
export enum Status {
  PROCESS = 'PROCESS',
  DONE = 'DONE',
  ERROR = 'ERROR'
}
 
type TestConnectionProps = BaseProviderStepProps
 
export default function TestConnection(props: TestConnectionProps): React.ReactElement {
  const [currentStep] = useState(1)
  const [currentStatus, setCurrentStatus] = useState<Status>(Status.PROCESS)
  const [currentIntent, setCurrentIntent] = useState<Intent>(Intent.NONE)
  const url = (props?.prevStepData?.spec as ConnectedArgoGitOpsInfoDTO)?.adapterUrl
  const healthCheckURL = url?.endsWith('/') ? `${url}api/version` : `${url}/api/version`
  const stepName = (
    <String
      stringID="cd.testConnectionStepName"
      vars={{
        url: healthCheckURL
      }}
      useRichText
    />
  )
 
  const { getString } = useStrings()
  const handleSuccess = (): void => {
    props.onClose?.()
  }
 
  const launchArgoDashboard = (): void => {
    const provider = props?.prevStepData
    props.onLaunchArgoDashboard?.(provider)
  }
 
  const validateAdapterURL = (): void => {
    fetch(healthCheckURL)
      .then(() => {
        setCurrentStatus(Status.DONE)
        setCurrentIntent(Intent.SUCCESS)
      })
      .catch(() => {
        setCurrentStatus(Status.ERROR)
        setCurrentIntent(Intent.DANGER)
      })
  }
 
  useEffect(() => {
    validateAdapterURL()
  }, [])
 
  return (
    <Layout.Vertical className={css.stepContainer}>
      <div className={css.heading}>Test Connection</div>
 
      <Container className={css.connectorForm}>
        <Container style={{ minHeight: 460 }}>
          <Layout.Vertical spacing="large" className={css.stepFormContainer}>
            <StepsProgress
              steps={[stepName]}
              intent={currentIntent}
              current={currentStep}
              currentStatus={currentStatus}
            />
 
            {(currentStatus === Status.DONE || currentStatus === Status.ERROR) && (
              <div className={cx(css.validationStatus, { [css.success]: currentStatus === Status.DONE })}>
                {currentStatus === Status.DONE ? (
                  <> {getString('common.test.connectionSuccessful')} </>
                ) : (
                  <Card className={css.card}>
                    <Text color={Color.RED_700} font={{ weight: 'semi-bold' }} style={{ marginBottom: '16px' }}>
                      {getString('cd.notReachable')}
                      <a href={url} target="_blank" rel="noreferrer">
                        {healthCheckURL}
                      </a>
                    </Text>
                    <div className={css.issueInfo}>
                      <Icon color={Color.GREY_700} name="info" style={{ marginRight: '8px' }}></Icon>
                      <String stringID="cd.connectionIssueInfo" useRichText />
                    </div>
 
                    <div className={css.issueSuggestion}>
                      <Icon color={Color.GREY_700} name="lightbulb" style={{ marginRight: '8px' }}></Icon>
                      <String stringID="cd.connectionIssueSuggestion" useRichText />
                    </div>
                  </Card>
                )}
              </div>
            )}
          </Layout.Vertical>
        </Container>
 
        <Layout.Horizontal spacing="large">
          {currentStatus === Status.ERROR && (
            <Button
              variation={ButtonVariation.SECONDARY}
              text={getString('back')}
              icon="chevron-left"
              onClick={() => props?.previousStep?.(props?.prevStepData)}
              data-name="commonGitBackButton"
            />
          )}
          <Button
            variation={currentStatus === Status.ERROR ? ButtonVariation.PRIMARY : ButtonVariation.SECONDARY}
            text={getString('finish')}
            onClick={handleSuccess}
            className={css.nextButton}
          />
 
          {currentStatus === Status.DONE && (
            <Button
              variation={ButtonVariation.PRIMARY}
              text={getString('cd.launchArgo')}
              onClick={launchArgoDashboard}
              className={css.nextButton}
            />
          )}
        </Layout.Horizontal>
      </Container>
    </Layout.Vertical>
  )
}