Commit 19a89698 authored by Stefan Probst's avatar Stefan Probst
Browse files

fix: filter out nulls from arrays

parent d9260a7b
Pipeline #179326 canceled with stage
in 4 minutes and 39 seconds
......@@ -212,12 +212,14 @@ function CreateActorForm(props: CreateActorFormProps) {
const queryClient = useQueryClient()
const toast = useToast()
function onSubmit(values: ActorFormValues) {
function onSubmit(unsanitized: ActorFormValues) {
if (auth.session?.accessToken === undefined) {
toast.error('Authentication required.')
return Promise.reject()
}
const values = sanitizeActorFormValues(unsanitized)
return createActor.mutateAsync(
[values, { token: auth.session.accessToken }],
{
......@@ -331,3 +333,10 @@ function CreateActorForm(props: CreateActorFormProps) {
</Form>
)
}
function sanitizeActorFormValues(values: ActorFormValues) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
values.affiliations = values.affiliations?.filter((v) => v != null)
return values
}
......@@ -12,6 +12,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
......@@ -87,18 +88,13 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
},
})
function onSubmit({ draft, ...values }: ItemFormValues) {
function onSubmit({ draft, ...unsanitized }: ItemFormValues) {
if (auth.session?.accessToken == null) {
toast.error('Authentication required.')
return Promise.reject()
}
/**
* Backend crashes with `source: {}`.
*/
if (values.source && values.source.id === undefined) {
delete values.source
}
const values = sanitizeFormValues(unsanitized)
return create.mutateAsync([
{ draft },
......
......@@ -12,6 +12,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
......@@ -89,18 +90,13 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
},
})
function onSubmit({ draft, ...values }: ItemFormValues) {
function onSubmit({ draft, ...unsanitized }: ItemFormValues) {
if (auth.session?.accessToken == null) {
toast.error('Authentication required.')
return Promise.reject()
}
/**
* Backend crashes with `source: {}`.
*/
if (values.source && values.source.id === undefined) {
delete values.source
}
const values = sanitizeFormValues(unsanitized)
return create.mutateAsync([
{ id },
......
......@@ -12,6 +12,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
......@@ -87,18 +88,13 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
},
})
function onSubmit({ draft, ...values }: ItemFormValues) {
function onSubmit({ draft, ...unsanitized }: ItemFormValues) {
if (auth.session?.accessToken == null) {
toast.error('Authentication required.')
return Promise.reject()
}
/**
* Backend crashes with `source: {}`.
*/
if (values.source && values.source.id === undefined) {
delete values.source
}
const values = sanitizeFormValues(unsanitized)
return create.mutateAsync([
{ draft },
......
......@@ -12,6 +12,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
......@@ -89,18 +90,13 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
},
})
function onSubmit({ draft, ...values }: ItemFormValues) {
function onSubmit({ draft, ...unsanitized }: ItemFormValues) {
if (auth.session?.accessToken == null) {
toast.error('Authentication required.')
return Promise.reject()
}
/**
* Backend crashes with `source: {}`.
*/
if (values.source && values.source.id === undefined) {
delete values.source
}
const values = sanitizeFormValues(unsanitized)
return create.mutateAsync([
{ id },
......
......@@ -11,6 +11,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
......@@ -85,18 +86,13 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
},
})
function onSubmit({ draft, ...values }: ItemFormValues) {
function onSubmit({ draft, ...unsanitized }: ItemFormValues) {
if (auth.session?.accessToken == null) {
toast.error('Authentication required.')
return Promise.reject()
}
/**
* Backend crashes with `source: {}`.
*/
if (values.source && values.source.id === undefined) {
delete values.source
}
const values = sanitizeFormValues(unsanitized)
return create.mutateAsync([
{ draft },
......
......@@ -11,6 +11,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
......@@ -87,18 +88,13 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
},
})
function onSubmit({ draft, ...values }: ItemFormValues) {
function onSubmit({ draft, ...unsanitized }: ItemFormValues) {
if (auth.session?.accessToken == null) {
toast.error('Authentication required.')
return Promise.reject()
}
/**
* Backend crashes with `source: {}`.
*/
if (values.source && values.source.id === undefined) {
delete values.source
}
const values = sanitizeFormValues(unsanitized)
return create.mutateAsync([
{ id },
......
......@@ -11,6 +11,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
......@@ -85,18 +86,13 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
},
})
function onSubmit({ draft, ...values }: ItemFormValues) {
function onSubmit({ draft, ...unsanitized }: ItemFormValues) {
if (auth.session?.accessToken == null) {
toast.error('Authentication required.')
return Promise.reject()
}
/**
* Backend crashes with `source: {}`.
*/
if (values.source && values.source.id === undefined) {
delete values.source
}
const values = sanitizeFormValues(unsanitized)
return create.mutateAsync([
{ draft },
......
......@@ -11,6 +11,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { sanitizeFormValues } from '@/lib/sshoc/sanitizeFormValues'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
......@@ -87,18 +88,13 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
},
})
function onSubmit({ draft, ...values }: ItemFormValues) {
function onSubmit({ draft, ...unsanitized }: ItemFormValues) {
if (auth.session?.accessToken == null) {
toast.error('Authentication required.')
return Promise.reject()
}
/**
* Backend crashes with `source: {}`.
*/
if (values.source && values.source.id === undefined) {
delete values.source
}
const values = sanitizeFormValues(unsanitized)
return create.mutateAsync([
{ id },
......
import type {
DatasetCore,
PublicationCore,
StepCore,
ToolCore,
TrainingMaterialCore,
WorkflowCore,
} from '@/api/sshoc'
export function sanitizeFormValues<
T extends
| DatasetCore
| PublicationCore
| ToolCore
| TrainingMaterialCore
| WorkflowCore
| StepCore
>(values: T): T {
/**
* Backend crashes with `[null]`
*/
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
values.accessibleAt = values.accessibleAt?.filter((v) => v != null)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
values.contributors = values.contributors?.filter((v) => v != null)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
values.properties = values.properties?.filter((v) => v != null)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
values.relatedItems = values.relatedItems?.filter((v) => v != null)
/**
* Backend crashes with `source: {}`.
*/
if (values.source && values.source.id === undefined) {
delete values.source
}
return values
}
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