All files / modules/75-cf/components/CreateFlagDialog FlagDialog.tsx

68.75% Statements 22/32
50% Branches 1/2
25% Functions 2/8
68.75% Lines 22/32

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              4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x             4x 6x 6x 6x   6x         6x         6x       6x   1x                                                                                                                                       6x     4x  
/*
 * 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 { Dialog } from '@blueprintjs/core'
import { Button, Container, Text, Icon } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { FlagTypeVariations } from './FlagDialogUtils'
import FlagWizard from '../CreateFlagWizard/FlagWizard'
import FlagTypeElement from '../CreateFlagType/FlagTypeElement'
import CreateFlagButton from '../CreateFlagButton/CreateFlagButton'
import css from './FlagDialog.module.scss'
 
export interface FlagModalProps {
  disabled?: boolean
  environment: string
}
 
const FlagModal: React.FC<FlagModalProps> = ({ disabled, environment }) => {
  const { getString } = useStrings()
  const [flagTypeClicked, setFlagTypeClicked] = useState(false)
  const [flagTypeView, setFlagTypeView] = useState('')
 
  const booleanFlagBtn = (typeOfFlag: boolean): void => {
    setFlagTypeClicked(typeOfFlag)
    setFlagTypeView(FlagTypeVariations.booleanFlag)
  }
 
  const multiFlagBtn = (typeOfFlag: boolean): void => {
    setFlagTypeClicked(typeOfFlag)
    setFlagTypeView(FlagTypeVariations.multiFlag)
  }
 
  const toggleFlagType = (newFlagType: string): void => {
    setFlagTypeView(newFlagType)
  }
 
  const [showModal, hideModal] = useModalHook(
    () => (
      <Dialog
        isOpen={true}
        enforceFocus={false}
        onClose={() => {
          setFlagTypeClicked(false)
          hideModal()
        }}
        className={css.modal}
      >
        {flagTypeClicked ? (
          <FlagWizard
            flagTypeView={flagTypeView}
            environmentIdentifier={environment}
            toggleFlagType={toggleFlagType}
            hideModal={hideModal}
            goBackToTypeSelections={() => {
              setFlagTypeClicked(false)
            }}
          />
        ) : (
          <Container className={css.typeFlagContainer} padding="huge">
            <Text color={Color.WHITE} margin={{ bottom: 'small' }} style={{ fontSize: '24px' }}>
              {getString('cf.featureFlags.typeOfFlag')}
            </Text>
            <Text font="small" color={Color.WHITE} margin={{ bottom: 'xxxlarge' }}>
              {getString('cf.featureFlags.startVariation')}
            </Text>
            <Container className={css.typeFlagBtns}>
              <FlagTypeElement
                type={FlagTypeVariations.booleanFlag}
                text={getString('cf.boolean')}
                textDesc={getString('cf.featureFlags.booleanBtnText')}
                typeOfFlagFnc={booleanFlagBtn}
              >
                <Icon name="full-circle" color={Color.BLUE_800} />
                <Icon name="full-circle" color={Color.BLUE_500} className={css.iconMl} />
              </FlagTypeElement>
 
              <FlagTypeElement
                type={FlagTypeVariations.multiFlag}
                text={getString('cf.multivariate')}
                textDesc={getString('cf.featureFlags.multiBtnText')}
                typeOfFlagFnc={multiFlagBtn}
              >
                <Icon name="full-circle" color={Color.BLUE_800} />
                <Icon name="full-circle" color={Color.BLUE_500} className={css.iconMl} />
                <Icon name="full-circle" color={Color.YELLOW_700} className={css.iconMl} />
                <Icon name="small-plus" color={Color.GREY_600} className={css.iconMl} />
              </FlagTypeElement>
            </Container>
          </Container>
        )}
 
        <Button
          minimal
          icon="small-cross"
          iconProps={{ size: 25 }}
          onClick={() => {
            setFlagTypeClicked(false)
            hideModal()
          }}
          className={css.closeIcon}
        />
      </Dialog>
    ),
    [flagTypeClicked, flagTypeView]
  )
 
  return <CreateFlagButton disabled={disabled} showModal={showModal} />
}
 
export default FlagModal