Skip to content

Commit

Permalink
fix: use the same validation logic on AuthorFormGroup.tsx as is used …
Browse files Browse the repository at this point in the history
…in other input fields
  • Loading branch information
ekraffmiller committed Apr 11, 2024
1 parent 1bcd0bb commit aa5d959
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
13 changes: 11 additions & 2 deletions src/dataset/domain/useCases/validateDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ export function validateDataset(dataset: DatasetDTO) {
for (let i = 0; i < dataset.metadataBlocks[0].fields.author.length; i++) {
if (!dataset.metadataBlocks[0].fields.author[i].authorName) {
if (isArrayOfSubfieldValue(errors.metadataBlocks[0].fields.author)) {
errors.metadataBlocks[0].fields.author[i].authorName = AUTHOR_NAME_REQUIRED
isValid = false
// Check if the errors array has enough elements
if (i < errors.metadataBlocks[0].fields.author.length) {
errors.metadataBlocks[0].fields.author[i].authorName = AUTHOR_NAME_REQUIRED
console.log('invalid author name')
isValid = false
} else {
// If the errors array does not have enough elements, add a new one
errors.metadataBlocks[0].fields.author.push({ authorName: AUTHOR_NAME_REQUIRED })
console.log('invalid author name')
isValid = false
}
}
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/sections/create-dataset/AuthorFormGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,35 @@ import { DynamicFieldsButtons } from './dynamic-fields-buttons/DynamicFieldsButt
import { ChangeEvent } from 'react'
import _ from 'lodash'
import { useMultipleFields } from './useMultipleFields'
import { DatasetDTO } from '../../dataset/domain/useCases/DTOs/DatasetDTO'
interface AuthorFormGroupProps {
submissionStatus: SubmissionStatus
initialAuthorFields: DatasetMetadataSubField[]
updateFormData: (name: string, value: string | DatasetMetadataSubField[]) => void
validationErrors: DatasetDTO
}

export function AuthorFormGroup({
submissionStatus,
initialAuthorFields,
updateFormData
updateFormData,
validationErrors
}: AuthorFormGroupProps) {
const { t } = useTranslation('createDataset')
const { multipleFields, setMultipleFields, addField, removeField } =
useMultipleFields(initialAuthorFields)

const isAuthorValid = (index: number) => {
return !!multipleFields[index].authorName
const isAuthorInvalid = (index: number) => {
const subfieldArray = validationErrors.metadataBlocks[0].fields
.author as DatasetMetadataSubField[]
const retValue = subfieldArray[index] ? !!subfieldArray[index].authorName : false
return retValue
}
const handleFieldChange = (index: number, event: ChangeEvent<HTMLInputElement>) => {
const updatedAuthorFields = _.cloneDeep(multipleFields)
updatedAuthorFields[index].authorName = (event.target as HTMLInputElement).value
updatedAuthorFields[index].authorName = event.target.value
setMultipleFields(updatedAuthorFields)
updateFormData('metadataBlocks.0.fields.author', multipleFields)
updateFormData('metadataBlocks.0.fields.author', updatedAuthorFields)
}
const FIRST_AUTHOR = 0
const initialAuthorFieldState = { authorName: '' }
Expand Down Expand Up @@ -60,7 +66,7 @@ export function AuthorFormGroup({
onChange={(event: ChangeEvent<HTMLInputElement>) =>
handleFieldChange(index, event)
}
isInvalid={!isAuthorValid(index)}
isInvalid={isAuthorInvalid(index)}
value={author.authorName}
required
/>
Expand Down
3 changes: 2 additions & 1 deletion src/sections/create-dataset/CreateDatasetForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export function CreateDatasetForm({ repository }: CreateDatasetFormProps) {
updateFormData={updateFormData}
initialAuthorFields={
formData.metadataBlocks[0].fields['author'] as DatasetMetadataSubField[]
}></AuthorFormGroup>
}
validationErrors={validationErrors}></AuthorFormGroup>
<Form.Group controlId="contact-email" required>
<Form.Group.Label message={t('datasetForm.fields.datasetContactEmail.tooltip')}>
{t('datasetForm.fields.datasetContactEmail.label')}
Expand Down
2 changes: 2 additions & 0 deletions src/stories/create-dataset/AuthorFormGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AuthorFormGroup } from '../../sections/create-dataset/AuthorFormGroup'
import { SubmissionStatus } from '../../sections/create-dataset/useCreateDatasetForm'
import { initialState } from '../../sections/create-dataset/useDatasetValidator'
import { DatasetMetadataSubField } from '../../dataset/domain/models/Dataset'
import { initialDatasetDTO } from '../../../src/dataset/domain/useCases/DTOs/DatasetDTO'

const meta: Meta<typeof AuthorFormGroup> = {
title: 'Sections/Create Dataset Page/AuthorFormGroup',
Expand All @@ -22,6 +23,7 @@ export const Default: Story = {
initialAuthorFields={
initialState.metadataBlocks[0].fields['author'] as DatasetMetadataSubField[]
}
validationErrors={initialDatasetDTO}
/>
)
}
4 changes: 3 additions & 1 deletion tests/component/create-dataset/AuthorFormGroup.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AuthorFormGroup } from '../../../src/sections/create-dataset/AuthorFormGroup'
import { SubmissionStatus } from '../../../src/sections/create-dataset/useCreateDatasetForm'
import { initialDatasetDTO } from '../../../src/dataset/domain/useCases/DTOs/DatasetDTO'
describe('AuthorFormGroup', () => {
const initialAuthorFields = [{ authorName: '' }]

const datasetDTO = initialDatasetDTO
it('renders and allows adding and removing author fields', () => {
const updateFormDataMock = cy.stub()

Expand All @@ -11,6 +12,7 @@ describe('AuthorFormGroup', () => {
submissionStatus={SubmissionStatus.NotSubmitted}
initialAuthorFields={initialAuthorFields}
updateFormData={updateFormDataMock}
validationErrors={datasetDTO}
/>
)
cy.findByLabelText(/Name/i).should('exist')
Expand Down

0 comments on commit aa5d959

Please sign in to comment.