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

91.3% Statements 21/23
83.33% Branches 30/36
83.33% Functions 5/6
91.3% Lines 21/23

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              5x 5x                         5x   5x 5x 5x   5x   5x                         5x 15x 14x   14x                                                                                                                     1x               5x 2x 1x 1x   1x                                       1x     14x           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, { useState } from 'react'
import {
  Formik,
  FormikForm as Form,
  FormInput,
  Layout,
  Container,
  StepProps,
  Button,
  Collapse,
  Text,
  Utils
} from '@wings-software/uicore'
import type { FormikProps } from 'formik'
import { Color } from '@harness/design-system'
import type { IconName } from '@blueprintjs/core'
import * as Yup from 'yup'
import { useStrings } from 'framework/strings'
import { illegalIdentifiers } from '@common/utils/StringUtils'
import type { FlagWizardFormValues } from './FlagWizard'
import css from './FlagElemAbout.module.scss'
 
const collapseProps = {
  collapsedIcon: 'plus' as IconName,
  expandedIcon: 'minus' as IconName,
  isOpen: false,
  isRemovable: false
}
 
interface FlagElemAboutProps {
  goBackToTypeSelections: () => void
}
 
type AboutFormProps = FormikProps<any> & FlagElemAboutProps & { isEdit: boolean }
 
const AboutForm: React.FC<AboutFormProps> = props => {
  const { getString } = useStrings()
  const [descOpened, setDescOpened] = useState<boolean>(Boolean(props.values.description.length))
 
  return (
    <Form>
      <Container className={css.aboutFlagContainer}>
        <Container style={{ flexGrow: 1, overflow: 'auto' }}>
          <Text style={{ fontSize: '18px', color: Color.GREY_700 }} margin={{ bottom: 'xlarge' }}>
            {getString('cf.creationModal.aboutFlag.aboutFlagHeading')}
          </Text>
          <Container margin={{ bottom: 'large' }} width="60%">
            <FormInput.InputWithIdentifier
              inputName="name"
              idName="identifier"
              isIdentifierEditable={true}
              inputGroupProps={{
                placeholder: getString('cf.creationModal.aboutFlag.ffNamePlaceholder'),
                inputGroup: { autoFocus: true }
              }}
            />
          </Container>
          <Container>
            <div className={css.optionalCollapse}>
              <Collapse
                {...collapseProps}
                onToggleOpen={setDescOpened}
                heading={getString('description')}
                isOpen={Boolean(props.values.description.length) || descOpened}
              >
                <FormInput.TextArea name="description" />
              </Collapse>
            </div>
          </Container>
          <Container margin={{ top: 'xlarge' }}>
            <Layout.Horizontal>
              <FormInput.CheckBox name="permanent" label={getString('cf.creationModal.aboutFlag.permaFlag')} />
              <Text
                margin={{ left: 'xsmall' }}
                tooltip={getString('cf.creationModal.aboutFlag.permaFlagTooltip')}
                tooltipProps={{
                  isDark: true,
                  portalClassName: css.tooltipAboutFlag
                }}
                inline
              />
            </Layout.Horizontal>
          </Container>
        </Container>
        <Layout.Horizontal spacing="small" margin={{ top: 'large' }}>
          <Button
            type="button"
            text={getString('back')}
            onMouseDown={e => {
              Utils.stopEvent(e)
              props.goBackToTypeSelections()
            }}
          />
          <Button
            type="submit"
            intent="primary"
            rightIcon="chevron-right"
            text={getString('next')}
            onClick={() => props.handleSubmit()}
          />
        </Layout.Horizontal>
      </Container>
    </Form>
  )
}
 
const FlagElemAbout: React.FC<StepProps<Partial<FlagWizardFormValues>> & FlagElemAboutProps> = props => {
  const { getString } = useStrings()
  const { nextStep, prevStepData, goBackToTypeSelections } = props
  const isEdit = Boolean(prevStepData)
 
  return (
    <>
      <Formik
        initialValues={{
          name: prevStepData?.name || '',
          identifier: prevStepData?.identifier || '',
          description: prevStepData?.description || '',
          tags: prevStepData?.tags || [],
          permanent: prevStepData?.permanent || false
        }}
        formName="cfFlagElem"
        validationSchema={Yup.object().shape({
          name: Yup.string().trim().required(getString('cf.creationModal.aboutFlag.nameRequired')),
          identifier: Yup.string()
            .trim()
            .required(getString('cf.creationModal.aboutFlag.idRequired'))
            .matches(/^(?![0-9])[0-9a-zA-Z_$]*$/, getString('cf.creationModal.aboutFlag.ffRegex'))
            .notOneOf(illegalIdentifiers)
        })}
        onSubmit={vals => {
          nextStep?.({ ...prevStepData, ...vals })
        }}
      >
        {formikProps => <AboutForm {...formikProps} isEdit={isEdit} goBackToTypeSelections={goBackToTypeSelections} />}
      </Formik>
    </>
  )
}
 
export default FlagElemAbout