Skip to content

Commit

Permalink
Merge pull request IQSS#399 from IQSS/feature/340-collection-host-input
Browse files Browse the repository at this point in the history
Feature/340 collection host input
  • Loading branch information
GPortas authored May 22, 2024
2 parents 2827410 + 0bb0424 commit 0a4d267
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 33 deletions.
6 changes: 6 additions & 0 deletions public/locales/en/createDataset.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"title": "Metadata Tip",
"content": "After adding the dataset, click the Edit Dataset button to add more metadata."
},
"hostCollection": {
"label": "Host Collection",
"description": "The collection which contains this data.",
"helpText": "Changing the host collection will clear any fields you may have entered data into.",
"buttonLabel": "Edit Host Collection"
},
"requiredFields": "Asterisks indicate required fields",
"validationAlert": {
"title": "Validation Error",
Expand Down
2 changes: 1 addition & 1 deletion src/dataset/domain/repositories/DatasetRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DatasetsWithCount } from '../models/DatasetsWithCount'
export interface DatasetRepository {
getByPersistentId: (persistentId: string, version?: string) => Promise<Dataset | undefined>
getByPrivateUrlToken: (privateUrlToken: string) => Promise<Dataset | undefined>
create: (dataset: DatasetDTO) => Promise<{ persistentId: string }>
create: (dataset: DatasetDTO, collectionId?: string) => Promise<{ persistentId: string }>
getAllWithCount: (
collectionId: string,
paginationInfo: DatasetPaginationInfo
Expand Down
5 changes: 3 additions & 2 deletions src/dataset/domain/useCases/createDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { DatasetDTO } from './DTOs/DatasetDTO'

export function createDataset(
datasetRepository: DatasetRepository,
dataset: DatasetDTO
dataset: DatasetDTO,
collectionId: string
): Promise<{ persistentId: string }> {
return datasetRepository.create(dataset).catch((error: Error) => {
return datasetRepository.create(dataset, collectionId).catch((error: Error) => {
throw new Error(error.message)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { JSDatasetPreviewMapper } from '../mappers/JSDatasetPreviewMapper'
import { DatasetDTO } from '../../domain/useCases/DTOs/DatasetDTO'
import { DatasetDTOMapper } from '../mappers/DatasetDTOMapper'
import { DatasetsWithCount } from '../../domain/models/DatasetsWithCount'

const defaultCollectionId = 'root'
const includeDeaccessioned = true
type DatasetDetails = [JSDataset, string[], string, JSDatasetPermissions, JSDatasetLock[]]

Expand Down Expand Up @@ -180,9 +180,12 @@ export class DatasetJSDataverseRepository implements DatasetRepository {
})
}

create(dataset: DatasetDTO): Promise<{ persistentId: string }> {
create(
dataset: DatasetDTO,
collectionId = defaultCollectionId
): Promise<{ persistentId: string }> {
return createDataset
.execute(DatasetDTOMapper.toJSDatasetDTO(dataset))
.execute(DatasetDTOMapper.toJSDatasetDTO(dataset), collectionId)
.then((jsDatasetIdentifiers: JSDatasetIdentifiers) => ({
persistentId: jsDatasetIdentifiers.persistentId
}))
Expand Down
43 changes: 26 additions & 17 deletions src/sections/create-dataset/CreateDataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { type MetadataBlockInfoRepository } from '../../metadata-block-info/doma
import { SeparationLine } from '../shared/layout/SeparationLine/SeparationLine'
import { DatasetForm } from './DatasetForm'
import { DatasetFormSkeleton } from './DatasetFormSkeleton'
import { HostCollectionForm } from './HostCollectionForm/HostCollectionForm'
import { NotImplementedModal } from '../not-implemented/NotImplementedModal'
import { useNotImplementedModal } from '../not-implemented/NotImplementedModalContext'

interface CreateDatasetProps {
repository: DatasetRepository
Expand All @@ -22,7 +25,7 @@ export function CreateDataset({
}: CreateDatasetProps) {
const { t } = useTranslation('createDataset')
const { isLoading, setIsLoading } = useLoading()

const { isModalOpen, hideModal } = useNotImplementedModal()
const {
metadataBlocks,
isLoading: isLoadingMetadataBlocksConfiguration,
Expand All @@ -41,21 +44,27 @@ export function CreateDataset({
}, [isLoading, isLoadingMetadataBlocksConfiguration])

return (
<article>
<header>
<h1>{t('pageTitle')}</h1>
</header>
<SeparationLine />
{isLoadingMetadataBlocksConfiguration ? (
<DatasetFormSkeleton />
) : (
<DatasetForm
repository={repository}
metadataBlocks={metadataBlocks}
formDefaultValues={formDefaultValues}
errorLoadingMetadataBlocks={errorLoadingMetadataBlocksToRender}
/>
)}
</article>
<>
<NotImplementedModal show={isModalOpen} handleClose={hideModal} />
<article>
<header>
<h1>{t('pageTitle')}</h1>
</header>
<SeparationLine />
<HostCollectionForm collectionId={collectionId} />

{isLoadingMetadataBlocksConfiguration ? (
<DatasetFormSkeleton />
) : (
<DatasetForm
repository={repository}
collectionId={collectionId}
metadataBlocks={metadataBlocks}
formDefaultValues={formDefaultValues}
errorLoadingMetadataBlocks={errorLoadingMetadataBlocksToRender}
/>
)}
</article>
</>
)
}
7 changes: 6 additions & 1 deletion src/sections/create-dataset/CreateDatasetFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import { useSearchParams } from 'react-router-dom'
import { CreateDataset } from './CreateDataset'
import { DatasetJSDataverseRepository } from '../../dataset/infrastructure/repositories/DatasetJSDataverseRepository'
import { MetadataBlockInfoJSDataverseRepository } from '../../metadata-block-info/infrastructure/repositories/MetadataBlockInfoJSDataverseRepository'
import { NotImplementedModalProvider } from '../not-implemented/NotImplementedModalProvider'

const repository = new DatasetJSDataverseRepository()
const metadataBlockInfoRepository = new MetadataBlockInfoJSDataverseRepository()

export class CreateDatasetFactory {
static create(): ReactElement {
return <CreateDatasetWithSearchParams />
return (
<NotImplementedModalProvider>
<CreateDatasetWithSearchParams />
</NotImplementedModalProvider>
)
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/sections/create-dataset/DatasetForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import { Route } from '../Route.enum'

interface DatasetFormProps {
repository: DatasetRepository
collectionId?: string
metadataBlocks: MetadataBlockInfo[]
errorLoadingMetadataBlocks: string | null
formDefaultValues: CreateDatasetFormValues
}

export const DatasetForm = ({
repository,
collectionId = 'root',
metadataBlocks,
errorLoadingMetadataBlocks,
formDefaultValues
Expand All @@ -29,7 +31,7 @@ export const DatasetForm = ({
const { t } = useTranslation('createDataset')
const accordionRef = useRef<HTMLDivElement>(null)

const { submissionStatus, submitForm } = useCreateDatasetForm(repository)
const { submissionStatus, submitForm } = useCreateDatasetForm(repository, collectionId)

const isErrorLoadingMetadataBlocks = Boolean(errorLoadingMetadataBlocks)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.input-button-wrapper {
display: flex;
flex-direction: column;
gap: 1rem;

@media (min-width: 768px) {
flex-direction: row;
gap: 0;

button {
width: 100%;
}
}
}

.edit-button-wrapper {
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: flex-end;

@media (min-width: 768px) {
padding-left: 1rem;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Button, Col } from '@iqss/dataverse-design-system'
import { Form } from '@iqss/dataverse-design-system'
import { useTranslation } from 'react-i18next'
import { useNotImplementedModal } from '../../not-implemented/NotImplementedModalContext'
import styles from './HostCollectionForm.module.scss'

interface HostCollectionFormProps {
collectionId: string
}

export function HostCollectionForm({ collectionId }: HostCollectionFormProps) {
const { t } = useTranslation('createDataset')
const { showModal } = useNotImplementedModal()

return (
<>
<Form.Group controlId="host-collection">
<Form.Group.Label message={t('hostCollection.description')} column sm={3} required>
{t('hostCollection.label')}
</Form.Group.Label>
<Col sm={9} className={styles['input-button-wrapper']}>
<Col md={9}>
<Form.Group.Text>{t('hostCollection.helpText')}</Form.Group.Text>
<Form.Group.Input type="text" disabled defaultValue={collectionId} />
</Col>
<Col className={styles['edit-button-wrapper']} md={3}>
<Button type={'button'} onClick={showModal} variant="secondary">
{t('hostCollection.buttonLabel')}
</Button>
</Col>
</Col>
</Form.Group>
</>
)
}
7 changes: 5 additions & 2 deletions src/sections/create-dataset/useCreateDatasetForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export enum SubmissionStatus {
Errored = 'Errored'
}

export function useCreateDatasetForm(repository: DatasetRepository): {
export function useCreateDatasetForm(
repository: DatasetRepository,
collectionId: string
): {
submissionStatus: SubmissionStatus
submitForm: (formData: CreateDatasetFormValues) => void
} {
Expand All @@ -30,7 +33,7 @@ export function useCreateDatasetForm(repository: DatasetRepository): {
formDataBackToOriginalKeys
)

createDataset(repository, formattedFormValues)
createDataset(repository, formattedFormValues, collectionId)
.then(({ persistentId }) => {
setSubmissionStatus(SubmissionStatus.SubmitComplete)
navigate(`${Route.DATASETS}?persistentId=${persistentId}`, {
Expand Down
11 changes: 7 additions & 4 deletions src/stories/create-dataset/DatasetCreate.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DatasetMockRepository } from '../dataset/DatasetMockRepository'
import { MetadataBlockInfoMockRepository } from './MetadataBlockInfoMockRepository'
import { MetadataBlockInfoMockLoadingRepository } from './MetadataBlockInfoMockLoadingRepository'
import { MetadataBlockInfoMockErrorRepository } from './MetadataBlockInfoMockErrorRepository'
import { NotImplementedModalProvider } from '../../sections/not-implemented/NotImplementedModalProvider'

const meta: Meta<typeof CreateDataset> = {
title: 'Pages/Create Dataset',
Expand All @@ -17,10 +18,12 @@ type Story = StoryObj<typeof CreateDataset>

export const Default: Story = {
render: () => (
<CreateDataset
repository={new DatasetMockRepository()}
metadataBlockInfoRepository={new MetadataBlockInfoMockRepository()}
/>
<NotImplementedModalProvider>
<CreateDataset
repository={new DatasetMockRepository()}
metadataBlockInfoRepository={new MetadataBlockInfoMockRepository()}
/>
</NotImplementedModalProvider>
)
}

Expand Down
22 changes: 20 additions & 2 deletions tests/component/sections/create-dataset/CreateDataset.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DatasetRepository } from '../../../../src/dataset/domain/repositories/D
import { MetadataBlockInfoRepository } from '../../../../src/metadata-block-info/domain/repositories/MetadataBlockInfoRepository'
import { MetadataBlockInfoMother } from '../../metadata-block-info/domain/models/MetadataBlockInfoMother'
import { TypeMetadataFieldOptions } from '../../../../src/metadata-block-info/domain/models/MetadataBlockInfo'
import { NotImplementedModalProvider } from '../../../../src/sections/not-implemented/NotImplementedModalProvider'

const datasetRepository: DatasetRepository = {} as DatasetRepository
const metadataBlockInfoRepository: MetadataBlockInfoRepository = {} as MetadataBlockInfoRepository
Expand Down Expand Up @@ -63,7 +64,25 @@ describe('Create Dataset', () => {
.stub()
.resolves(collectionMetadataBlocksInfo)
})

it('renders the Host Collection Form', () => {
cy.customMount(
<NotImplementedModalProvider>
<CreateDataset
repository={datasetRepository}
collectionId={'test-collectionId'}
metadataBlockInfoRepository={metadataBlockInfoRepository}
/>
</NotImplementedModalProvider>
)
cy.findByText(/^Host Collection/i).should('exist')
cy.findByDisplayValue('test-collectionId').should('exist')
cy.findByText(/^Edit Host Collection/i)
.should('exist')
.click()
.then(() => {
cy.findByText('Not Implemented').should('exist')
})
})
it('renders the Create Dataset page and its metadata blocks sections', () => {
cy.customMount(
<CreateDataset
Expand All @@ -72,7 +91,6 @@ describe('Create Dataset', () => {
/>
)
cy.findByText(/Create Dataset/i).should('exist')

cy.findByText(/Citation Metadata/i).should('exist')
cy.findByText(/Astronomy and Astrophysics Metadata/i).should('exist')
})
Expand Down

0 comments on commit 0a4d267

Please sign in to comment.