import type { FormikErrors, FormikProps } from 'formik'; import React from 'react'; import type { IProject } from '../../domain'; import type { IWizardPageComponent } from '../../modal'; import { FormField, FormikFormField, TextInput } from '../../presentation'; export interface IProjectAttributesProps { allProjects: IProject[]; formik: FormikProps; onDelete?: Function; } interface IProjectAttributesState { showProjectDeleteForm: boolean; projectNameForDeletion: string; } export class ProjectAttributes extends React.Component implements IWizardPageComponent { public state = { showProjectDeleteForm: false, projectNameForDeletion: null as string, }; public validate(values: IProject) { const errors: FormikErrors = {}; const isValidName = (name: string) => { const namePattern = /^[^\\^/?%#]*$/; return name.match(namePattern); }; const isValidEmail = (email: string) => { const emailPattern = /^(.+)@(.+).([A-Za-z]{2,6})/; return email.match(emailPattern); }; const { allProjects } = this.props; const allProjectNames = allProjects.map((project) => project.name.toLowerCase()); if (!values.name) { errors.name = 'Please enter a project name'; } else if (!isValidName(values.name)) { errors.name = 'Project name cannot contain any of the following characters: / % #'; } else if (allProjectNames.includes(values.name.toLowerCase())) { errors.name = 'There is already a project with that name'; } if (!values.email) { errors.email = 'Please enter an email address'; } else if (values.email && !isValidEmail(values.email)) { errors.email = 'Please enter a valid email address'; } return errors; } private DeleteConfirmation = ({ projectName }: { projectName: string }) => { const { onDelete } = this.props; const { projectNameForDeletion } = this.state; const matchError = projectNameForDeletion !== projectName; const handleCancelClicked = () => this.setState({ showProjectDeleteForm: false, projectNameForDeletion: null }); return (

{`Type the name of the project (${projectName}) below to continue.`}

} value={projectNameForDeletion || ''} onChange={(evt: any) => this.setState({ projectNameForDeletion: evt.target.value })} validate={() => matchError && 'Project name does not match'} touched={projectNameForDeletion != null} required={true} />
); }; private DeleteButton = () => { const handleDeleteClicked = () => { this.setState({ showProjectDeleteForm: true }, () => document.getElementById('projectNameForDeletion').focus()); }; return ( ); }; public render() { const { formik } = this.props; const { DeleteConfirmation, DeleteButton } = this; return ( <>
} name="name" label="Project Name" required={true} />
} name="email" label="Owner Email" required={true} />
{this.state.showProjectDeleteForm ? : } ); } }