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

94.74% Statements 18/19
77.27% Branches 17/22
100% Functions 3/3
94.74% Lines 18/19

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              6x 6x 6x 6x 6x     6x   6x           6x           3x   3x   3x   3x       3x             3x               1x                 1x 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, { ReactElement } from 'react'
import { FormikForm, Formik, Layout, Button } from '@wings-software/uicore'
import * as Yup from 'yup'
import { useStrings } from 'framework/strings'
import { PageSpinner } from '@common/components'
import type { StepProps } from '@common/components/WizardWithProgress/WizardWithProgress'
 
import { useGitSync } from '@cf/hooks/useGitSync'
import type { FlagWizardFormValues } from './FlagWizard'
import SaveFlagToGitSubForm from '../SaveFlagToGitSubForm/SaveFlagToGitSubForm'
 
export interface SaveFlagRepoStepProps extends StepProps<Partial<FlagWizardFormValues>> {
  isLoadingCreateFeatureFlag: boolean
}
 
const SaveFlagRepoStep = ({
  nextStep,
  previousStep,
  prevStepData,
  isLoadingCreateFeatureFlag
}: SaveFlagRepoStepProps): ReactElement => {
  const { getString } = useStrings()
 
  const { getGitSyncFormMeta, gitSyncLoading } = useGitSync()
 
  const { gitSyncValidationSchema, gitSyncInitialValues } = getGitSyncFormMeta()
 
  Iif (gitSyncLoading) {
    return <PageSpinner />
  }
 
  const initialFormData = {
    flagName: prevStepData?.name || '',
    flagIdentifier: prevStepData?.identifier || '',
    gitDetails: gitSyncInitialValues.gitDetails,
    autoCommit: false
  }
 
  return (
    <Formik
      enableReinitialize={true}
      initialValues={initialFormData}
      formName="saveFlagRepoStep"
      validationSchema={Yup.object().shape({
        gitDetails: gitSyncValidationSchema
      })}
      onSubmit={formValues => nextStep?.({ ...prevStepData, ...formValues })}
    >
      <FormikForm data-testid="save-flag-to-git-form">
        <SaveFlagToGitSubForm title={getString('cf.selectFlagRepo.dialogTitle')} />
 
        <Layout.Horizontal spacing="small" margin={{ top: 'large' }}>
          <Button
            text={getString('back')}
            onClick={event => {
              event.preventDefault()
              previousStep?.(prevStepData)
            }}
          />
          <Button
            type="submit"
            intent="primary"
            text={getString('cf.creationModal.saveAndClose')}
            disabled={isLoadingCreateFeatureFlag}
            loading={isLoadingCreateFeatureFlag}
          />
        </Layout.Horizontal>
      </FormikForm>
    </Formik>
  )
}
 
export default SaveFlagRepoStep