@@ -19,6 +19,9 @@ import { type Template } from '@/templates/domain/models/Template'
1919import { DatasetTemplateSelect } from './dataset-template-select/DatasetTemplateSelect'
2020import { TemplateRepository } from '@/templates/domain/repositories/TemplateRepository'
2121import { useCollectionRepositories } from '@/shared/contexts/repositories/RepositoriesProvider'
22+ import { useGetAvailableDatasetTypes } from '@/dataset/domain/hooks/useGetAvailableDatasetTypes'
23+ import { DatasetType } from '@/dataset/domain/models/DatasetType'
24+ import { DatasetTypeSelect } from './dataset-type-select/DatasetTypeSelect'
2225
2326interface CreateDatasetProps {
2427 datasetRepository : DatasetRepository
@@ -38,12 +41,15 @@ export function CreateDataset({
3841 const { isModalOpen, hideModal } = useNotImplementedModal ( )
3942 const { setIsLoading } = useLoading ( )
4043 const [ selectedTemplate , setSelectedTemplate ] = useState < Template | null > ( null )
44+ const [ selectedDatasetType , setSelectedDatasetType ] = useState < DatasetType | null > ( null )
4145
4246 const { collection, isLoading : isLoadingCollection } = useCollection (
4347 collectionRepository ,
4448 collectionId
4549 )
4650
51+ const { datasetTypes, isLoading : isLoadingDatasetTypes } = useGetAvailableDatasetTypes ( { datasetRepository } )
52+
4753 const { collectionUserPermissions, isLoading : isLoadingCollectionUserPermissions } =
4854 useGetCollectionUserPermissions ( {
4955 collectionIdOrAlias : collectionId ,
@@ -63,13 +69,33 @@ export function CreateDataset({
6369 setSelectedTemplate ( template )
6470 }
6571
72+ const handleDatasetTypeChange = ( selectedTypeId : string ) => {
73+ const type : DatasetType | null =
74+ datasetTypes . find ( ( type ) => type . id === Number ( selectedTypeId ) ) || null
75+
76+ setSelectedType ( type )
77+ }
78+
6679 const isLoadingData =
67- isLoadingCollectionUserPermissions || isLoadingCollection || isLoadingDatasetTemplates
80+ isLoadingCollectionUserPermissions ||
81+ isLoadingCollection ||
82+ isLoadingDatasetTemplates ||
83+ isLoadingDatasetTypes
6884
6985 useEffect ( ( ) => {
7086 setIsLoading ( isLoadingData )
7187 } , [ isLoadingData , setIsLoading ] )
7288
89+ // When dataset types are loaded we set the default one to DATASET if available, it should always be there
90+ useEffect ( ( ) => {
91+ if ( datasetTypes . length > 0 ) {
92+ const defaultType : DatasetType | null =
93+ datasetTypes . find ( ( type ) => type . name === 'dataset' ) || null
94+
95+ setSelectedType ( defaultType )
96+ }
97+ } , [ datasetTypes ] )
98+
7399 // When dataset templates are loaded we set the default one if any
74100 useEffect ( ( ) => {
75101 if ( datasetTemplates . length > 0 ) {
@@ -88,6 +114,11 @@ export function CreateDataset({
88114 return < CreateDatasetSkeleton />
89115 }
90116
117+ // We use the template id and dataset type id as key to force remounting the form when the template or type changes
118+ const formKey = `${ selectedType ? selectedType . id : 'no-type' } --${
119+ selectedTemplate ? selectedTemplate . id : 'no-template'
120+ } `
121+
91122 if ( collectionUserPermissions && ! canUserAddDataset ) {
92123 return (
93124 < div className = "pt-4" data-testid = "not-allowed-to-create-dataset-alert" >
@@ -120,13 +151,23 @@ export function CreateDataset({
120151 />
121152 ) }
122153
154+ { /* Show the dataset type selector only if there's more than one dataset type (besides 'dataset') */ }
155+ { datasetTypes . length > 1 && selectedType && (
156+ < DatasetTypeSelect
157+ datasetTypes = { datasetTypes }
158+ onChange = { handleDatasetTypeChange }
159+ selectedType = { selectedType }
160+ />
161+ ) }
162+
123163 < DatasetMetadataForm
124164 mode = "create"
125165 collectionId = { collectionId }
126166 datasetRepository = { datasetRepository }
127167 metadataBlockInfoRepository = { metadataBlockInfoRepository }
128168 datasetTemplate = { selectedTemplate ?? undefined }
129- key = { selectedTemplate ? selectedTemplate . id : 'no-template-selected' } // We use the template id as key to force remounting the form when the template changes
169+ datasetTypeName = { selectedType ? selectedType . name : undefined }
170+ key = { formKey }
130171 />
131172 </ section >
132173 </ >
0 commit comments