Skip to content

Commit

Permalink
refactor: remove temporal use case to get all blocks with get all blo…
Browse files Browse the repository at this point in the history
…cks new use case
  • Loading branch information
g-saracca committed Aug 19, 2024
1 parent f66b503 commit 50f0665
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 163 deletions.
6 changes: 0 additions & 6 deletions public/locales/en/createCollection.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@
"sectionLabel": "Metadata Fields",
"helperText": "Choose the metadata fields to use in dataset templates and when adding a dataset to this dataverse.",
"useMetadataFieldsFrom": "Use metadata fields from",
"citationMetadata": "Citation Metadata (Required)",
"geospatialMetadata": "Geospatial Metadata",
"socialScienceMetadata": "Social Science and Humanities Metadata",
"astrophysicsMetadata": "Astronomy and Astrophysics Metadata",
"biomedicalMetadata": "Life Sciences Metadata",
"journalMetadata": "Journal Metadata",
"inputLevelsTable": {
"hideTableAriaLabel": "Hide input levels table",
"requiredByDataverse": "Required by Dataverse",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MetadataBlockInfoDisplayFormat, MetadataBlockInfo } from '../models/Met

export interface MetadataBlockInfoRepository {
getByName: (name: string) => Promise<MetadataBlockInfoDisplayFormat | undefined>
getAllTemporal: (names: string[]) => Promise<MetadataBlockInfo[]>
getAll: () => Promise<MetadataBlockInfo[]>
getDisplayedOnCreateByCollectionId: (
collectionId: number | string
) => Promise<MetadataBlockInfo[]>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { MetadataBlockInfoRepository } from '../repositories/MetadataBlockInfoRepository'
import { MetadataBlockInfo } from '../models/MetadataBlockInfo'

export async function getAllMetadataBlocksInfoTemporal(
metadataBlockInfoRepository: MetadataBlockInfoRepository,
names: string[]
export async function getAllMetadataBlocksInfo(
metadataBlockInfoRepository: MetadataBlockInfoRepository
): Promise<MetadataBlockInfo[]> {
return metadataBlockInfoRepository.getAllTemporal(names).catch((error: Error) => {
return metadataBlockInfoRepository.getAll().catch((error: Error) => {
throw new Error(error.message)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import {
getMetadataBlockByName,
getCollectionMetadataBlocks,
getAllMetadataBlocks,
MetadataBlock as JSMetadataBlockInfo,
ReadError
} from '@iqss/dataverse-client-javascript'
Expand All @@ -23,11 +24,9 @@ export class MetadataBlockInfoJSDataverseRepository implements MetadataBlockInfo
})
}

// TODO: This will be replaced to a new use case that will return all metadata blocks info
getAllTemporal(names: string[]): Promise<MetadataBlockInfo[]> {
const blockPromises = names.map((name) => getMetadataBlockByName.execute(name))

return Promise.all(blockPromises)
getAll(): Promise<MetadataBlockInfo[]> {
return getAllMetadataBlocks
.execute()
.then((jsMetadataBlockInfo: JSMetadataBlockInfo[]) => {
return jsMetadataBlockInfo
})
Expand Down
28 changes: 12 additions & 16 deletions src/sections/create-collection/CreateCollection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { useGetCollectionMetadataBlocksInfo } from './useGetCollectionMetadataBl
import { useGetAllMetadataBlocksInfo } from './useGetAllMetadataBlocksInfo'
import { CollectionRepository } from '../../collection/domain/repositories/CollectionRepository'
import { MetadataBlockInfoRepository } from '../../metadata-block-info/domain/repositories/MetadataBlockInfoRepository'
import { MetadataBlockName } from '../../dataset/domain/models/Dataset'
import { useLoading } from '../loading/LoadingContext'
import { useSession } from '../session/SessionContext'
import { CollectionFormHelper } from './collection-form/CollectionFormHelper'
Expand Down Expand Up @@ -72,25 +71,22 @@ export function CreateCollection({
)
}, [baseInputLevels, formattedCollectionInputLevels])

const baseBlockNames = useDeepCompareMemo(() => {
return allMetadataBlocksInfo.reduce((acc, block) => {
acc[block.name as keyof CollectionFormMetadataBlocks] = false
return acc
}, {} as CollectionFormMetadataBlocks)
}, [allMetadataBlocksInfo])

const defaultBlocksNames = useDeepCompareMemo(
() =>
metadataBlocksInfo
.map((block) => block.name)
.reduce(
(acc, blockName) => {
acc[blockName as keyof CollectionFormMetadataBlocks] = true
return acc
},
{
[MetadataBlockName.CITATION]: false,
[MetadataBlockName.GEOSPATIAL]: false,
[MetadataBlockName.SOCIAL_SCIENCE]: false,
[MetadataBlockName.ASTROPHYSICS]: false,
[MetadataBlockName.BIOMEDICAL]: false,
[MetadataBlockName.JOURNAL]: false
}
),
[metadataBlocksInfo]
.reduce((acc, blockName) => {
acc[blockName as keyof CollectionFormMetadataBlocks] = true
return acc
}, baseBlockNames),
[metadataBlocksInfo, baseBlockNames]
)

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,14 @@ export type CollectionFormData = {
[METADATA_BLOCKS_NAMES_GROUPER]: CollectionFormMetadataBlocks
[INPUT_LEVELS_GROUPER]: FormattedCollectionInputLevels
}
export type CollectionFormMetadataBlock = Exclude<
MetadataBlockName,
MetadataBlockName.CODE_META | MetadataBlockName.COMPUTATIONAL_WORKFLOW
>

export type CollectionFormMetadataBlocks = Record<CollectionFormMetadataBlock, boolean>
export type CollectionFormMetadataBlocks = Record<MetadataBlockName, boolean>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import { MetadataBlockName } from '../../../metadata-block-info/domain/models/MetadataBlockInfo'
import { ReducedMetadataBlockInfo, ReducedMetadataFieldInfo } from '../useGetAllMetadataBlocksInfo'
import {
CollectionFormMetadataBlock,
CollectionFormMetadataBlocks,
CONDITIONALLY_REQUIRED_FIELDS,
FormattedCollectionInputLevels,
Expand Down Expand Up @@ -34,7 +33,7 @@ export class CollectionFormHelper {
include: true,
optionalOrRequired:
isFieldRequiredByDataverse && !isAConditionallyRequiredField ? 'required' : 'optional',
parentBlockName: block.name as CollectionFormMetadataBlock
parentBlockName: block.name as MetadataBlockName
}

if (field.childMetadataFields) {
Expand All @@ -51,7 +50,7 @@ export class CollectionFormHelper {
isChildFieldRequiredByDataverse && !isAConditionallyRequiredChildField
? 'required'
: 'optional',
parentBlockName: block.name as CollectionFormMetadataBlock
parentBlockName: block.name as MetadataBlockName
}
})
}
Expand Down Expand Up @@ -105,50 +104,6 @@ export class CollectionFormHelper {
return result
}

public static separateMetadataBlocksInfoByNames(
allMetadataBlocksInfo: ReducedMetadataBlockInfo[]
): {
citationBlock: ReducedMetadataBlockInfo
geospatialBlock: ReducedMetadataBlockInfo
socialScienceBlock: ReducedMetadataBlockInfo
astrophysicsBlock: ReducedMetadataBlockInfo
biomedicalBlock: ReducedMetadataBlockInfo
journalBlock: ReducedMetadataBlockInfo
} {
const citationBlock: ReducedMetadataBlockInfo = allMetadataBlocksInfo.find(
(block) => block.name === MetadataBlockName.CITATION
) as ReducedMetadataBlockInfo

const geospatialBlock: ReducedMetadataBlockInfo = allMetadataBlocksInfo.find(
(block) => block.name === MetadataBlockName.GEOSPATIAL
) as ReducedMetadataBlockInfo

const socialScienceBlock: ReducedMetadataBlockInfo = allMetadataBlocksInfo.find(
(block) => block.name === MetadataBlockName.SOCIAL_SCIENCE
) as ReducedMetadataBlockInfo

const astrophysicsBlock: ReducedMetadataBlockInfo = allMetadataBlocksInfo.find(
(block) => block.name === MetadataBlockName.ASTROPHYSICS
) as ReducedMetadataBlockInfo

const biomedicalBlock: ReducedMetadataBlockInfo = allMetadataBlocksInfo.find(
(block) => block.name === MetadataBlockName.BIOMEDICAL
) as ReducedMetadataBlockInfo

const journalBlock: ReducedMetadataBlockInfo = allMetadataBlocksInfo.find(
(block) => block.name === MetadataBlockName.JOURNAL
) as ReducedMetadataBlockInfo

return {
citationBlock,
geospatialBlock,
socialScienceBlock,
astrophysicsBlock,
biomedicalBlock,
journalBlock
}
}

public static formatFormMetadataBlockNamesToMetadataBlockNamesDTO(
formMetadataBlockNames: CollectionFormMetadataBlocks
): string[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ReducedMetadataBlockInfo } from '../../useGetAllMetadataBlocksInfo'
import { MetadataInputLevelFieldsBlock } from './metadata-input-level-fields-block/MetadataInputLevelFieldsBlock'
import { FieldsFromParentCheckbox } from './fields-from-parent-checkbox/FieldsFromParentCheckbox'
import { MetadataBlockName } from '../../../../metadata-block-info/domain/models/MetadataBlockInfo'
import { CollectionFormHelper } from '../CollectionFormHelper'
import { CollectionFormData } from '../CollectionForm'

interface MetadataFieldsSectionProps {
Expand All @@ -18,15 +17,6 @@ export const MetadataFieldsSection = ({
}: MetadataFieldsSectionProps) => {
const { t } = useTranslation('createCollection', { keyPrefix: 'fields.metadataFields' })

const {
citationBlock,
geospatialBlock,
socialScienceBlock,
astrophysicsBlock,
biomedicalBlock,
journalBlock
} = CollectionFormHelper.separateMetadataBlocksInfoByNames(allMetadataBlocksInfo)

return (
<Row>
<Col lg={3}>
Expand All @@ -37,37 +27,16 @@ export const MetadataFieldsSection = ({
<Col className="mt-3">
<Stack gap={2}>
<FieldsFromParentCheckbox defaultValues={defaultValues} />

<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.CITATION}
blockDisplayName={t('citationMetadata')}
reducedMetadataBlockInfo={citationBlock}
/>
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.GEOSPATIAL}
blockDisplayName={t('geospatialMetadata')}
reducedMetadataBlockInfo={geospatialBlock}
/>
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.SOCIAL_SCIENCE}
blockDisplayName={t('socialScienceMetadata')}
reducedMetadataBlockInfo={socialScienceBlock}
/>
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.ASTROPHYSICS}
blockDisplayName={t('astrophysicsMetadata')}
reducedMetadataBlockInfo={astrophysicsBlock}
/>
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.BIOMEDICAL}
blockDisplayName={t('biomedicalMetadata')}
reducedMetadataBlockInfo={biomedicalBlock}
/>
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.JOURNAL}
blockDisplayName={t('journalMetadata')}
reducedMetadataBlockInfo={journalBlock}
/>
{allMetadataBlocksInfo.map((block) => {
return (
<MetadataInputLevelFieldsBlock
key={block.name}
blockName={block.name as MetadataBlockName}
blockDisplayName={block.displayName}
reducedMetadataBlockInfo={block}
/>
)
})}
</Stack>
</Col>
</Col>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const MetadataInputLevelFieldsBlock = ({
id={checkboxID}
onChange={(e) => handleIncludeBlockChange(e, onChange)}
name={metadataBlockFieldName}
label={blockDisplayName}
label={isCitation ? `${blockDisplayName} (Required)` : blockDisplayName}
checked={value as boolean}
isInvalid={invalid}
invalidFeedback={error?.message}
Expand Down
17 changes: 2 additions & 15 deletions src/sections/create-collection/useGetAllMetadataBlocksInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { useEffect, useState } from 'react'
import { MetadataBlockInfoRepository } from '../../metadata-block-info/domain/repositories/MetadataBlockInfoRepository'
import {
MetadataBlockInfo,
MetadataBlockName,
MetadataField
} from '../../metadata-block-info/domain/models/MetadataBlockInfo'
import { getAllMetadataBlocksInfoTemporal } from '../../metadata-block-info/domain/useCases/getAllMetadataBlocksInfoTemporal'
import { getAllMetadataBlocksInfo } from '../../metadata-block-info/domain/useCases/getAllMetadataBlocksInfo'

interface Props {
metadataBlockInfoRepository: MetadataBlockInfoRepository
Expand Down Expand Up @@ -33,15 +32,6 @@ export type ReducedMetadataFieldInfo = ReducedMetadataFieldsAndChildsInfo & {
childMetadataFields?: Record<string, ReducedMetadataFieldsAndChildsInfo>
}

const blocksNames: MetadataBlockName[] = [
MetadataBlockName.CITATION,
MetadataBlockName.GEOSPATIAL,
MetadataBlockName.SOCIAL_SCIENCE,
MetadataBlockName.ASTROPHYSICS,
MetadataBlockName.BIOMEDICAL,
MetadataBlockName.JOURNAL
]

export const useGetAllMetadataBlocksInfo = ({
metadataBlockInfoRepository
}: Props): UseGetAllMetadataBlocksInfo => {
Expand All @@ -53,10 +43,7 @@ export const useGetAllMetadataBlocksInfo = ({
const handleGetInfo = async () => {
setIsLoading(true)
try {
const blocksInfo = await getAllMetadataBlocksInfoTemporal(
metadataBlockInfoRepository,
blocksNames
)
const blocksInfo = await getAllMetadataBlocksInfo(metadataBlockInfoRepository)

const reducedMetadataBlocksInfo: ReducedMetadataBlockInfo[] =
reduceMetadataBlocksInfo(blocksInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class MetadataBlockInfoMockErrorRepository implements MetadataBlockInfoMo
})
}

getAllTemporal(_names: string[]): Promise<MetadataBlockInfo[]> {
getAll(): Promise<MetadataBlockInfo[]> {
return new Promise((_resolve, reject) => {
setTimeout(() => {
reject('Error thrown from mock')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class MetadataBlockInfoMockLoadingRepository implements MetadataBlockInfo
return new Promise(() => {})
}

getAllTemporal(_names: string[]): Promise<MetadataBlockInfo[]> {
getAll(): Promise<MetadataBlockInfo[]> {
return new Promise(() => {})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class MetadataBlockInfoMockRepository implements MetadataBlockInfoReposit
})
}

getAllTemporal(_names: string[]): Promise<MetadataBlockInfo[]> {
getAll(): Promise<MetadataBlockInfo[]> {
return new Promise((resolve) => {
setTimeout(() => {
resolve([
Expand Down
Loading

0 comments on commit 50f0665

Please sign in to comment.