All files / modules/75-ci/pages/get-started-with-ci/InfraProvisioningWizard InfraProvisioningWizard.tsx

82.43% Statements 61/74
58.75% Branches 47/80
85.71% Functions 12/14
81.94% Lines 59/72

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242              4x 4x 4x 4x 4x               4x 4x 4x   4x   4x 10x 10x 10x 10x   10x 10x 10x 10x   10x               10x 6x 6x 6x 8x 6x         10x           1x 1x                   2x 2x 2x 1x   2x 1x 1x 1x             2x 2x 2x                                   1x 1x 1x 1x 1x                                                                                                 1x 1x 1x                 10x     10x 10x 2x   8x           10x                                                 1x 1x 1x 1x                               2x             5x              
/*
 * Copyright 2022 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 { Container, Button, ButtonVariation, Layout, MultiStepProgressIndicator } from '@harness/uicore'
import { useStrings } from 'framework/strings'
import { InfraProvisioningCarousel } from '../InfraProvisioningCarousel/InfraProvisioningCarousel'
import {
  InfraProvisioningWizardProps,
  WizardStep,
  HostedByHarnessBuildLocation,
  InfraProvisiongWizardStepId,
  StepStatus,
  GitAuthenticationMethod
} from './Constants'
import { SelectBuildLocation } from './SelectBuildLocation'
import { SelectGitProvider, SelectGitProviderRef } from './SelectGitProvider'
import { SelectRepository, SelectRepositoryRef } from './SelectRepository'
 
import css from './InfraProvisioningWizard.module.scss'
 
export const InfraProvisioningWizard: React.FC<InfraProvisioningWizardProps> = props => {
  const { lastConfiguredWizardStepId = InfraProvisiongWizardStepId.SelectBuildLocation } = props
  const { getString } = useStrings()
  const [showDialog, setShowDialog] = useState<boolean>(false)
  const [disable, setDisable] = useState<boolean>(false)
  const [currentWizardStepId, setCurrentWizardStepId] =
    useState<InfraProvisiongWizardStepId>(lastConfiguredWizardStepId)
  const selectGitProviderRef = React.useRef<SelectGitProviderRef | null>(null)
  const selectRepositoryRef = React.useRef<SelectRepositoryRef | null>(null)
  const [showError, setShowError] = useState<boolean>(false)
 
  const [wizardStepStatus, setWizardStepStatus] = useState<Map<InfraProvisiongWizardStepId, StepStatus>>(
    new Map<InfraProvisiongWizardStepId, StepStatus>([
      [InfraProvisiongWizardStepId.SelectBuildLocation, StepStatus.ToDo],
      [InfraProvisiongWizardStepId.SelectGitProvider, StepStatus.ToDo],
      [InfraProvisiongWizardStepId.SelectRepository, StepStatus.ToDo]
    ])
  )
 
  const updateStepStatus = React.useCallback((stepIds: InfraProvisiongWizardStepId[], status: StepStatus) => {
    Eif (Array.isArray(stepIds)) {
      setWizardStepStatus((prevState: Map<InfraProvisiongWizardStepId, StepStatus>) => {
        const clonedState = new Map(prevState)
        stepIds.forEach((item: InfraProvisiongWizardStepId) => clonedState.set(item, status))
        return clonedState
      })
    }
  }, [])
 
  const WizardSteps: Map<InfraProvisiongWizardStepId, WizardStep> = new Map([
    [
      InfraProvisiongWizardStepId.SelectBuildLocation,
      {
        stepRender: <SelectBuildLocation selectedBuildLocation={HostedByHarnessBuildLocation} />,
        onClickNext: () => {
          updateStepStatus([InfraProvisiongWizardStepId.SelectBuildLocation], StepStatus.InProgress)
          setShowDialog(true)
        },
        stepFooterLabel: 'ci.getStartedWithCI.configInfra'
      }
    ],
    [
      InfraProvisiongWizardStepId.SelectGitProvider,
      {
        stepRender: <SelectGitProvider ref={selectGitProviderRef} />,
        onClickNext: () => {
          const { values, setFieldTouched } = selectGitProviderRef.current || {}
          const { gitProvider } = values || {}
          if (!gitProvider) {
            setFieldTouched?.('gitProvider', true)
          }
          if (gitProvider) {
            setFieldTouched?.('gitAuthenticationMethod', false)
            setCurrentWizardStepId(InfraProvisiongWizardStepId.SelectGitProviderWithAuthenticationMethod)
            updateStepStatus(
              [InfraProvisiongWizardStepId.SelectGitProviderWithAuthenticationMethod],
              StepStatus.InProgress
            )
          }
        },
        onClickBack: () => {
          setDisable(false)
          setCurrentWizardStepId(InfraProvisiongWizardStepId.SelectBuildLocation)
          updateStepStatus(
            [InfraProvisiongWizardStepId.SelectBuildLocation, InfraProvisiongWizardStepId.SelectGitProvider],
            StepStatus.ToDo
          )
        },
        stepFooterLabel: 'ci.getStartedWithCI.selectRepo'
      }
    ],
    [
      InfraProvisiongWizardStepId.SelectGitProviderWithAuthenticationMethod,
      {
        stepRender: (
          <SelectGitProvider
            ref={selectGitProviderRef}
            selectedGitProvider={selectGitProviderRef.current?.values.gitProvider}
          />
        ),
        onClickNext: () => {
          const { values, setFieldTouched } = selectGitProviderRef.current || {}
          const { accessToken, gitProvider, gitAuthenticationMethod } = values || {}
          Eif (!gitAuthenticationMethod) {
            setFieldTouched?.('gitAuthenticationMethod', true)
            return
          }
          if (!accessToken) {
            setFieldTouched?.('accessToken', true)
          }
          if (
            (gitAuthenticationMethod === GitAuthenticationMethod.AccessToken && accessToken && gitProvider) ||
            (gitAuthenticationMethod === GitAuthenticationMethod.OAuth && gitProvider)
          ) {
            setCurrentWizardStepId(InfraProvisiongWizardStepId.SelectRepository)
            updateStepStatus(
              [
                InfraProvisiongWizardStepId.SelectGitProvider,
                InfraProvisiongWizardStepId.SelectGitProviderWithAuthenticationMethod
              ],
              StepStatus.Success
            )
            updateStepStatus([InfraProvisiongWizardStepId.SelectRepository], StepStatus.InProgress)
          }
        },
        onClickBack: () => {
          setDisable(false)
          setCurrentWizardStepId(InfraProvisiongWizardStepId.SelectGitProvider)
          updateStepStatus(
            [
              InfraProvisiongWizardStepId.SelectGitProvider,
              InfraProvisiongWizardStepId.SelectGitProviderWithAuthenticationMethod
            ],
            StepStatus.ToDo
          )
        },
        stepFooterLabel: 'ci.getStartedWithCI.selectRepo'
      }
    ],
    [
      InfraProvisiongWizardStepId.SelectRepository,
      {
        stepRender: <SelectRepository ref={selectRepositoryRef} showError={showError} />,
        onClickBack: () => {
          setCurrentWizardStepId(InfraProvisiongWizardStepId.SelectGitProviderWithAuthenticationMethod)
          updateStepStatus(
            [
              InfraProvisiongWizardStepId.SelectGitProviderWithAuthenticationMethod,
              InfraProvisiongWizardStepId.SelectRepository
            ],
            StepStatus.ToDo
          )
        },
        onClickNext: () => {
          const shouldShowError = !selectRepositoryRef.current?.repository.name
          setShowError(shouldShowError)
          Iif (!shouldShowError) {
            updateStepStatus([InfraProvisiongWizardStepId.SelectRepository], StepStatus.Success)
          }
        },
        stepFooterLabel: 'ci.getStartedWithCI.createPipeline'
      }
    ]
  ])
 
  const { stepRender, onClickBack, onClickNext, stepFooterLabel } = WizardSteps.get(currentWizardStepId) ?? {}
 
  let buttonLabel: string
  Eif (stepFooterLabel) {
    if (currentWizardStepId === InfraProvisiongWizardStepId.SelectRepository) {
      buttonLabel = getString(stepFooterLabel)
    } else {
      buttonLabel = `${getString('next')}: ${getString(stepFooterLabel)}`
    }
  } else {
    buttonLabel = getString('next')
  }
 
  return stepRender ? (
    <Layout.Vertical
      padding={{ left: 'huge', right: 'huge', top: 'huge' }}
      flex={{ justifyContent: 'space-between', alignItems: 'flex-start' }}
      height="100%"
    >
      <Layout.Vertical width="100%" height="90%">
        <Container padding={{ top: 'large', bottom: 'large' }}>
          <MultiStepProgressIndicator
            progressMap={
              new Map([
                [0, wizardStepStatus.get(InfraProvisiongWizardStepId.SelectBuildLocation) || 'TODO'],
                [1, wizardStepStatus.get(InfraProvisiongWizardStepId.SelectGitProvider) || 'TODO'],
                [2, wizardStepStatus.get(InfraProvisiongWizardStepId.SelectRepository) || 'TODO']
              ])
            }
          />
        </Container>
        {stepRender}
      </Layout.Vertical>
      {showDialog ? (
        <InfraProvisioningCarousel
          show={showDialog}
          provisioningStatus="IN_PROGRESS"
          onClose={() => {
            setShowDialog(false)
            updateStepStatus([InfraProvisiongWizardStepId.SelectBuildLocation], StepStatus.Success)
            updateStepStatus([InfraProvisiongWizardStepId.SelectGitProvider], StepStatus.InProgress)
            setCurrentWizardStepId(InfraProvisiongWizardStepId.SelectGitProvider)
          }}
        />
      ) : null}
      <Layout.Horizontal
        spacing="medium"
        padding={{ top: 'large', bottom: 'xlarge' }}
        className={css.footer}
        width="100%"
      >
        {currentWizardStepId !== InfraProvisiongWizardStepId.SelectBuildLocation ? (
          <Button
            variation={ButtonVariation.SECONDARY}
            text={getString('back')}
            icon="chevron-left"
            minimal
            onClick={() => onClickBack?.()}
          />
        ) : null}
        <Button
          text={buttonLabel}
          variation={ButtonVariation.PRIMARY}
          rightIcon="chevron-right"
          onClick={() => onClickNext?.()}
          disabled={disable}
        />
      </Layout.Horizontal>
    </Layout.Vertical>
  ) : null
}