Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify DTO sent to CreateCollection and UpdateCollection use cases #596

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@dnd-kit/sortable": "8.0.0",
"@dnd-kit/utilities": "3.2.2",
"@faker-js/faker": "7.6.0",
"@iqss/dataverse-client-javascript": "2.0.0-alpha.16",
"@iqss/dataverse-client-javascript": "2.0.0-alpha.19",
"@iqss/dataverse-design-system": "*",
"@istanbuljs/nyc-config-typescript": "1.0.2",
"@tanstack/react-table": "8.9.2",
Expand Down
10 changes: 8 additions & 2 deletions public/locales/en/shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,17 @@
"name": {
"label": "Collection Name",
"description": "The project, department, university, professor, or journal this collection will contain data for.",
"required": "Collection Name is required"
"required": "Collection Name is required",
"invalid": {
"maxLength": "Collection Name must be at most {{maxLength}} characters."
}
},
"affiliation": {
"label": "Affiliation",
"description": "The organization with which this collection is affiliated."
"description": "The organization with which this collection is affiliated.",
"invalid": {
"maxLength": "Affiliation must be at most {{maxLength}} characters."
}
},
"alias": {
"label": "Identifier",
Expand Down
2 changes: 2 additions & 0 deletions src/collection/domain/useCases/DTOs/CollectionDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface CollectionDTO {
metadataBlockNames?: string[]
facetIds?: string[]
inputLevels?: CollectionInputLevelDTO[]
inheritMetadataBlocksFromParent: boolean
inheritFacetsFromParent: boolean
}

export interface CollectionInputLevelDTO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,54 +246,4 @@ export class CollectionFormHelper {
return true
}
}

public static defineShouldSendMetadataBlockNamesAndInputLevels(
useFieldsFromParentChecked: boolean,
isEditingRootCollection: boolean,
blockNamesHaveChanged: boolean,
inputLevelsHaveChanged: boolean,
mode: 'edit' | 'create'
): boolean {
if (mode === 'edit') {
if (isEditingRootCollection) {
if (blockNamesHaveChanged || inputLevelsHaveChanged) return true

return false
} else {
if (useFieldsFromParentChecked) return false

// If useFieldsFromParentChecked is not checked we should send the default metadata block names and input levels, the only way for the user to edit them is to uncheck the useFromParent first
return true
}
} else {
if (useFieldsFromParentChecked) return false

// If useFieldsFromParentChecked is not checked we should send the default metadata block names and input levels, the only way for the user to edit them is to uncheck the useFromParent first
return true
}
}

public static defineShouldSendFacetIds(
useFacetsFromParentChecked: boolean,
isEditingRootCollection: boolean,
facetIdsHaveChanged: boolean,
mode: 'edit' | 'create'
): boolean {
if (mode === 'edit') {
if (isEditingRootCollection) {
if (facetIdsHaveChanged) return true
return false
} else {
if (useFacetsFromParentChecked) return false

// If useFacetsFromParentChecked is not checked we should send the default facetIds, changed or not
return true
}
} else {
if (useFacetsFromParentChecked) return false

// If useFacetsFromParentChecked is not checked we should send the default facetIds, changed or not
return true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {
MetadataField
} from '@/metadata-block-info/domain/models/MetadataBlockInfo'
import { SubmissionStatus, useSubmitCollection } from './useSubmitCollection'
import styles from './CollectionForm.module.scss'
import { TopFieldsSection } from './top-fields-section/TopFieldsSection'
import { SeparationLine } from '@/sections/shared/layout/SeparationLine/SeparationLine'
import { MetadataFieldsSection } from './metadata-fields-section/MetadataFieldsSection'
import { BrowseSearchFacetsSection } from './browse-search-facets-section/BrowseSearchFacetsSection'
import { EditCreateCollectionFormMode } from '../EditCreateCollectionForm'
import { RouteWithParams } from '@/sections/Route.enum'
import styles from './CollectionForm.module.scss'

export interface CollectionFormProps {
mode: EditCreateCollectionFormMode
Expand Down Expand Up @@ -49,14 +49,10 @@ export const CollectionForm = ({
defaultValues
})

const { formState } = form

const { submitForm, submitError, submissionStatus } = useSubmitCollection(
mode,
collectionIdOrParentCollectionId,
collectionRepository,
isEditingRootCollection,
formState.dirtyFields,
onSubmittedCollectionError
)

Expand All @@ -71,8 +67,8 @@ export const CollectionForm = ({
}

const disableSubmitButton = useMemo(() => {
return submissionStatus === SubmissionStatus.IsSubmitting || !formState.isDirty
}, [submissionStatus, formState.isDirty])
return submissionStatus === SubmissionStatus.IsSubmitting || !form.formState.isDirty
}, [submissionStatus, form.formState.isDirty])

return (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ export const TopFieldsSection = ({ isEditingRootCollection }: TopFieldsSectionPr
}

const nameRules: UseControllerProps['rules'] = {
required: t('fields.name.required')
required: t('fields.name.required'),
maxLength: {
value: 255,
message: t('fields.name.invalid.maxLength', { maxLength: 255 })
}
}

const affiliationRules: UseControllerProps['rules'] = {
maxLength: {
value: 255,
message: t('fields.affiliation.invalid.maxLength', { maxLength: 255 })
}
}

const aliasRules: UseControllerProps['rules'] = {
Expand Down Expand Up @@ -119,6 +130,7 @@ export const TopFieldsSection = ({ isEditingRootCollection }: TopFieldsSectionPr
<Controller
name="affiliation"
control={control}
rules={affiliationRules}
render={({ field: { onChange, ref, value }, fieldState: { invalid, error } }) => (
<Col>
<Form.Group.Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { CollectionFormData, CollectionFormValuesOnSubmit } from '../types'
import { CollectionRepository } from '@/collection/domain/repositories/CollectionRepository'
import {
EditCreateCollectionFormMode,
FACET_IDS_FIELD,
INPUT_LEVELS_GROUPER,
METADATA_BLOCKS_NAMES_GROUPER,
USE_FACETS_FROM_PARENT,
Expand All @@ -17,7 +16,6 @@ import { editCollection } from '@/collection/domain/useCases/editCollection'
import { RouteWithParams } from '@/sections/Route.enum'
import { JSDataverseWriteErrorHandler } from '@/shared/helpers/JSDataverseWriteErrorHandler'
import { CollectionFormHelper } from '../CollectionFormHelper'
import { FormState } from 'react-hook-form'

export enum SubmissionStatus {
NotSubmitted = 'NotSubmitted',
Expand Down Expand Up @@ -45,8 +43,6 @@ export function useSubmitCollection(
mode: EditCreateCollectionFormMode,
collectionIdOrParentCollectionId: string,
collectionRepository: CollectionRepository,
isEditingRootCollection: boolean,
formDirtyFields: FormState<CollectionFormData>['dirtyFields'],
onSubmitErrorCallback: () => void
): UseSubmitCollectionReturnType {
const navigate = useNavigate()
Expand All @@ -57,7 +53,7 @@ export function useSubmitCollection(
const [submitError, setSubmitError] = useState<string | null>(null)

const submitForm = (formData: CollectionFormValuesOnSubmit): void => {
// setSubmissionStatus(SubmissionStatus.IsSubmitting)
setSubmissionStatus(SubmissionStatus.IsSubmitting)

const contactsDTO = formData.contacts.map((contact) => contact.value)

Expand All @@ -73,46 +69,22 @@ export function useSubmitCollection(

const facetIdsDTO = formData.facetIds.map((facet) => facet.value)

const useFieldsFromParentChecked = formData[USE_FIELDS_FROM_PARENT]
const useMetadataFieldsFromParentChecked = formData[USE_FIELDS_FROM_PARENT]

const useFacetsFromParentChecked = formData[USE_FACETS_FROM_PARENT]

const hasMetadataBlockNamesChangedFromDefaultValue =
formDirtyFields[METADATA_BLOCKS_NAMES_GROUPER] !== undefined

const hasInputLevelsChangedFromDefaultValue =
formDirtyFields[INPUT_LEVELS_GROUPER] !== undefined

const hasFacetIdsChangedFromDefaultValue = formDirtyFields[FACET_IDS_FIELD] !== undefined

const shouldSendMetadataBlockNamesAndInputLevels =
CollectionFormHelper.defineShouldSendMetadataBlockNamesAndInputLevels(
useFieldsFromParentChecked,
isEditingRootCollection,
hasMetadataBlockNamesChangedFromDefaultValue,
hasInputLevelsChangedFromDefaultValue,
mode
)

const shouldSendFacetIds = CollectionFormHelper.defineShouldSendFacetIds(
useFacetsFromParentChecked,
isEditingRootCollection,
hasFacetIdsChangedFromDefaultValue,
mode
)

const newOrUpdatedCollection: CollectionDTO = {
name: formData.name,
alias: formData.alias,
type: formData.type,
affiliation: formData.affiliation,
description: formData.description,
contacts: contactsDTO,
metadataBlockNames: shouldSendMetadataBlockNamesAndInputLevels
? metadataBlockNamesDTO
: undefined,
inputLevels: shouldSendMetadataBlockNamesAndInputLevels ? inputLevelsDTO : undefined,
facetIds: shouldSendFacetIds ? facetIdsDTO : undefined
metadataBlockNames: metadataBlockNamesDTO,
inputLevels: inputLevelsDTO,
facetIds: facetIdsDTO,
inheritMetadataBlocksFromParent: useMetadataFieldsFromParentChecked,
inheritFacetsFromParent: useFacetsFromParentChecked
}

if (mode === 'create') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,128 +612,4 @@ describe('CollectionFormHelper', () => {
expect(result).to.equal(true)
})
})

describe('defineShouldSendMetadataBlockNamesAndInputLevels', () => {
it('for edit mode - returns true if is editing root collection and blockNamesHaveChanged is true', () => {
const result = CollectionFormHelper.defineShouldSendMetadataBlockNamesAndInputLevels(
false,
true,
true,
false,
'edit'
)

expect(result).to.equal(true)
})

it('for edit mode - returns true if is editing root collection and inputLevelsHaveChanged is true', () => {
const result = CollectionFormHelper.defineShouldSendMetadataBlockNamesAndInputLevels(
false,
true,
false,
true,
'edit'
)

expect(result).to.equal(true)
})

it('for edit mode - returns false if is editing root collection and blockNamesHaveChanged and inputLevelsHaveChanged are false', () => {
const result = CollectionFormHelper.defineShouldSendMetadataBlockNamesAndInputLevels(
false,
true,
false,
false,
'edit'
)

expect(result).to.equal(false)
})

it('for edit mode - returns false if is not editing root collection and useFieldsFromParentChecked is true', () => {
const result = CollectionFormHelper.defineShouldSendMetadataBlockNamesAndInputLevels(
true,
false,
true,
true,
'edit'
)

expect(result).to.equal(false)
})

it('for edit mode - returns true if is not editing root collection and useFieldsFromParentChecked is false', () => {
const result = CollectionFormHelper.defineShouldSendMetadataBlockNamesAndInputLevels(
false,
false,
true,
true,
'edit'
)

expect(result).to.equal(true)
})

it('for create mode - returns false if useFieldsFromParentChecked is true', () => {
const result = CollectionFormHelper.defineShouldSendMetadataBlockNamesAndInputLevels(
true,
false,
true,
true,
'create'
)

expect(result).to.equal(false)
})

it('for create mode - returns true if useFieldsFromParentChecked is false', () => {
const result = CollectionFormHelper.defineShouldSendMetadataBlockNamesAndInputLevels(
false,
false,
true,
true,
'create'
)

expect(result).to.equal(true)
})
})

describe('defineShouldSendFacetIds', () => {
it('for edit mode - returns true if is editing root collection and facetIdsHaveChanged is true', () => {
const result = CollectionFormHelper.defineShouldSendFacetIds(false, true, true, 'edit')

expect(result).to.equal(true)
})

it('for edit mode - returns false if is editing root collection and facetIdsHaveChanged is false', () => {
const result = CollectionFormHelper.defineShouldSendFacetIds(false, true, false, 'edit')

expect(result).to.equal(false)
})

it('for edit mode - returns false if is not editing root collection and useFacetsFromParentChecked is true', () => {
const result = CollectionFormHelper.defineShouldSendFacetIds(true, false, true, 'edit')

expect(result).to.equal(false)
})

it('for edit mode - returns true if is not editing root collection and useFacetsFromParentChecked is false', () => {
const result = CollectionFormHelper.defineShouldSendFacetIds(false, false, true, 'edit')

expect(result).to.equal(true)
})

it('for create mode - returns false if useFacetsFromParentChecked is true', () => {
const result = CollectionFormHelper.defineShouldSendFacetIds(true, false, true, 'create')

expect(result).to.equal(false)
})

it('for create mode - returns true if useFacetsFromParentChecked is false', () => {
const result = CollectionFormHelper.defineShouldSendFacetIds(false, false, true, 'create')

expect(result).to.equal(true)
})
})
})
Loading