All files / modules/75-cf/components/SaveFlagRepoDialog SaveFlagRepoDialogForm.tsx

100% Statements 28/28
66.67% Branches 28/42
100% Functions 9/9
100% Lines 25/25

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              3x 3x 3x 3x   3x 3x   3x             3x 15x 15x   15x   15x             30x           29x         15x 2x   1x   15x                         2x       22x                 1x 1x                                     2x 2x                     3x  
/*
 * 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, { ReactElement, useState } from 'react'
import { Container, FormikForm, Formik, Button, FormInput, Layout, SelectOption } from '@wings-software/uicore'
import * as Yup from 'yup'
import { useStrings } from 'framework/strings'
import type { GitRepoRequestRequestBody } from 'services/cf'
import { useGitSyncStore } from 'framework/GitRepoStore/GitSyncStoreContext'
import { DEFAULT_FLAG_GIT_REPO_PATH } from '@cf/constants'
import type { GitSyncConfig, GitSyncFolderConfigDTO } from 'services/cd-ng'
import css from './SaveFlagRepoDialog.module.scss'
 
export interface SaveFlagRepoDialogFormProps {
  onSubmit: (formData: GitRepoRequestRequestBody) => void
  onClose: () => void
}
 
const SaveFlagRepoDialogForm = ({ onSubmit, onClose }: SaveFlagRepoDialogFormProps): ReactElement => {
  const { getString } = useStrings()
  const { gitSyncRepos } = useGitSyncStore()
 
  const [selectedRepoIndex, setSelectedRepoIndex] = useState(0)
 
  const initialFormData = {
    repoIdentifier: gitSyncRepos[selectedRepoIndex]?.identifier || '',
    rootFolder: '',
    branch: gitSyncRepos[selectedRepoIndex]?.branch || '',
    filePath: DEFAULT_FLAG_GIT_REPO_PATH
  }
 
  const repoSelectOptions = gitSyncRepos?.map((gitRepo: GitSyncConfig) => ({
    label: gitRepo?.name || '',
    value: gitRepo?.identifier || ''
  }))
 
  const rootFolderSelectOptions =
    gitSyncRepos[selectedRepoIndex]?.gitSyncFolderConfigDTOs?.map((folder: GitSyncFolderConfigDTO) => ({
      label: folder.rootFolder || '',
      value: folder.rootFolder || ''
    })) || []
 
  const handleRepoOptionChange = (value: string): void => {
    const index = gitSyncRepos.findIndex(repo => repo.identifier === value)
 
    setSelectedRepoIndex(index)
  }
  return (
    <Formik
      enableReinitialize={true}
      initialValues={initialFormData}
      formName="saveFlagRepoConfig"
      validationSchema={Yup.object().shape({
        repoIdentifier: Yup.string().trim().required(getString('common.validation.repositoryName')),
        filePath: Yup.string()
          .trim()
          .required(getString('common.git.validation.filePath'))
          .matches(new RegExp(/^[0-9a-zA-Z/_-]+\.(yaml)+$/g))
      })}
      onSubmit={formData => {
        onSubmit(formData)
      }}
    >
      {formik => {
        return (
          <FormikForm data-testid="save-flag-repo-dialog-form">
            <Container>
              <Layout.Horizontal spacing="medium" className={css.formRow}>
                <FormInput.Select
                  name="repoIdentifier"
                  label={getString('common.git.selectRepoLabel')}
                  items={repoSelectOptions}
                  onChange={(option: SelectOption) => {
                    formik.setFieldValue('repoIdentifier', option.value)
                    handleRepoOptionChange(option.value as string)
                  }}
                />
                <FormInput.Text name="branch" label={getString('common.git.branchName')} disabled />
              </Layout.Horizontal>
              <FormInput.Select
                name="rootFolder"
                label={getString('common.gitSync.harnessFolderLabel')}
                items={rootFolderSelectOptions}
              />
              <FormInput.Text name="filePath" label={getString('common.git.filePath')} disabled />
            </Container>
 
            <Layout.Horizontal padding={{ top: 'medium' }} spacing="medium">
              <Button type="submit" intent="primary" text={getString('save')} />
              <Button
                text={getString('cancel')}
                margin={{ left: 'medium' }}
                onClick={e => {
                  e.preventDefault()
                  onClose()
                }}
              />
            </Layout.Horizontal>
          </FormikForm>
        )
      }}
    </Formik>
  )
}
 
export default SaveFlagRepoDialogForm