Skip to content

Commit

Permalink
feat: remove inputLevels from not selected blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
g-saracca committed Jul 31, 2024
1 parent c934154 commit c4e64db
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 38 deletions.
19 changes: 12 additions & 7 deletions src/sections/create-collection/CreateCollection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
CollectionFormData,
CollectionFormMetadataBlocks,
FormattedCollectionInputLevels,
FormattedCollectionInputLevelsWithoutParentBlockName,
METADATA_BLOCKS_NAMES_GROUPER,
USE_FIELDS_FROM_PARENT
} from './collection-form/CollectionForm'
Expand Down Expand Up @@ -59,9 +60,10 @@ export function CreateCollection({
return CollectionFormHelper.defineBaseInputLevels(allMetadataBlocksInfo)
}, [allMetadataBlocksInfo])

const formDefaultInputLevels: FormattedCollectionInputLevels = useDeepCompareMemo(() => {
return CollectionFormHelper.formatCollectiontInputLevels(collection?.inputLevels)
}, [collection?.inputLevels])
const formDefaultInputLevels: FormattedCollectionInputLevelsWithoutParentBlockName =
useDeepCompareMemo(() => {
return CollectionFormHelper.formatCollectiontInputLevels(collection?.inputLevels)
}, [collection?.inputLevels])

const defaultBlocksNames = useDeepCompareMemo(
() =>
Expand Down Expand Up @@ -112,6 +114,12 @@ export function CreateCollection({
return <CreateCollectionSkeleton />
}

// TODO:ME use memo for this also?¿
const mergedInputLevels = CollectionFormHelper.mergeBaseAndDefaultInputLevels(
formBaseInputLevels,
formDefaultInputLevels
)

const formDefaultValues: CollectionFormData = {
hostCollection: collection.name,
name: user?.displayName ? `${user?.displayName} Collection` : '',
Expand All @@ -123,10 +131,7 @@ export function CreateCollection({
description: '',
[USE_FIELDS_FROM_PARENT]: true,
[METADATA_BLOCKS_NAMES_GROUPER]: defaultBlocksNames,
inputLevels: {
...formBaseInputLevels,
...formDefaultInputLevels
}
inputLevels: mergedInputLevels
}

return (
Expand Down
19 changes: 14 additions & 5 deletions src/sections/create-collection/collection-form/CollectionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,30 @@ export type CollectionFormData = {
contacts: { value: string }[]
[USE_FIELDS_FROM_PARENT]: boolean
[METADATA_BLOCKS_NAMES_GROUPER]: CollectionFormMetadataBlocks
[INPUT_LEVELS_GROUPER]?: FormattedCollectionInputLevels
[INPUT_LEVELS_GROUPER]: FormattedCollectionInputLevels
}

export type CollectionFormMetadataBlocks = Omit<
Record<MetadataBlockName, boolean>,
'codeMeta20' | 'computationalworkflow'
export type CollectionFormMetadataBlock = Exclude<
MetadataBlockName,
MetadataBlockName.CODE_META | MetadataBlockName.COMPUTATIONAL_WORKFLOW
>

export type CollectionFormMetadataBlocks = Record<CollectionFormMetadataBlock, boolean>

export type FormattedCollectionInputLevels = {
[key: string]: {
include: boolean
optionalOrRequired: CollectionFormInputLevelValue
parentBlockName: CollectionFormMetadataBlock
}
}

export type FormattedCollectionInputLevelsWithoutParentBlockName = {
[K in keyof FormattedCollectionInputLevels]: Omit<
FormattedCollectionInputLevels[K],
'parentBlockName'
>
}

export const CollectionFormInputLevelOptions = {
OPTIONAL: 'optional',
REQUIRED: 'required'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { CollectionInputLevel } from '../../../collection/domain/models/Collection'
import {
CollectionDTO,
CollectionInputLevelDTO
} from '../../../collection/domain/useCases/DTOs/CollectionDTO'
import { MetadataBlockName } from '../../../metadata-block-info/domain/models/MetadataBlockInfo'
import { ReducedMetadataBlockInfo } from '../useGetAllMetadataBlocksInfoByName'
import { FormattedCollectionInputLevels } from './CollectionForm'
import {
CollectionFormMetadataBlock,
CollectionFormMetadataBlocks,
FormattedCollectionInputLevels,
FormattedCollectionInputLevelsWithoutParentBlockName
} from './CollectionForm'

export class CollectionFormHelper {
public static replaceDotWithSlash = (str: string) => str.replace(/\./g, '/')
Expand All @@ -20,15 +29,17 @@ export class CollectionFormHelper {

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

if (field.childMetadataFields) {
Object.entries(field.childMetadataFields).forEach(([_key, childField]) => {
const normalizedFieldName = this.replaceDotWithSlash(childField.name)
childFields[normalizedFieldName] = {
include: true,
optionalOrRequired: 'optional'
optionalOrRequired: 'optional',
parentBlockName: block.name as CollectionFormMetadataBlock
}
})
}
Expand All @@ -43,8 +54,8 @@ export class CollectionFormHelper {

public static formatCollectiontInputLevels(
collectionInputLevels: CollectionInputLevel[] | undefined
): FormattedCollectionInputLevels {
const result: FormattedCollectionInputLevels = {}
): FormattedCollectionInputLevelsWithoutParentBlockName {
const result: FormattedCollectionInputLevelsWithoutParentBlockName = {}

if (!collectionInputLevels) {
return result
Expand All @@ -63,6 +74,28 @@ export class CollectionFormHelper {
return result
}

public static mergeBaseAndDefaultInputLevels(
baseInputLevels: FormattedCollectionInputLevels,
defaultInputLevels: FormattedCollectionInputLevelsWithoutParentBlockName
): FormattedCollectionInputLevels {
const result: FormattedCollectionInputLevels = { ...baseInputLevels }

for (const key in defaultInputLevels) {
if (baseInputLevels[key]) {
result[key] = {
...baseInputLevels[key],
...defaultInputLevels[key],
parentBlockName: baseInputLevels[key].parentBlockName
}
} else {
// TODO:ME Fix this ts error
result[key] = { ...defaultInputLevels[key] }

Check failure on line 92 in src/sections/create-collection/collection-form/CollectionFormHelper.ts

View workflow job for this annotation

GitHub Actions / lint

Property 'parentBlockName' is missing in type '{ include: boolean; optionalOrRequired: CollectionFormInputLevelValue; }' but required in type '{ include: boolean; optionalOrRequired: CollectionFormInputLevelValue; parentBlockName: CollectionFormMetadataBlock; }'.
}
}

return result
}

public static separateMetadataBlocksInfoByNames(
allMetadataBlocksInfo: ReducedMetadataBlockInfo[]
): {
Expand Down Expand Up @@ -106,4 +139,55 @@ export class CollectionFormHelper {
journalBlock
}
}

public static formatFormMetadataBlockNamesToMetadataBlockNamesDTO(
formMetadataBlockNames: CollectionFormMetadataBlocks
): string[] {
const result: CollectionDTO['metadataBlockNames'] = []

Object.entries(formMetadataBlockNames).forEach(([key, value]) => {
if (value) {
result.push(key)
}
})

return result
}

public static formatFormInputLevelsToInputLevelsDTO(
metadataBlockNamesSelected: string[],
formCollectionInputLevels: FormattedCollectionInputLevels
): CollectionInputLevelDTO[] {
const normalizedInputLevels =
this.replaceSlashBackToDotsFromInputLevels(formCollectionInputLevels)

const result: CollectionInputLevelDTO[] = []

Object.entries(normalizedInputLevels).forEach(([key, value]) => {
if (metadataBlockNamesSelected.includes(value.parentBlockName)) {
result.push({
datasetFieldName: key,
include: value.include,
required: value.optionalOrRequired === 'required'
})
}
})

return result
}

private static replaceSlashBackToDotsFromInputLevels(
inputLevels: FormattedCollectionInputLevels
): FormattedCollectionInputLevels {
const result: FormattedCollectionInputLevels = {}

Object.entries(inputLevels).forEach(([key, value]) => {
const replaceSlashWithDot = (str: string) => str.replace(/\//g, '.')
const normalizedFieldName = replaceSlashWithDot(key)

result[normalizedFieldName] = value
})

return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import { WriteError } from '@iqss/dataverse-client-javascript'
import { createCollection } from '../../../collection/domain/useCases/createCollection'
import { CollectionRepository } from '../../../collection/domain/repositories/CollectionRepository'
import { CollectionDTO } from '../../../collection/domain/useCases/DTOs/CollectionDTO'
import { CollectionFormData, CollectionFormValuesOnSubmit } from './CollectionForm'
import {
CollectionFormData,
CollectionFormValuesOnSubmit,
INPUT_LEVELS_GROUPER,
METADATA_BLOCKS_NAMES_GROUPER
} from './CollectionForm'
import { Route } from '../../Route.enum'
import { JSDataverseWriteErrorHandler } from '../../../shared/helpers/JSDataverseWriteErrorHandler'
import { CollectionFormHelper } from './CollectionFormHelper'

export enum SubmissionStatus {
NotSubmitted = 'NotSubmitted',
Expand Down Expand Up @@ -45,37 +51,48 @@ export function useSubmitCollection(
const submitForm = (formData: CollectionFormValuesOnSubmit): void => {
setSubmissionStatus(SubmissionStatus.IsSubmitting)

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

const metadataBlockNamesDTO =
CollectionFormHelper.formatFormMetadataBlockNamesToMetadataBlockNamesDTO(
formData[METADATA_BLOCKS_NAMES_GROUPER]
)

const inputLevelsDTO = CollectionFormHelper.formatFormInputLevelsToInputLevelsDTO(
metadataBlockNamesDTO,
formData[INPUT_LEVELS_GROUPER]
)

const newCollection: CollectionDTO = {
name: formData.name,
alias: formData.alias,
type: formData.type,
affiliation: formData.affiliation,
description: formData.description,
contacts: formData.contacts.map((contact) => contact.value),
inputLevels: []
contacts: contactsDTO,
metadataBlockNames: metadataBlockNamesDTO,
inputLevels: inputLevelsDTO
}

console.log({ newCollection })

// TODO: We can't send the hostCollection name, but we should send the hostCollection alias
// So in a next iteration we should get the hostCollection alias from the hostCollection name selected

// createCollection(collectionRepository, newCollection, ownerCollectionId)
// .then(() => {
// setSubmitError(null)
// setSubmissionStatus(SubmissionStatus.SubmitComplete)
// navigate(`${Route.COLLECTIONS}?id=${newCollection.alias}`, {
// state: { created: true }
// })
// return
// })
// .catch((err: WriteError) => {
// const error = new JSDataverseWriteErrorHandler(err)
// const formattedError = error.getReasonWithoutStatusCode() ?? error.getErrorMessage()
// setSubmitError(formattedError)
// setSubmissionStatus(SubmissionStatus.Errored)
// onSubmitErrorCallback()
// })
createCollection(collectionRepository, newCollection, ownerCollectionId)
.then(() => {
setSubmitError(null)
setSubmissionStatus(SubmissionStatus.SubmitComplete)
navigate(`${Route.COLLECTIONS}?id=${newCollection.alias}`, {
state: { created: true }
})
return
})
.catch((err: WriteError) => {
const error = new JSDataverseWriteErrorHandler(err)
const formattedError = error.getReasonWithoutStatusCode() ?? error.getErrorMessage()
setSubmitError(formattedError)
setSubmissionStatus(SubmissionStatus.Errored)
onSubmitErrorCallback()
})
}

return {
Expand Down

0 comments on commit c4e64db

Please sign in to comment.