diff --git a/package-lock.json b/package-lock.json index d737851d5..42ddaf1e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.1.0", "dependencies": { "@faker-js/faker": "7.6.0", - "@iqss/dataverse-client-javascript": "npm:@IQSS/dataverse-client-javascript@^2.0.0-alpha.4", + "@iqss/dataverse-client-javascript": "2.0.0-alpha.4", "@iqss/dataverse-design-system": "*", "@istanbuljs/nyc-config-typescript": "1.0.2", "@tanstack/react-table": "8.9.2", @@ -3677,6 +3677,7 @@ "version": "2.0.0-alpha.4", "resolved": "https://npm.pkg.github.com/download/@IQSS/dataverse-client-javascript/2.0.0-alpha.4/10ab7e0ed2a31a09b1b32d27521b96f84ef50f4f", "integrity": "sha512-SJdkBIks+yjJxEVw8G7sf4YY2Bujl+8vOD+fZqt2qhIGmyPSlj9ld0APnYyoVX6lI8lGsREHZzYYjzeAmYfxhw==", + "license": "MIT", "dependencies": { "@types/node": "^18.15.11", "@types/turndown": "^5.0.1", diff --git a/src/sections/create-collection/collection-form/metadata-fields-section/fields-from-parent-checkbox/FieldsFromParentCheckbox.tsx b/src/sections/create-collection/collection-form/metadata-fields-section/fields-from-parent-checkbox/FieldsFromParentCheckbox.tsx index 30c93442d..e04c1ea71 100644 --- a/src/sections/create-collection/collection-form/metadata-fields-section/fields-from-parent-checkbox/FieldsFromParentCheckbox.tsx +++ b/src/sections/create-collection/collection-form/metadata-fields-section/fields-from-parent-checkbox/FieldsFromParentCheckbox.tsx @@ -2,7 +2,6 @@ import { ChangeEvent, useId, useState } from 'react' import { useTranslation } from 'react-i18next' import { Controller, UseControllerProps, useFormContext, useWatch } from 'react-hook-form' import { Form } from '@iqss/dataverse-design-system' -import { MetadataBlockName } from '../../../../../metadata-block-info/domain/models/MetadataBlockInfo' import { ConfirmResetModificationsModal } from './ConfirmResetModificationsModal' import { CollectionFormData, @@ -11,15 +10,6 @@ import { USE_FIELDS_FROM_PARENT } from '../../CollectionForm' -const ALL_INPUT_LEVEL_FIELDS = [ - MetadataBlockName.CITATION, - MetadataBlockName.GEOSPATIAL, - MetadataBlockName.SOCIAL_SCIENCE, - MetadataBlockName.ASTROPHYSICS, - MetadataBlockName.BIOMEDICAL, - MetadataBlockName.JOURNAL -] - interface FieldsFromParentCheckboxProps { defaultValues: CollectionFormData } @@ -34,12 +24,11 @@ export const FieldsFromParentCheckbox = ({ defaultValues }: FieldsFromParentChec const handleContinueWithReset = () => { setValue(USE_FIELDS_FROM_PARENT, true) - // Reset metadata block names checboxes to the inital value - ALL_INPUT_LEVEL_FIELDS.forEach((blockName) => { - setValue( - `${METADATA_BLOCKS_NAMES_GROUPER}.${blockName}`, - defaultValues[METADATA_BLOCKS_NAMES_GROUPER][blockName] - ) + const metadataBlockDefaultValues = Object.entries(defaultValues[METADATA_BLOCKS_NAMES_GROUPER]) + + // Reset metadata block names checkboxes to the inital value + metadataBlockDefaultValues.forEach(([blockName, blockInitialValue]) => { + setValue(`${METADATA_BLOCKS_NAMES_GROUPER}.${blockName}`, blockInitialValue) }) // Reset input levels to the initial value diff --git a/src/sections/create-collection/collection-form/useSubmitCollection.ts b/src/sections/create-collection/collection-form/useSubmitCollection.ts index e3a724877..7a8fff79c 100644 --- a/src/sections/create-collection/collection-form/useSubmitCollection.ts +++ b/src/sections/create-collection/collection-form/useSubmitCollection.ts @@ -8,7 +8,8 @@ import { CollectionFormData, CollectionFormValuesOnSubmit, INPUT_LEVELS_GROUPER, - METADATA_BLOCKS_NAMES_GROUPER + METADATA_BLOCKS_NAMES_GROUPER, + USE_FIELDS_FROM_PARENT } from './CollectionForm' import { RouteWithParams } from '../../Route.enum' import { JSDataverseWriteErrorHandler } from '../../../shared/helpers/JSDataverseWriteErrorHandler' @@ -65,6 +66,8 @@ export function useSubmitCollection( const facetIdsDTO = formData.facetIds.map((facet) => facet.value) + const useFieldsFromParentChecked = formData[USE_FIELDS_FROM_PARENT] + const newCollection: CollectionDTO = { name: formData.name, alias: formData.alias, @@ -72,8 +75,8 @@ export function useSubmitCollection( affiliation: formData.affiliation, description: formData.description, contacts: contactsDTO, - metadataBlockNames: metadataBlockNamesDTO, - inputLevels: inputLevelsDTO, + metadataBlockNames: useFieldsFromParentChecked ? undefined : metadataBlockNamesDTO, + inputLevels: useFieldsFromParentChecked ? undefined : inputLevelsDTO, facetIds: facetIdsDTO } diff --git a/tests/component/sections/create-collection/CollectionForm.spec.tsx b/tests/component/sections/create-collection/CollectionForm.spec.tsx index b7442e989..0dd7f9f78 100644 --- a/tests/component/sections/create-collection/CollectionForm.spec.tsx +++ b/tests/component/sections/create-collection/CollectionForm.spec.tsx @@ -18,6 +18,7 @@ import { MetadataBlockInfoMother } from '../../metadata-block-info/domain/models import { CollectionFormHelper } from '../../../../src/sections/create-collection/collection-form/CollectionFormHelper' import { MetadataBlockName } from '../../../../src/metadata-block-info/domain/models/MetadataBlockInfo' import { CollectionFacetMother } from '../../collection/domain/models/CollectionFacetMother' +import { CollectionDTO } from '@/collection/domain/useCases/DTOs/CollectionDTO' const collectionRepository: CollectionRepository = {} as CollectionRepository @@ -547,6 +548,75 @@ describe('CollectionForm', () => { cy.findByLabelText('Subtitle').should('be.checked') }) + + it('should send metadataBlockNames and inputLevels as undefined if use fields from parent is checked', () => { + const collectionRepository = {} as CollectionRepository + collectionRepository.create = cy.stub().as('createCollection').resolves() + + cy.customMount( + + ) + // Accept suggestion + cy.findByRole('button', { name: 'Apply suggestion' }).click() + // Select a Category option + cy.findByLabelText(/^Category/i).select(1) + + cy.findByRole('button', { name: 'Create Collection' }).click() + + cy.get('@createCollection').should((spy) => { + const createCollectionSpy = spy as unknown as Cypress.Agent + const collectionDTO = createCollectionSpy.getCall(0).args[0] as CollectionDTO + + const inputLevels = collectionDTO.inputLevels + const metadataBlockNames = collectionDTO.metadataBlockNames + + expect(inputLevels).to.be.undefined + expect(metadataBlockNames).to.be.undefined + }) + }) + + it('should not send metadataBlockNames and inputLevels as undefined if use fields from parent is unchecked', () => { + const collectionRepository = {} as CollectionRepository + collectionRepository.create = cy.stub().as('createCollection').resolves() + + cy.customMount( + + ) + + cy.get('@useFieldsFromParentCheckbox').uncheck({ force: true }) + + // Accept suggestion + cy.findByRole('button', { name: 'Apply suggestion' }).click() + // Select a Category option + cy.findByLabelText(/^Category/i).select(1) + + cy.findByRole('button', { name: 'Create Collection' }).click() + + cy.get('@createCollection').should((spy) => { + const createCollectionSpy = spy as unknown as Cypress.Agent + const collectionDTO = createCollectionSpy.getCall(0).args[0] as CollectionDTO + + const inputLevels = collectionDTO.inputLevels + const metadataBlockNames = collectionDTO.metadataBlockNames + + expect(inputLevels).to.not.be.undefined + expect(metadataBlockNames).to.not.be.undefined + }) + }) }) describe('InputLevelFieldRow', () => {