All files / modules/45-projects-orgs/modals/ProjectModal/views ProjectForm.tsx

100% Statements 24/24
86.96% Branches 20/23
100% Functions 3/3
100% Lines 24/24

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              17x 17x                               17x 17x 17x   17x 17x 17x 17x 17x 17x 17x 17x 17x                                           17x 14x                             14x 14x 14x 14x                                       5x       5x       32x                                                                                                                                       17x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Free Trial 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/05/PolyForm-Free-Trial-1.0.0.txt.
 */
 
import React from 'react'
import {
  Formik,
  FormikForm as Form,
  FormInput,
  Button,
  Text,
  Layout,
  StepProps,
  SelectOption,
  Container,
  ModalErrorHandler,
  ModalErrorHandlerBinding,
  ButtonVariation,
  IconName,
  Heading
} from '@wings-software/uicore'
import * as Yup from 'yup'
import { FontVariation, Color } from '@harness/design-system'
import { DescriptionTags } from '@common/components/NameIdDescriptionTags/NameIdDescriptionTags'
import type { Project } from 'services/cd-ng'
import ProjectCard from '@projects-orgs/components/ProjectCard/ProjectCard'
import { DEFAULT_COLOR } from '@common/constants/Utils'
import { useStrings } from 'framework/strings'
import ProjectsEmptyState from '@projects-orgs/pages/projects/projects-empty-state.png'
import { NameSchema, IdentifierSchema } from '@common/utils/Validation'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { Category, ProjectActions } from '@common/constants/TrackingConstants'
import css from './Steps.module.scss'
 
interface ProjectModalData {
  data?: Project
  disableSelect: boolean
  disableSubmit: boolean
  enableEdit: boolean
  title: string
  saveTitle: string
  saveIcon?: IconName
  initialOrgIdentifier: string
  initialModules?: Project['modules']
  onComplete: (project: Project) => Promise<void>
  organizationItems: SelectOption[]
  setModalErrorHandler: (modalErrorHandler: ModalErrorHandlerBinding) => void
  displayProjectCardPreview?: boolean
}
 
interface AboutPageData extends Project {
  preview?: boolean
}
 
const ProjectForm: React.FC<StepProps<Project> & ProjectModalData> = props => {
  const { trackEvent } = useTelemetry()
  const {
    data: projectData,
    title,
    saveTitle,
    saveIcon,
    enableEdit,
    onComplete,
    disableSelect,
    disableSubmit,
    organizationItems,
    initialOrgIdentifier,
    initialModules,
    setModalErrorHandler,
    displayProjectCardPreview = true
  } = props
  const { getString } = useStrings()
  const { currentUserInfo: user } = useAppStore()
  return (
    <Formik
      initialValues={{
        color: DEFAULT_COLOR,
        identifier: '',
        name: '',
        orgIdentifier: initialOrgIdentifier,
        modules: initialModules,
        description: '',
        tags: {},
        ...projectData
      }}
      formName="projectsForm"
      enableReinitialize={true}
      validationSchema={Yup.object().shape({
        name: NameSchema(),
        identifier: IdentifierSchema(),
        orgIdentifier: Yup.string().required(getString('validation.orgValidation'))
      })}
      onSubmit={(values: AboutPageData) => {
        trackEvent(ProjectActions.SaveCreateProject, {
          category: Category.PROJECT,
          ...values
        })
        onComplete(values)
      }}
    >
      {formikProps => {
        return (
          <Form>
            <Layout.Horizontal>
              <Layout.Vertical width={displayProjectCardPreview ? '50%' : '100%'} padding="xxlarge">
                <Container style={{ minHeight: '450px' }}>
                  <Layout.Horizontal padding={{ bottom: 'large' }}>
                    <Heading level={3} font={{ variation: FontVariation.H3 }}>
                      {title}
                    </Heading>
                  </Layout.Horizontal>
                  <ModalErrorHandler bind={setModalErrorHandler} style={{ padding: '5px 10px' }} />
                  <FormInput.InputWithIdentifier isIdentifierEditable={enableEdit} />
                  <Layout.Horizontal spacing="small" margin={{ bottom: 'xsmall' }}>
                    <FormInput.ColorPicker label={getString('color')} name="color" height={38} />
                    <FormInput.Select
                      label={getString('orgLabel')}
                      name="orgIdentifier"
                      items={organizationItems}
                      disabled={disableSelect}
                    />
                  </Layout.Horizontal>
                  <DescriptionTags formikProps={formikProps} />
                </Container>
                <Layout.Horizontal>
                  <Button
                    variation={ButtonVariation.PRIMARY}
                    text={saveTitle}
                    type="submit"
                    disabled={disableSubmit}
                    rightIcon={saveIcon}
                  />
                </Layout.Horizontal>
              </Layout.Vertical>
              {displayProjectCardPreview && (
                <Container width="50%" flex={{ align: 'center-center' }} className={css.preview}>
                  {Object.keys(formikProps.touched).length ? (
                    <ProjectCard
                      data={{
                        projectResponse: { project: formikProps.values },
                        admins: [
                          { name: user.name, email: user?.email || '', uuid: user?.uuid || '', locked: user?.locked }
                        ]
                      }}
                      isPreview={true}
                    />
                  ) : (
                    <Layout.Vertical width="100%" padding="huge" flex={{ align: 'center-center' }}>
                      <img src={ProjectsEmptyState} className={css.img} />
                      <Layout.Vertical flex={{ alignItems: 'flex-start' }} spacing="medium">
                        <Text color={Color.GREY_800} font={{ weight: 'semi-bold' }}>
                          {getString('projectsOrgs.whyCreateProject')}
                        </Text>
                        <Text color={Color.GREY_800} font="small">
                          {getString('projectsOrgs.createProjectMessage')}
                        </Text>
                      </Layout.Vertical>
                    </Layout.Vertical>
                  )}
                </Container>
              )}
            </Layout.Horizontal>
          </Form>
        )
      }}
    </Formik>
  )
}
 
export default ProjectForm