Skip to content

Commit

Permalink
feat: clean up, refactor all blocks fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
g-saracca committed Jul 30, 2024
1 parent 3dc04f8 commit 3b07a60
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 74 deletions.
10 changes: 5 additions & 5 deletions src/sections/create-collection/CreateCollection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { useDeepCompareMemo } from 'use-deep-compare'
import { useCollection } from '../collection/useCollection'
import { useGetCollectionMetadataBlocksNamesInfo } from './useGetCollectionMetadataBlocksNamesInfo'
import { useGetAllMetadataBlocksInfoByName } from './useGetAllMetadataBlocksInfoByName'
import { CollectionRepository } from '../../collection/domain/repositories/CollectionRepository'
import { MetadataBlockInfoRepository } from '../../metadata-block-info/domain/repositories/MetadataBlockInfoRepository'
import { useLoading } from '../loading/LoadingContext'
import { useSession } from '../session/SessionContext'
import { MetadataBlockInfoRepository } from '../../metadata-block-info/domain/repositories/MetadataBlockInfoRepository'
import { useGetCollectionMetadataBlocksNamesInfo } from './useGetCollectionMetadataBlocksNamesInfo'
import { CollectionFormHelper } from './collection-form/CollectionFormHelper'
import {
CollectionForm,
CollectionFormData,
Expand All @@ -20,8 +22,6 @@ import { SeparationLine } from '../shared/layout/SeparationLine/SeparationLine'
import { RequiredFieldText } from '../shared/form/RequiredFieldText/RequiredFieldText'
import { PageNotFound } from '../page-not-found/PageNotFound'
import { CreateCollectionSkeleton } from './CreateCollectionSkeleton'
import { useGetAllMetadataBlocksInfoByName } from './useGetAllMetadataBlocksInfoByName'
import { CollectionFormHelper } from './collection-form/CollectionFormHelper'

interface CreateCollectionProps {
ownerCollectionId: string
Expand Down Expand Up @@ -144,9 +144,9 @@ export function CreateCollection({

<CollectionForm
collectionRepository={collectionRepository}
metadataBlockInfoRepository={metadataBlockInfoRepository}
ownerCollectionId={ownerCollectionId}
defaultValues={formDefaultValues}
allMetadataBlocksInfo={allMetadataBlocksInfo}
/>
</section>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import {
CollectionType,
CollectionStorage
} from '../../../collection/domain/useCases/DTOs/CollectionDTO'
import { SeparationLine } from '../../shared/layout/SeparationLine/SeparationLine'
import { SubmissionStatus, useSubmitCollection } from './useSubmitCollection'
import { ReducedMetadataBlockInfo } from '../useGetAllMetadataBlocksInfoByName'
import { MetadataBlockName } from '../../../metadata-block-info/domain/models/MetadataBlockInfo'
import { SeparationLine } from '../../shared/layout/SeparationLine/SeparationLine'
import { TopFieldsSection } from './top-fields-section/TopFieldsSection'
import { MetadataFieldsSection } from './metadata-fields-section/MetadataFieldsSection'
import { BrowseSearchFacetsSection } from './browse-search-facets-section/BrowseSearchFacetsSection'
import { MetadataBlockName } from '../../../metadata-block-info/domain/models/MetadataBlockInfo'
import { MetadataBlockInfoRepository } from '../../../metadata-block-info/domain/repositories/MetadataBlockInfoRepository'
import styles from './CollectionForm.module.scss'

export const METADATA_BLOCKS_NAMES_GROUPER = 'metadataBlockNames'
Expand All @@ -23,9 +23,9 @@ export const INPUT_LEVELS_GROUPER = 'inputLevels'

export interface CollectionFormProps {
collectionRepository: CollectionRepository
metadataBlockInfoRepository: MetadataBlockInfoRepository
ownerCollectionId: string
defaultValues: CollectionFormData
allMetadataBlocksInfo: ReducedMetadataBlockInfo[]
}

export type CollectionFormData = {
Expand Down Expand Up @@ -69,9 +69,9 @@ export type CollectionFormValuesOnSubmit = Omit<CollectionFormData, 'type'> & {

export const CollectionForm = ({
collectionRepository,
metadataBlockInfoRepository,
ownerCollectionId,
defaultValues
defaultValues,
allMetadataBlocksInfo
}: CollectionFormProps) => {
const formContainerRef = useRef<HTMLDivElement>(null)
const { t } = useTranslation('createCollection')
Expand Down Expand Up @@ -146,7 +146,7 @@ export const CollectionForm = ({
<Stack>
<Card>
<Card.Body>
<MetadataFieldsSection metadataBlockInfoRepository={metadataBlockInfoRepository} />
<MetadataFieldsSection allMetadataBlocksInfo={allMetadataBlocksInfo} />
</Card.Body>
</Card>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CollectionInputLevel } from '../../../collection/domain/models/Collection'
import { MetadataBlockName } from '../../../metadata-block-info/domain/models/MetadataBlockInfo'
import { ReducedMetadataBlockInfo } from '../useGetAllMetadataBlocksInfoByName'
import { FormattedCollectionInputLevels } from './CollectionForm'

Expand Down Expand Up @@ -61,4 +62,48 @@ 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
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { useTranslation } from 'react-i18next'
import { Col, Form, Row, Stack } from '@iqss/dataverse-design-system'
import { ReducedMetadataBlockInfo } from '../../useGetAllMetadataBlocksInfoByName'
import { MetadataInputLevelFieldsBlock } from './metadata-input-level-fields-block/MetadataInputLevelFieldsBlock'
import { FieldsFromRootCheckbox } from './fields-from-root-checkbox/FieldsFromRootCheckbox'
import { MetadataBlockName } from '../../../../metadata-block-info/domain/models/MetadataBlockInfo'
import { MetadataBlockInfoRepository } from '../../../../metadata-block-info/domain/repositories/MetadataBlockInfoRepository'
import { CollectionFormHelper } from '../CollectionFormHelper'

interface MetadataFieldsSectionProps {
metadataBlockInfoRepository: MetadataBlockInfoRepository
allMetadataBlocksInfo: ReducedMetadataBlockInfo[]
}

export const MetadataFieldsSection = ({
metadataBlockInfoRepository
}: MetadataFieldsSectionProps) => {
export const MetadataFieldsSection = ({ allMetadataBlocksInfo }: MetadataFieldsSectionProps) => {
const { t } = useTranslation('createCollection')

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

return (
<Row>
<Col lg={3}>
Expand All @@ -28,32 +36,32 @@ export const MetadataFieldsSection = ({
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.CITATION}
blockDisplayName="Citation Metadata (Required)"
metadataBlockInfoRepository={metadataBlockInfoRepository}
reducedMetadataBlockInfo={citationBlock}
/>
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.GEOSPATIAL}
blockDisplayName="Geospatial Metadata"
metadataBlockInfoRepository={metadataBlockInfoRepository}
reducedMetadataBlockInfo={geospatialBlock}
/>
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.SOCIAL_SCIENCE}
blockDisplayName="Social Science and Humanities Metadata"
metadataBlockInfoRepository={metadataBlockInfoRepository}
reducedMetadataBlockInfo={socialScienceBlock}
/>
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.ASTROPHYSICS}
blockDisplayName="Astronomy and Astrophysics Metadata"
metadataBlockInfoRepository={metadataBlockInfoRepository}
reducedMetadataBlockInfo={astrophysicsBlock}
/>
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.BIOMEDICAL}
blockDisplayName="Life Sciences Metadata"
metadataBlockInfoRepository={metadataBlockInfoRepository}
reducedMetadataBlockInfo={biomedicalBlock}
/>
<MetadataInputLevelFieldsBlock
blockName={MetadataBlockName.JOURNAL}
blockDisplayName="Journal Metadata"
metadataBlockInfoRepository={metadataBlockInfoRepository}
reducedMetadataBlockInfo={journalBlock}
/>
</Stack>
</Col>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ import { useId, useState } from 'react'
import { Controller, UseControllerProps, useFormContext, useWatch } from 'react-hook-form'
import { Button, Form, Stack } from '@iqss/dataverse-design-system'
import { CloseButton } from 'react-bootstrap'
// import { CollectionInputLevel } from '../../../../../collection/domain/models/Collection'
import { MetadataBlockName } from '../../../../../metadata-block-info/domain/models/MetadataBlockInfo'
import { ReducedMetadataBlockInfo } from '../../../useGetAllMetadataBlocksInfoByName'
import { METADATA_BLOCKS_NAMES_GROUPER, USE_FIELDS_FROM_PARENT } from '../../CollectionForm'
import { MetadataBlockInfoRepository } from '../../../../../metadata-block-info/domain/repositories/MetadataBlockInfoRepository'
import { useGetBlockMetadataInputLevelFields } from './useGetBlockMetadataInputLevelFields'
import { InputLevelsTable } from './input-levels-table/InputLevelsTable'

interface MetadataInputLevelFieldsBlockProps {
blockName: MetadataBlockName
blockDisplayName: string
metadataBlockInfoRepository: MetadataBlockInfoRepository
reducedMetadataBlockInfo: ReducedMetadataBlockInfo
}

export const MetadataInputLevelFieldsBlock = ({
blockName,
blockDisplayName,
metadataBlockInfoRepository
reducedMetadataBlockInfo
}: MetadataInputLevelFieldsBlockProps) => {
const checkboxID = useId()
const { control } = useFormContext()
Expand All @@ -36,12 +34,6 @@ export const MetadataInputLevelFieldsBlock = ({
name: metadataBlockFieldName
}) as boolean

const {
blockMetadataInputLevelFields,
error: errorBlockMetadataFields,
isLoading: isLoadingBlockMetadataFields
} = useGetBlockMetadataInputLevelFields({ blockName, metadataBlockInfoRepository })

const isCitation = blockName === MetadataBlockName.CITATION

const rules: UseControllerProps['rules'] = {}
Expand All @@ -67,11 +59,6 @@ export const MetadataInputLevelFieldsBlock = ({
})
}

// TODO: Temporary, this will change with get all metadata blocks info use case
if (isLoadingBlockMetadataFields || errorBlockMetadataFields || !blockMetadataInputLevelFields) {
return null
}

return (
<Stack direction="vertical" gap={2}>
<Stack direction="horizontal" gap={0}>
Expand Down Expand Up @@ -122,12 +109,12 @@ export const MetadataInputLevelFieldsBlock = ({
<InputLevelsTable
show={inputLevelsTableStatus.show}
disabled={inputLevelsTableStatus.asDisabled}
blockMetadataInputLevelFields={blockMetadataInputLevelFields}
blockMetadataInputLevelFields={reducedMetadataBlockInfo}
closeButton={
<CloseButton
onClick={handleHideInputLevelsTable}
aria-label="Hide input levels table"
tabIndex={inputLevelsTableStatus.show ? 1 : -1}
tabIndex={inputLevelsTableStatus.show ? 0 : -1}
/>
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { ChangeEvent, useId } from 'react'
import { Controller, UseControllerProps, useFormContext } from 'react-hook-form'
import { Form, Stack } from '@iqss/dataverse-design-system'
import { ReducedMetadataFieldInfo } from '../useGetBlockMetadataInputLevelFields'
import styles from './InputLevelsTable.module.scss'
import { CollectionFormInputLevelValue, INPUT_LEVELS_GROUPER } from '../../../CollectionForm'
import styles from './InputLevelsTable.module.scss'

interface InputLevelFieldRowProps {
metadataField: ReducedMetadataFieldInfo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { BlockMetadataInputLevelFields } from '../useGetBlockMetadataInputLevelFields'
import { ReactNode } from 'react'
import cn from 'classnames'
import styles from './InputLevelsTable.module.scss'
import React from 'react'
import { Table } from '@iqss/dataverse-design-system'
import { BlockMetadataInputLevelFields } from '../useGetBlockMetadataInputLevelFields'
import { InputLevelFieldRow } from './InputLevelFieldRow'
import styles from './InputLevelsTable.module.scss'

interface InputLevelsTableProps {
show: boolean
disabled: boolean
blockMetadataInputLevelFields: BlockMetadataInputLevelFields
closeButton: React.ReactNode
closeButton: ReactNode
}

export const InputLevelsTable = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import {
MetadataField
} from '../../metadata-block-info/domain/models/MetadataBlockInfo'
import { getMetadataBlockInfoByNameTemporal } from '../../metadata-block-info/domain/useCases/getMetadataBlockInfoByNameTemporal'
// import { MetadataBlockInfoRepository } from '../../../../../metadata-block-info/domain/repositories/MetadataBlockInfoRepository'
// import {
// MetadataBlockInfo,
// MetadataBlockName,
// MetadataField
// } from '../../../../../metadata-block-info/domain/models/MetadataBlockInfo'
// import { getMetadataBlockInfoByNameTemporal } from '../../../../../metadata-block-info/domain/useCases/getMetadataBlockInfoByNameTemporal'

interface Props {
metadataBlockInfoRepository: MetadataBlockInfoRepository
Expand Down Expand Up @@ -100,27 +93,6 @@ export const useGetAllMetadataBlocksInfoByName = ({
}
)

// #region Temporal
// const citationBlock: MetadataBlockInfo = reducedMetadataBlocksInfo.find(
// (block) => block.name === MetadataBlockName.CITATION
// )
// const geospatialBlock: MetadataBlockInfo = reducedMetadataBlocksInfo.find(
// (block) => block.name === MetadataBlockName.GEOSPATIAL
// )
// const socialScienceBlock: MetadataBlockInfo = reducedMetadataBlocksInfo.find(
// (block) => block.name === MetadataBlockName.SOCIAL_SCIENCE
// )
// const astrophysicsBlock: MetadataBlockInfo = reducedMetadataBlocksInfo.find(
// (block) => block.name === MetadataBlockName.ASTROPHYSICS
// )
// const biomedicalBlock: MetadataBlockInfo = reducedMetadataBlocksInfo.find(
// (block) => block.name === MetadataBlockName.BIOMEDICAL
// )
// const journalBlock: MetadataBlockInfo = reducedMetadataBlocksInfo.find(
// (block) => block.name === MetadataBlockName.JOURNAL
// )
// #endregion

setAllMetadataBlocksInfo(reducedMetadataBlocksInfo)
} catch (err) {
const errorMessage =
Expand Down

0 comments on commit 3b07a60

Please sign in to comment.