Skip to content

Commit

Permalink
feat: make required by dataverse fields required by default and dont …
Browse files Browse the repository at this point in the history
…allow to set them as optional
  • Loading branch information
g-saracca committed Aug 2, 2024
1 parent a7b4a47 commit 90c08f7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/sections/create-collection/CreateCollection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
CollectionFormMetadataBlocks,
FormattedCollectionInputLevels,
FormattedCollectionInputLevelsWithoutParentBlockName,
INPUT_LEVELS_GROUPER,
METADATA_BLOCKS_NAMES_GROUPER,
USE_FIELDS_FROM_PARENT
} from './collection-form/CollectionForm'
Expand Down Expand Up @@ -130,7 +131,7 @@ export function CreateCollection({
description: '',
[USE_FIELDS_FROM_PARENT]: true,
[METADATA_BLOCKS_NAMES_GROUPER]: defaultBlocksNames,
inputLevels: mergedInputLevels
[INPUT_LEVELS_GROUPER]: mergedInputLevels
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
CollectionFormMetadataBlock,
CollectionFormMetadataBlocks,
FormattedCollectionInputLevels,
FormattedCollectionInputLevelsWithoutParentBlockName
FormattedCollectionInputLevelsWithoutParentBlockName,
REQUIRED_BY_DATAVERSE_FIELDS
} from './CollectionForm'

export class CollectionFormHelper {
Expand All @@ -26,19 +27,26 @@ export class CollectionFormHelper {
allMetadataBlocksInfoReduced.forEach((block) => {
Object.entries(block.metadataFields).forEach(([_key, field]) => {
const normalizedFieldName = this.replaceDotWithSlash(field.name)
const isRequiredByDataverseField = REQUIRED_BY_DATAVERSE_FIELDS.includes(
field.name as (typeof REQUIRED_BY_DATAVERSE_FIELDS)[number]
)

fields[normalizedFieldName] = {
include: true,
optionalOrRequired: 'optional',
optionalOrRequired: isRequiredByDataverseField ? 'required' : 'optional',
parentBlockName: block.name as CollectionFormMetadataBlock
}

if (field.childMetadataFields) {
Object.entries(field.childMetadataFields).forEach(([_key, childField]) => {
const normalizedFieldName = this.replaceDotWithSlash(childField.name)
const isRequiredByDataverseField = REQUIRED_BY_DATAVERSE_FIELDS.includes(
childField.name as (typeof REQUIRED_BY_DATAVERSE_FIELDS)[number]
)

childFields[normalizedFieldName] = {
include: true,
optionalOrRequired: 'optional',
optionalOrRequired: isRequiredByDataverseField ? 'required' : 'optional',
parentBlockName: block.name as CollectionFormMetadataBlock
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { ChangeEvent } from 'react'
import { Form, Stack } from '@iqss/dataverse-design-system'
import { Controller, useFormContext, useWatch } from 'react-hook-form'
import { ReducedMetadataFieldInfo } from '../../../../useGetAllMetadataBlocksInfo'
import { CollectionFormInputLevelValue, INPUT_LEVELS_GROUPER } from '../../../CollectionForm'
import {
CollectionFormInputLevelValue,
INPUT_LEVELS_GROUPER,
REQUIRED_BY_DATAVERSE_FIELDS
} from '../../../CollectionForm'

type RequiredOptionalRadiosProps =
| {
Expand Down Expand Up @@ -47,9 +51,13 @@ export const RequiredOptionalRadios = ({
e: ChangeEvent<HTMLInputElement>,
formOnChange: (...event: unknown[]) => void
) => {
// Check if all siblingChildFields are required then set parent to required also
// Check if all siblingChildFields are required then set parent to required also unless parent is required by dataverse
if (isForChildField) {
if (e.target.value === 'required') {
const isParentFieldRequiredByDataverseField = REQUIRED_BY_DATAVERSE_FIELDS.includes(
parentIncludeName as (typeof REQUIRED_BY_DATAVERSE_FIELDS)[number]
)
// If parent is required by dataverse, then is already required
if (e.target.value === 'required' && !isParentFieldRequiredByDataverseField) {
const allSiblingsRequired = (
siblingChildFieldsValues as CollectionFormInputLevelValue[]
).every((value) => value === 'required')
Expand All @@ -59,7 +67,8 @@ export const RequiredOptionalRadios = ({
}
}

if (e.target.value === 'optional') {
// If parent is required by dataverse, then is already required and should not be set to optional
if (e.target.value === 'optional' && !isParentFieldRequiredByDataverseField) {
setValue(`${INPUT_LEVELS_GROUPER}.${parentIncludeName}.optionalOrRequired`, 'optional')
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ export const useGetAllMetadataBlocksInfo = ({
blocksNames
)

console.log(blocksInfo)

const reducedMetadataBlocksInfo: ReducedMetadataBlockInfo[] = blocksInfo.map(
(blockInfo) => {
const formattedMetadataFields: Record<string, ReducedMetadataFieldInfo> =
Expand Down

0 comments on commit 90c08f7

Please sign in to comment.