All files / modules/75-cf/components/CreateFlagWizard FlagElemBoolean.tsx

100% Statements 39/39
83.33% Branches 20/24
100% Functions 9/9
100% Lines 39/39

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 243 244 245 246 247 248              6x 6x 6x                         6x 6x 6x 6x   6x                     6x 6x 6x 6x   6x                   35x 34x   34x 1x     34x 34x   34x 12x     34x 13x     34x 34x   34x   34x 1x     34x   34x 62x 12x     62x 13x       34x                                       2x       69x                                             1x                                                                                                                                                                                                                     6x  
/*
 * 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, { Dispatch, SetStateAction, useState } from 'react'
import * as yup from 'yup'
import {
  Formik,
  FormikForm,
  FormInput,
  StepProps,
  Text,
  Layout,
  SelectOption,
  Container,
  Button,
  ModalErrorHandler,
  FlexExpander
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { FormikEffect, FormikEffectProps } from '@common/components/FormikEffect/FormikEffect'
import { FlagTypeVariations } from '../CreateFlagDialog/FlagDialogUtils'
import type { FlagWizardFormValues } from './FlagWizard'
import css from './FlagElemVariations.module.scss'
 
export interface FlagElemBooleanProps extends StepProps<Partial<FlagWizardFormValues>> {
  toggleFlagType: (newFlag: string) => void
  flagTypeOptions: SelectOption[]
  projectIdentifier?: string | number | null | undefined
  // FIXME: Check for the right type
  setModalErrorHandler: Dispatch<SetStateAction<any>>
  isLoadingCreateFeatureFlag: boolean
}
 
const TRUE = 'true'
const FALSE = 'false'
const True = 'True'
const False = 'False'
 
const FlagElemBoolean = (props: FlagElemBooleanProps): JSX.Element => {
  const {
    toggleFlagType,
    flagTypeOptions,
    prevStepData,
    previousStep,
    setModalErrorHandler,
    isLoadingCreateFeatureFlag,
    currentStep,
    totalSteps
  } = props
  const { getString } = useStrings()
 
  const handleNewFlagType = (newFlagTypeVal: string): void => {
    toggleFlagType(newFlagTypeVal)
  }
 
  const [trueFlagOption, setTrueFlagOption] = useState(True)
  const [falseFlagOption, setFalseFlagOption] = useState(False)
 
  const onTrueFlagChange = (valInput: string): void => {
    setTrueFlagOption(valInput)
  }
 
  const onFalseFlagChange = (valInput: string): void => {
    setFalseFlagOption(valInput)
  }
 
  const selectValueTrue = { label: trueFlagOption, value: TRUE }
  const selectValueFalse = { label: falseFlagOption, value: FALSE }
 
  const flagBooleanRules = [selectValueTrue, selectValueFalse]
 
  const onClickBack = (): void => {
    previousStep?.({ ...prevStepData })
  }
 
  const isLastStep = currentStep?.() === totalSteps?.()
 
  const onFormikEffect: FormikEffectProps['onChange'] = ({ prevValues, nextValues }) => {
    if (prevValues.variations[0].name !== nextValues.variations[0].name) {
      onTrueFlagChange(nextValues.variations[0].name)
    }
 
    if (prevValues.variations[1].name !== nextValues.variations[1].name) {
      onFalseFlagChange(nextValues.variations[1].name)
    }
  }
 
  return (
    <Formik
      initialValues={{
        kind: FlagTypeVariations.booleanFlag,
        variations: [
          { identifier: TRUE, name: True, value: TRUE },
          { identifier: FALSE, name: False, value: FALSE }
        ],
        defaultOnVariation: TRUE,
        defaultOffVariation: FALSE
      }}
      formName="cfFlagBool"
      validationSchema={yup.object().shape({
        variations: yup.array().of(
          yup.object().shape({
            name: yup.string().trim().required(getString('cf.creationModal.nameIsRequired'))
          })
        )
      })}
      onSubmit={formData => {
        props.nextStep?.({ ...prevStepData, ...formData, kind: FlagTypeVariations.booleanFlag })
      }}
    >
      {formik => (
        <FormikForm data-testid="boolean-step-form">
          <FormikEffect onChange={onFormikEffect} formik={formik} />
          <Container
            flex
            height="100%"
            style={{ flexDirection: 'column', alignItems: 'baseline' }}
            className={css.booleanForm}
          >
            <Container style={{ flexGrow: 1, overflow: 'auto' }} width="100%">
              <ModalErrorHandler bind={setModalErrorHandler} />
              <Text
                style={{ fontSize: '18px', color: Color.GREY_700 }}
                margin={{ bottom: 'xlarge' }}
                padding={{ left: 'xsmall' }}
              >
                {getString('cf.creationModal.variationSettingsHeading')}
              </Text>
              <Layout.Vertical padding={{ left: 'xsmall' }}>
                <FormInput.Select
                  data-testid="kind-dropdown"
                  name="kind"
                  label={getString('cf.creationModal.flagType')}
                  items={flagTypeOptions}
                  onChange={newFlagType => handleNewFlagType(newFlagType.value as string)}
                  className={css.inputSelectFlagType}
                />
 
                <Layout.Horizontal
                  style={{
                    background: '#FAFBFC',
                    boxShadow: '0px 0px 1px rgba(40, 41, 61, 0.04), 0px 2px 4px rgba(96, 97, 112, 0.16)',
                    borderRadius: '4px',
                    width: '570px',
                    marginBottom: 'var(--spacing-small)',
                    padding: 'var(--spacing-small) var(--spacing-small) 0 var(--spacing-medium)'
                  }}
                >
                  <Container width={255}>
                    <FormInput.InputWithIdentifier
                      inputName="variations[0].name"
                      idName="variations[0].identifier"
                      inputLabel={getString('name')}
                      isIdentifierEditable={false}
                      inputGroupProps={{ inputGroup: { autoFocus: true } }}
                    />
                  </Container>
                  <Container width={20} />
                  <Container width={255}>
                    <FormInput.Text
                      name="variations[0].value"
                      label={getString('valueLabel')}
                      disabled
                      className={css.disabledInput}
                    />
                  </Container>
                </Layout.Horizontal>
 
                <Layout.Horizontal
                  style={{
                    background: '#FAFBFC',
                    boxShadow: '0px 0px 1px rgba(40, 41, 61, 0.04), 0px 2px 4px rgba(96, 97, 112, 0.16)',
                    borderRadius: '4px',
                    width: '570px',
                    padding: 'var(--spacing-small) var(--spacing-small) 0 var(--spacing-medium)'
                  }}
                >
                  <Container width={255}>
                    <FormInput.InputWithIdentifier
                      inputName="variations[1].name"
                      idName="variations[1].identifier"
                      inputLabel={getString('name')}
                      isIdentifierEditable={false}
                    />
                  </Container>
                  <Container width={20} />
                  <Container width={255}>
                    <FormInput.Text
                      name="variations[1].value"
                      label={getString('valueLabel')}
                      disabled
                      className={css.disabledInput}
                    />
                  </Container>
                </Layout.Horizontal>
                <Container margin={{ bottom: 'xlarge' }}>
                  <Text color={Color.BLACK} margin={{ top: 'medium' }}>
                    {getString('cf.creationModal.defaultRules')}
                  </Text>
 
                  <Layout.Vertical margin={{ top: 'medium' }}>
                    <Container>
                      <Layout.Horizontal>
                        <Text width="25%" className={css.serveTextAlign}>
                          {getString('cf.creationModal.flagOn')}
                        </Text>
                        <FormInput.Select name="defaultOnVariation" items={flagBooleanRules} />
                      </Layout.Horizontal>
                    </Container>
                    <Container>
                      <Layout.Horizontal>
                        <Text width="25%" className={css.serveTextAlign}>
                          {getString('cf.creationModal.flagOff')}
                        </Text>
                        <FormInput.Select name="defaultOffVariation" items={flagBooleanRules} />
                      </Layout.Horizontal>
                    </Container>
                  </Layout.Vertical>
                </Container>
              </Layout.Vertical>
            </Container>
 
            <Layout.Horizontal spacing="small" margin={{ top: 'large' }} width="100%">
              <Button text={getString('back')} onClick={onClickBack} />
              <Button
                type="submit"
                intent="primary"
                rightIcon={isLastStep ? undefined : 'chevron-right'}
                text={isLastStep ? getString('cf.creationModal.saveAndClose') : getString('next')}
                disabled={isLoadingCreateFeatureFlag}
                loading={isLoadingCreateFeatureFlag}
              />
              <FlexExpander />
            </Layout.Horizontal>
          </Container>
        </FormikForm>
      )}
    </Formik>
  )
}
 
export default FlagElemBoolean