Commit e665c1da authored by Stefan Probst's avatar Stefan Probst
Browse files

feat: add initial recommended fields in create item form

parent e04d7ae0
Pipeline #218464 passed with stages
in 8 minutes and 13 seconds
......@@ -2,7 +2,7 @@
"tool": {
"required": ["label", "description"],
"recommended": [
"actors",
"contributors",
"accessibleAt",
"externalIds",
"media",
......@@ -32,7 +32,7 @@
"trainingMaterial": {
"required": ["label", "description"],
"recommended": [
"actors",
"contributors",
"accessibleAt",
"externalIds",
"media",
......@@ -58,7 +58,7 @@
"publication": {
"required": ["label", "description"],
"recommended": [
"actors",
"contributors",
"accessibleAt",
"externalIds",
"media",
......@@ -90,7 +90,7 @@
"dataset": {
"required": ["label", "description"],
"recommended": [
"actors",
"contributors",
"accessibleAt",
"externalIds",
"media",
......@@ -116,7 +116,7 @@
"workflow": {
"required": ["label", "description"],
"recommended": [
"actors",
"contributors",
"externalIds",
"media",
"thumbnail",
......
......@@ -12,6 +12,8 @@ import type {
WorkflowCore,
WorkflowDto,
} from '@/api/sshoc'
import type { Item } from '@/api/sshoc/types'
import allowedPropertyTypes from '@/utils/propertyTypes.preval'
export function convertToInitialFormValues(item: DatasetDto): DatasetCore
export function convertToInitialFormValues(
......@@ -135,3 +137,47 @@ export function convertToInitialFormValues(
return initialValues
}
/**
* Populates recommended form fields with empty values.
*/
export function createInitialRecommendedFields({
required,
recommended,
recommendedProperties,
}: {
required: Array<string>
recommended: Array<string>
recommendedProperties: Array<string>
}): Record<string, unknown> {
const fieldTypes: Partial<Item> = {
label: '',
version: '',
description: '',
contributors: [{ role: { code: 'contributor' } }],
accessibleAt: [''],
externalIds: [{}],
media: [{}],
thumbnail: {},
relatedItems: [{}],
}
const initialValues: any = {}
;(required as Array<keyof Item>).forEach((fieldName) => {
initialValues[fieldName] = fieldTypes[fieldName]
})
;(recommended as Array<keyof Item>).forEach((fieldName) => {
initialValues[fieldName] = fieldTypes[fieldName]
})
initialValues.properties = []
recommendedProperties
.filter((id) => allowedPropertyTypes.includes(id))
.forEach((id) => {
initialValues.properties.push({ type: { code: id } })
})
return initialValues
}
import { Fragment } from 'react'
import { createInitialRecommendedFields } from '@/api/sshoc/helpers'
import { ItemForm } from '@/components/item/DatasetCreateForm/DatasetCreateForm'
import ContentColumn from '@/modules/layout/ContentColumn'
import GridLayout from '@/modules/layout/GridLayout'
import Metadata from '@/modules/metadata/Metadata'
import { Title } from '@/modules/ui/typography/Title'
import forms from '@@/config/forms.json'
/**
* Create dataset screen.
......@@ -19,7 +21,10 @@ export default function DatasetCreateScreen(): JSX.Element {
style={{ gridColumn: '4 / span 8' }}
>
<Title>Create dataset</Title>
<ItemForm category="dataset" initialValues={{}} />
<ItemForm
category="dataset"
initialValues={createInitialRecommendedFields(forms.dataset)}
/>
</ContentColumn>
</GridLayout>
</Fragment>
......
import { Fragment } from 'react'
import { createInitialRecommendedFields } from '@/api/sshoc/helpers'
import { ItemForm } from '@/components/item/PublicationCreateForm/PublicationCreateForm'
import ContentColumn from '@/modules/layout/ContentColumn'
import GridLayout from '@/modules/layout/GridLayout'
import Metadata from '@/modules/metadata/Metadata'
import { Title } from '@/modules/ui/typography/Title'
import forms from '@@/config/forms.json'
/**
* Create publication screen.
......@@ -19,7 +21,10 @@ export default function PublicationCreateScreen(): JSX.Element {
style={{ gridColumn: '4 / span 8' }}
>
<Title>Create publication</Title>
<ItemForm category="publication" initialValues={{}} />
<ItemForm
category="publication"
initialValues={createInitialRecommendedFields(forms.publication)}
/>
</ContentColumn>
</GridLayout>
</Fragment>
......
import { Fragment } from 'react'
import { createInitialRecommendedFields } from '@/api/sshoc/helpers'
import { ItemForm } from '@/components/item/ToolCreateForm/ToolCreateForm'
import ContentColumn from '@/modules/layout/ContentColumn'
import GridLayout from '@/modules/layout/GridLayout'
import Metadata from '@/modules/metadata/Metadata'
import { Title } from '@/modules/ui/typography/Title'
import forms from '@@/config/forms.json'
/**
* Create tool screen.
......@@ -19,7 +21,10 @@ export default function ToolCreateScreen(): JSX.Element {
style={{ gridColumn: '4 / span 8' }}
>
<Title>Create tool</Title>
<ItemForm category="tool-or-service" initialValues={{}} />
<ItemForm
category="tool-or-service"
initialValues={createInitialRecommendedFields(forms.tool)}
/>
</ContentColumn>
</GridLayout>
</Fragment>
......
import { Fragment } from 'react'
import { createInitialRecommendedFields } from '@/api/sshoc/helpers'
import { ItemForm } from '@/components/item/TrainingMaterialCreateForm/TrainingMaterialCreateForm'
import ContentColumn from '@/modules/layout/ContentColumn'
import GridLayout from '@/modules/layout/GridLayout'
import Metadata from '@/modules/metadata/Metadata'
import { Title } from '@/modules/ui/typography/Title'
import forms from '@@/config/forms.json'
/**
* Create training material screen.
......@@ -19,7 +21,12 @@ export default function TrainingMaterialCreateScreen(): JSX.Element {
style={{ gridColumn: '4 / span 8' }}
>
<Title>Create training material</Title>
<ItemForm category="training-material" initialValues={{}} />
<ItemForm
category="training-material"
initialValues={createInitialRecommendedFields(
forms.trainingMaterial,
)}
/>
</ContentColumn>
</GridLayout>
</Fragment>
......
import { Fragment } from 'react'
import { createInitialRecommendedFields } from '@/api/sshoc/helpers'
import { ItemForm } from '@/components/item/WorkflowCreateForm/WorkflowCreateForm'
import ContentColumn from '@/modules/layout/ContentColumn'
import GridLayout from '@/modules/layout/GridLayout'
import Metadata from '@/modules/metadata/Metadata'
import forms from '@@/config/forms.json'
/**
* Create workflow screen.
......@@ -17,7 +19,10 @@ export default function WorkflowCreateScreen(): JSX.Element {
className="px-6 py-12 space-y-12"
style={{ gridColumn: '4 / span 8' }}
>
<ItemForm category="workflow" initialValues={{}} />
<ItemForm
category="workflow"
initialValues={createInitialRecommendedFields(forms.workflow)}
/>
</ContentColumn>
</GridLayout>
</Fragment>
......
import preval from 'next-plugin-preval'
import { getPropertyTypes } from '@/api/sshoc'
/**
* Retrieves allowed property-types at build-time, so we can filter out
* property types which are unknown in a sshoc instance when populating
* initial recommended form fields.
*/
export default preval(
getPropertyTypes({}).then(({ propertyTypes }) => {
return propertyTypes!.map((propertyType) => propertyType.code)
}),
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment