All files / modules/75-ce/components/PerspectiveReportsAndBudget/CreateBudgetSteps SelectPerspective.tsx

76.74% Statements 33/43
69.23% Branches 27/39
85.71% Functions 6/7
75.61% Lines 31/41

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              5x 5x                       5x 5x 5x 5x 5x 5x 5x 5x 5x   5x                         5x 2x 2x   2x 2x 2x       2x           2x   2x   8x         2x 4x   1x         1x     2x                                               2x       1x                   5x                                                                                                                                               5x  
/*
 * 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 from 'react'
import {
  Container,
  Text,
  FormikForm,
  FormInput,
  Formik,
  Layout,
  Button,
  FlexExpander,
  ButtonVariation,
  StepProps
} from '@wings-software/uicore'
import { FontVariation, Color } from '@harness/design-system'
import { useParams, useHistory } from 'react-router-dom'
import { get } from 'lodash-es'
import { useToaster } from '@common/components'
import routes from '@common/RouteDefinitions'
import { useCreatePerspective, CEView, Budget } from 'services/ce'
import { useStrings } from 'framework/strings'
import { QlceView, useFetchPerspectiveListQuery } from 'services/ce/services'
import { generateId, CREATE_CALL_OBJECT } from '@ce/utils/perspectiveUtils'
import type { BudgetStepData } from '../types'
import css from '../PerspectiveCreateBudget.module.scss'
 
interface PerspectiveForm {
  perspective: string
  budgetName: string
}
interface SelectPerspectiveProps {
  name: string
  perspective?: string
  isEditMode?: boolean
  budget?: Budget
}
 
const SelectPerspective: (props: StepProps<BudgetStepData> & SelectPerspectiveProps) => JSX.Element = props => {
  const { getString } = useStrings()
  const { nextStep, prevStepData, perspective, isEditMode, budget: { name } = {} } = props
 
  const { showError } = useToaster()
  const history = useHistory()
  const { accountId } = useParams<{
    accountId: string
  }>()
 
  const { mutate: createView, loading: createViewLoading } = useCreatePerspective({
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  const [{ data: perspectiveData }] = useFetchPerspectiveListQuery()
 
  const perspectiveList = (perspectiveData?.perspectives?.customerViews || []) as QlceView[]
 
  const items = perspectiveList.map(pName => ({
    label: pName.name as string,
    value: pName.id as string
  }))
 
  const handleSubmit = (data: PerspectiveForm) => {
    const perspectiveName = items.find(e => e.value === data.perspective)?.label as string
 
    const nextStepData = {
      perspective: data.perspective,
      perspectiveName: perspectiveName,
      budgetName: data.budgetName
    }
    nextStep?.(nextStepData)
  }
 
  const createNewPerspective = async () => {
    const perspectiveName = `Perspective-${generateId(6).toUpperCase()}`
    const createData = { ...CREATE_CALL_OBJECT, name: perspectiveName }
 
    try {
      const response = await createView(createData as CEView)
      const { data } = response
 
      const uuid = data?.uuid
 
      if (uuid) {
        history.push(
          routes.toCECreatePerspective({
            accountId: accountId,
            perspectiveId: uuid
          })
        )
      }
    } catch (e) {
      const errMessage = e.data.message
      showError(errMessage)
    }
  }
 
  return (
    <Container>
      <Formik<PerspectiveForm>
        onSubmit={data => {
          handleSubmit(data)
        }}
        formName="selectPerspective"
        enableReinitialize={true}
        initialValues={{
          perspective: perspective || get(prevStepData, 'perspective') || '',
          budgetName: name || get(prevStepData, 'budgetName') || ''
        }}
      >
        {formikProps => {
          return (
            <FormikForm>
              <Container className={css.selectPerspectiveContainer}>
                <Text font={{ variation: FontVariation.H4 }}>
                  {getString('ce.perspectives.budgets.defineTarget.title')}
                </Text>
 
                <Container
                  margin={{
                    top: 'xxlarge'
                  }}
                >
                  <Container padding="large" background={Color.PRIMARY_1}>
                    <Text>{getString('ce.perspectives.budgets.defineTarget.text')}</Text>
                  </Container>
 
                  <Layout.Horizontal
                    margin={{
                      top: 'xxlarge'
                    }}
                    spacing="large"
                  >
                    <Container width={280}>
                      <FormInput.Select
                        items={items}
                        name={'perspective'}
                        disabled={isEditMode || !!perspective}
                        label={getString('ce.perspectives.budgets.defineTarget.selectPerspective')}
                      />
                      <Container margin={{ top: 'xlarge' }}>
                        <FormInput.Text
                          name="budgetName"
                          placeholder={getString('ce.perspectives.budgets.defineTarget.budgetName')}
                          label={getString('ce.perspectives.budgets.defineTarget.budgetName')}
                        />
                      </Container>
                    </Container>
                    {!isEditMode && !perspective ? (
                      <Button
                        margin={{
                          top: 'large',
                          left: 'xlarge'
                        }}
                        loading={createViewLoading}
                        onClick={createNewPerspective}
                        text={getString('ce.perspectives.budgets.defineTarget.createNewPerspective')}
                        icon="plus"
                        variation={ButtonVariation.SECONDARY}
                      />
                    ) : null}
                  </Layout.Horizontal>
                </Container>
                <FlexExpander />
                <Container>
                  <Button
                    type="submit"
                    intent="primary"
                    rightIcon={'chevron-right'}
                    disabled={!formikProps.values.perspective || createViewLoading}
                  >
                    {getString('continue')}
                  </Button>
                </Container>
              </Container>
            </FormikForm>
          )
        }}
      </Formik>
    </Container>
  )
}
 
export default SelectPerspective