All files / modules/75-cf/components/SelectFeatureFlagsModalButton SelectFeatureFlagsModalButton.tsx

84.75% Statements 50/59
71.2% Branches 89/125
64.29% Functions 9/14
84.48% Lines 49/58

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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305              3x 3x 3x 3x                             3x 3x 3x 3x 3x 3x   3x   3x 3x 3x 3x   3x                                                     3x                           11x 5x 5x 5x 5x 5x 5x   5x   5x 3x                                   5x       5x   5x   1x 1x             1x       5x   5x 5x 1x 1x 1x   1x       1x           1x               5x 3x     5x   5x                                                                                                                                       5x                                                                                                                                             11x   11x    
/*
 * 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, { useCallback, useEffect, useMemo, useState } from 'react'
import { Dialog, Intent } from '@blueprintjs/core'
import * as yup from 'yup'
import {
  Button,
  Text,
  ButtonProps,
  Container,
  TextInput,
  Layout,
  FlexExpander,
  Icon,
  Pagination,
  PageError,
  Formik,
  FormikForm,
  Heading
} from '@wings-software/uicore'
import { FontVariation } from '@harness/design-system'
import { useModalHook } from '@harness/use-modal'
import { CF_DEFAULT_PAGE_SIZE, getErrorMessage, SegmentsSortByField, SortOrder } from '@cf/utils/CFUtils'
import { useStrings } from 'framework/strings'
import { Feature, useGetAllFeatures } from 'services/cf'
import { useToaster } from '@common/exports'
 
import { AUTO_COMMIT_MESSAGES } from '@cf/constants/GitSyncConstants'
import type { GitSyncFormValues, UseGitSync } from '@cf/hooks/useGitSync'
import { FeatureFlagRow } from './FeatureFlagRow'
import { NoDataFoundRow } from '../NoDataFoundRow/NoDataFoundRow'
import SaveFlagToGitSubForm from '../SaveFlagToGitSubForm/SaveFlagToGitSubForm'
import AddToFlagButton from './add-to-flag-button/AddToFlagButton'
 
import css from './SelectFeatureFlagsModalButton.module.scss'
 
export interface SelectedFeatureFlag {
  feature: Feature
  variationIdentifier: string
}
 
export interface SelectFeatureFlagsModalButtonProps extends Omit<ButtonProps, 'onClick' | 'onSubmit'> {
  accountIdentifier: string
  orgIdentifier: string
  projectIdentifier: string
  environmentIdentifier: string
  targetIdentifier?: string
 
  modalTitle: string
  submitButtonTitle?: string
  cancelButtonTitle?: string
 
  gitSync: UseGitSync
 
  shouldDisableItem: (feature: Feature) => boolean
  onSubmit: (
    selectedFeatureFlags: SelectedFeatureFlag[],
    gitDetails?: GitSyncFormValues
  ) => Promise<{ error: any } | any>
}
 
export const SelectFeatureFlagsModalButton: React.FC<SelectFeatureFlagsModalButtonProps> = ({
  accountIdentifier,
  orgIdentifier,
  projectIdentifier,
  targetIdentifier,
  environmentIdentifier,
  modalTitle,
  submitButtonTitle,
  cancelButtonTitle,
  gitSync,
  onSubmit,
  shouldDisableItem,
  ...props
}) => {
  const ModalComponent: React.FC = () => {
    const { getString } = useStrings()
    const { showError } = useToaster()
    const [queryString, setQueryString] = useState('')
    const [sortOrder, setSortOrder] = useState(SortOrder.ASCENDING)
    const [sortByField] = useState(SegmentsSortByField.NAME)
    const [pageNumber, setPageNumber] = useState(0)
 
    const gitSyncFormMeta = gitSync?.getGitSyncFormMeta(AUTO_COMMIT_MESSAGES.UPDATED_FLAG_TARGETS)
 
    const queryParams = useMemo(
      () => ({
        accountIdentifier,
        orgIdentifier,
        projectIdentifier,
        environmentIdentifier,
        name: queryString,
        sortOrder,
        sortByField,
        pageNumber,
        pageSize: CF_DEFAULT_PAGE_SIZE
      }),
      [queryString, sortOrder, sortByField, pageNumber]
    )
    const {
      data,
      loading: loadingSegments,
      error,
      refetch
    } = useGetAllFeatures({
      queryParams,
      lazy: true
    })
    const [checkedFeatureFlags, setCheckedFeatureFlags] = useState<Record<string, SelectedFeatureFlag>>({})
 
    const checkOrUncheckSegment = useCallback(
      (checked: boolean, feature: Feature, variationIdentifier: string) => {
        Eif (checked) {
          checkedFeatureFlags[feature.identifier] = {
            feature,
            variationIdentifier
          }
        } else {
          delete checkedFeatureFlags[feature.identifier]
        }
        setCheckedFeatureFlags({ ...checkedFeatureFlags })
      },
      [checkedFeatureFlags, setCheckedFeatureFlags]
    )
    const selectedCounter = Object.keys(checkedFeatureFlags || {}).length
 
    const [submitLoading, setSubmitLoading] = useState(false)
    const handleSubmit = (gitFormValues?: GitSyncFormValues): void => {
      setSubmitLoading(true)
      try {
        onSubmit(Object.values(checkedFeatureFlags), gitFormValues)
          .then(async () => {
            Iif (!gitSync?.isAutoCommitEnabled && gitFormValues?.autoCommit) {
              await gitSync?.handleAutoCommit(gitFormValues.autoCommit)
            }
 
            hideModal()
          })
          .catch(_error => {
            showError(getErrorMessage(_error), 0, 'cf.save.ff.error')
          })
          .finally(() => {
            setSubmitLoading(false)
          })
      } catch (exception) {
        setSubmitLoading(false)
        showError(getErrorMessage(error), 0, 'cf.save.ff.error')
      }
    }
 
    useEffect(() => {
      refetch()
    }, [queryString, sortOrder, sortByField, pageNumber]) // eslint-disable-line react-hooks/exhaustive-deps
 
    const loading = loadingSegments || submitLoading
 
    return (
      <Dialog isOpen enforceFocus={false} onClose={hideModal} title={''} style={{ width: 700, maxHeight: '95vh' }}>
        <Layout.Vertical
          padding={{ left: 'xxlarge' }}
          style={{ height: '100%' }}
          data-testid="add-target-to-flag-modal"
        >
          {/* Search Input */}
          <Heading level={3} font={{ variation: FontVariation.H3 }} margin={{ bottom: 'xlarge' }}>
            {modalTitle}
          </Heading>
          <Container padding={{ right: 'xxlarge', bottom: 'large' }}>
            <TextInput
              leftIcon="search"
              width="100%"
              placeholder={getString('cf.selectFlagsModal.searchPlaceholder')}
              onChange={(e: React.ChangeEvent<HTMLInputElement>) => setQueryString(e.target.value)}
              autoFocus
            />
          </Container>
 
          {/* Table view */}
          <Container className={css.container} margin={{ bottom: 'small', right: 'xxlarge' }}>
            {(error && (
              <PageError
                message={getErrorMessage(error)}
                onClick={() => {
                  refetch()
                }}
              />
            )) || (
              <>
                <Layout.Horizontal
                  style={{ position: 'sticky', top: 0, background: 'var(--white)', zIndex: 1 }}
                  padding={{ top: 'xsmall', left: 'large', right: 'huge', bottom: 'small' }}
                >
                  <Text
                    tabIndex={0}
                    role="button"
                    rightIcon={
                      sortByField === SegmentsSortByField.NAME
                        ? sortOrder === SortOrder.ASCENDING
                          ? 'caret-up'
                          : 'caret-down'
                        : undefined
                    }
                    style={{ color: '#4F5162', fontSize: '10px', cursor: 'pointer', fontWeight: 'bold' }}
                    onClick={() => {
                      setSortOrder(previous =>
                        previous === SortOrder.ASCENDING ? SortOrder.DESCENDING : SortOrder.ASCENDING
                      )
                    }}
                  >
                    {getString('flag').toUpperCase()}
                  </Text>
                  <FlexExpander />
                  <Text style={{ color: '#4F5162', fontSize: '10px', fontWeight: 'bold' }} width={148}>
                    {getString('cf.shared.variation').toLocaleUpperCase()}
                  </Text>
                </Layout.Horizontal>
 
                <Layout.Vertical
                  spacing="small"
                  padding={{ top: 'xsmall', right: 'xsmall', bottom: 'large' }}
                  style={{ paddingLeft: '1px' }}
                >
                  {!!data?.features?.length &&
                    data?.features?.map((feature, index) => (
                      <FeatureFlagRow
                        id={`flag_row_${index}`}
                        key={feature.identifier}
                        feature={feature}
                        checked={false}
                        disabled={shouldDisableItem(feature)}
                        onChecked={checkOrUncheckSegment}
                      />
                    ))}
                  {data?.features?.length === 0 && <NoDataFoundRow message={getString('cf.selectFlagsModal.empty')} />}
                </Layout.Vertical>
              </>
            )}
          </Container>
          <Container margin={{ right: 'xxlarge' }} height={47}>
            {!error && (
              <Pagination
                itemCount={data?.itemCount || 0}
                pageCount={data?.pageCount || 0}
                pageIndex={data?.pageIndex}
                pageSize={CF_DEFAULT_PAGE_SIZE}
                gotoPage={setPageNumber}
                hidePageNumbers={!!data?.pageCount && data?.pageCount > 3}
              />
            )}
          </Container>
 
          <Container margin={{ right: 'xxlarge' }}>
            <Formik
              initialValues={{
                gitDetails: gitSyncFormMeta?.gitSyncInitialValues.gitDetails,
                autoCommit: gitSyncFormMeta?.gitSyncInitialValues.autoCommit
              }}
              formName="editVariations"
              enableReinitialize={true}
              validationSchema={yup.object().shape({
                gitDetails: gitSyncFormMeta?.gitSyncValidationSchema
              })}
              validateOnChange
              validateOnBlur
              onSubmit={handleSubmit}
            >
              <FormikForm>
                {gitSync?.isGitSyncEnabled && !gitSync?.isAutoCommitEnabled && (
                  <SaveFlagToGitSubForm subtitle={getString('cf.gitSync.commitChanges')} hideNameField />
                )}
                <Layout.Horizontal
                  spacing="small"
                  padding={{ right: 'xxlarge', top: 'medium' }}
                  style={{ alignItems: 'center' }}
                >
                  <Button
                    type="submit"
                    text={submitButtonTitle || getString('add')}
                    intent={Intent.PRIMARY}
                    disabled={loading || !!error || !selectedCounter}
                  />
                  <Button text={cancelButtonTitle || getString('cancel')} minimal onClick={hideModal} />
                  <FlexExpander />
 
                  {loading && <Icon intent={Intent.PRIMARY} name="spinner" size={16} />}
                  {!!selectedCounter && <Text>{getString('cf.shared.selected', { counter: selectedCounter })}</Text>}
                </Layout.Horizontal>
              </FormikForm>
            </Formik>
          </Container>
        </Layout.Vertical>
      </Dialog>
    )
  }
 
  const [openModal, hideModal] = useModalHook(ModalComponent, [onSubmit, shouldDisableItem])
 
  return <AddToFlagButton handleClick={openModal} {...props} />
}