All files / modules/85-cv/hooks/useErrorBudgetRestHook/views ErrorBudgetResetForm.tsx

100% Statements 14/14
100% Branches 0/0
100% Functions 3/3
100% Lines 14/14

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              10x 10x                         10x 10x 10x   10x 10x 10x           10x 4x   4x                                                                                           2x               20x                                                                                                                                                                                 10x  
/*
 * 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 from 'react'
import {
  Button,
  ButtonVariation,
  Container,
  Formik,
  FormikForm,
  FormInput,
  Heading,
  Icon,
  Layout,
  Text,
  Accordion
} from '@harness/uicore'
import { FontVariation, Color } from '@harness/design-system'
import * as yup from 'yup'
import { useStrings } from 'framework/strings'
import type { SLOErrorBudgetResetDTO } from 'services/cv'
import { getRiskColorValue } from '@cv/utils/CommonUtils'
import ErrorBudgetResetHistory from '@cv/hooks/useErrorBudgetRestHook/views/ErrorBudgetResetHistory'
import {
  calculateErrorBudgetByIncrement,
  calculateRemainingErrorBudgetByIncrement
} from '../useErrorBudgetRestHook.utils'
import type { ErrorBudgetResetFormProps } from '../useErrorBudgetRestHook.types'
 
const ErrorBudgetResetForm: React.FC<ErrorBudgetResetFormProps> = ({ serviceLevelObjective, onSubmit, onCancel }) => {
  const { getString } = useStrings()
 
  return (
    <Container width={650}>
      <Text font={{ variation: FontVariation.BODY }} color={Color.BLACK}>
        {getString('cv.yourExistingErrorBudgetIs')}{' '}
        <Text tag="span" font={{ variation: FontVariation.BODY, weight: 'bold' }} data-testid="existing-error-budget">
          {serviceLevelObjective.totalErrorBudget.toLocaleString()} {getString('cv.minutes')}
        </Text>
        , {getString('cv.andYouHave')}{' '}
        <Text
          tag="span"
          font={{ variation: FontVariation.BODY, weight: 'bold' }}
          color={getRiskColorValue(serviceLevelObjective.errorBudgetRisk, false)}
          data-testid="remaining-error-budget"
        >
          {serviceLevelObjective.errorBudgetRemaining.toLocaleString()} {getString('cv.minutes')}
        </Text>{' '}
        {getString('cv.left')}.
      </Text>
      <Text font={{ variation: FontVariation.BODY }} color={Color.BLACK}>
        {getString('cv.adjustTheErrorBudgetToContinueWithDeployments')}
      </Text>
 
      <Accordion>
        <Accordion.Panel
          id="error-budget-reset-history"
          summary={
            <Text font={{ variation: FontVariation.BODY }} color={Color.PRIMARY_7}>
              {getString('cv.previousErrorBudgetResetHistory')}
            </Text>
          }
          details={<ErrorBudgetResetHistory serviceLevelObjectiveIdentifier={serviceLevelObjective.sloIdentifier} />}
        />
      </Accordion>
 
      <Formik<SLOErrorBudgetResetDTO>
        formName="errorBudgetReset"
        initialValues={{}}
        validationSchema={yup.object().shape({
          errorBudgetIncrementPercentage: yup
            .number()
            .typeError(getString('cv.increaseErrorBudgetByIsRequired'))
            .min(1, getString('common.validation.valueMustBeGreaterThanOrEqualToN', { n: 1 }))
            .required(getString('cv.increaseErrorBudgetByIsRequired')),
          reason: yup.string().trim().required(getString('cv.reasonIsRequired'))
        })}
        onSubmit={values => {
          onSubmit({
            ...values,
            errorBudgetAtReset: serviceLevelObjective.totalErrorBudget,
            remainingErrorBudgetAtReset: serviceLevelObjective.errorBudgetRemaining
          })
        }}
      >
        {formik => (
          <FormikForm>
            <Layout.Horizontal>
              <Container width={325} padding={{ right: 'xxxlarge' }} border={{ right: true }}>
                <FormInput.Text
                  name="errorBudgetIncrementPercentage"
                  label={getString('cv.increaseErrorBudgetBy')}
                  inputGroup={{
                    type: 'number',
                    min: 1,
                    rightElement: <Icon name="percentage" padding="small" />
                  }}
                />
                <FormInput.TextArea name="reason" label={getString('reason')} />
              </Container>
              <Layout.Vertical spacing="xxxlarge">
                <div>
                  <Text font={{ variation: FontVariation.FORM_LABEL }}>{getString('cv.updatedErrorBudget')}</Text>
                  <Heading
                    inline
                    level={2}
                    color={Color.GREY_800}
                    font={{ variation: FontVariation.H4 }}
                    data-testid="updated-error-budget"
                  >
                    {calculateErrorBudgetByIncrement(
                      serviceLevelObjective.totalErrorBudget,
                      formik.values.errorBudgetIncrementPercentage
                    )}
                  </Heading>
                  <Text inline font={{ variation: FontVariation.FORM_HELP }}>
                    {' '}
                    {getString('cv.mins')}
                  </Text>
                </div>
                <div>
                  <Text font={{ variation: FontVariation.FORM_LABEL }}>
                    {getString('cv.updatedRemainingErrorBudget')}
                  </Text>
                  <Heading
                    inline
                    level={2}
                    color={Color.GREY_800}
                    font={{ variation: FontVariation.H4 }}
                    data-testid="updated-remaining-error-budget"
                  >
                    {calculateRemainingErrorBudgetByIncrement(
                      serviceLevelObjective.totalErrorBudget,
                      serviceLevelObjective.errorBudgetRemaining,
                      formik.values.errorBudgetIncrementPercentage
                    )}
                  </Heading>
                  <Text inline font={{ variation: FontVariation.FORM_HELP }} padding={{ right: 'small' }}>
                    {' '}
                    {getString('cv.mins')}
                  </Text>
                  <Heading
                    inline
                    level={2}
                    color={Color.GREY_800}
                    font={{ variation: FontVariation.H4 }}
                    data-testid="updated-remaining-error-budget-percentage"
                    border={{ left: true }}
                    padding={{ left: 'small' }}
                  >
                    {calculateRemainingErrorBudgetByIncrement(
                      serviceLevelObjective.totalErrorBudget,
                      serviceLevelObjective.errorBudgetRemaining,
                      formik.values.errorBudgetIncrementPercentage,
                      true
                    )}
                  </Heading>
                  <Text inline font={{ variation: FontVariation.FORM_HELP }}>
                    &nbsp;%
                  </Text>
                </div>
              </Layout.Vertical>
            </Layout.Horizontal>
 
            <Layout.Horizontal spacing="small" margin={{ top: 'xxxlarge' }}>
              <Button type="submit" text={getString('save')} variation={ButtonVariation.PRIMARY} />
              <Button text={getString('cancel')} variation={ButtonVariation.TERTIARY} onClick={onCancel} />
            </Layout.Horizontal>
          </FormikForm>
        )}
      </Formik>
    </Container>
  )
}
 
export default ErrorBudgetResetForm