2021-11-15 19:29:20 +01:00
import Input from '@shared/FormInput'
2021-10-28 12:33:29 +02:00
import { Field , useFormikContext } from 'formik'
2021-11-15 13:44:27 +01:00
import React , { ReactElement , useEffect } from 'react'
2021-10-25 15:26:00 +02:00
import IconDownload from '@images/download.svg'
import IconCompute from '@images/compute.svg'
2021-11-01 15:42:55 +01:00
import content from '../../../../content/publish/form.json'
2023-09-07 16:14:27 +02:00
import consumerParametersContent from '../../../../content/publish/consumerParameters.json'
2022-06-27 11:15:45 +02:00
import { getFieldContent } from '@utils/form'
2021-11-01 15:42:55 +01:00
import { FormPublishData } from '../_types'
2022-06-08 10:46:46 +02:00
import Alert from '@shared/atoms/Alert'
import { useMarketMetadata } from '@context/MarketMetadata'
import styles from '../index.module.css'
2021-10-28 12:33:29 +02:00
const accessTypeOptionsTitles = getFieldContent (
'access' ,
content . services . fields
) . options
2021-10-25 15:26:00 +02:00
export default function ServicesFields ( ) : ReactElement {
2022-06-08 10:46:46 +02:00
const { siteContent } = useMarketMetadata ( )
2021-10-28 12:33:29 +02:00
// connect with Form state, use for conditional field rendering
2022-04-15 15:28:45 +02:00
const { values , setFieldValue } = useFormikContext < FormPublishData > ( )
2021-11-15 13:44:27 +01:00
2021-12-21 21:26:08 +01:00
// name and title should be download, but option value should be access, probably the best way would be to change the component so that option is an object like {name,value}
2021-11-15 16:19:50 +01:00
const accessTypeOptions = [
{
2021-12-21 21:26:08 +01:00
name : 'download' ,
2021-12-23 13:36:22 +01:00
value : accessTypeOptionsTitles [ 0 ] . toLowerCase ( ) ,
2021-12-21 21:26:08 +01:00
title : 'Download' ,
2021-11-15 16:19:50 +01:00
icon : < IconDownload / > ,
// BoxSelection component is not a Formik component
// so we need to handle checked state manually.
checked :
values . services [ 0 ] . access === accessTypeOptionsTitles [ 0 ] . toLowerCase ( )
} ,
{
name : accessTypeOptionsTitles [ 1 ] . toLowerCase ( ) ,
2021-12-23 13:36:22 +01:00
value : accessTypeOptionsTitles [ 1 ] . toLowerCase ( ) ,
2021-11-15 16:19:50 +01:00
title : accessTypeOptionsTitles [ 1 ] ,
icon : < IconCompute / > ,
checked :
values . services [ 0 ] . access === accessTypeOptionsTitles [ 1 ] . toLowerCase ( )
}
]
2021-11-15 13:44:27 +01:00
// Auto-change access type based on algo privacy boolean.
// Could be also done later in transformPublishFormToDdo().
useEffect ( ( ) = > {
2022-03-16 13:52:05 +01:00
if (
values . services [ 0 ] . algorithmPrivacy === null ||
values . services [ 0 ] . algorithmPrivacy === undefined
)
return
2021-11-19 15:26:39 +01:00
2021-11-15 13:44:27 +01:00
setFieldValue (
'services[0].access' ,
2021-12-21 21:26:08 +01:00
values . services [ 0 ] . algorithmPrivacy === true ? 'compute' : 'access'
2021-11-15 13:44:27 +01:00
)
2021-11-24 13:29:14 +01:00
} , [ values . services [ 0 ] . algorithmPrivacy , setFieldValue ] )
2021-10-25 15:26:00 +02:00
return (
< >
2021-11-15 16:02:13 +01:00
< Field
{ . . . getFieldContent ( 'dataTokenOptions' , content . services . fields ) }
component = { Input }
name = "services[0].dataTokenOptions"
/ >
2021-11-15 13:44:27 +01:00
{ values . metadata . type === 'algorithm' ? (
< Field
{ . . . getFieldContent ( 'algorithmPrivacy' , content . services . fields ) }
component = { Input }
name = "services[0].algorithmPrivacy"
/ >
) : (
2022-06-08 10:46:46 +02:00
< >
< Field
{ . . . getFieldContent ( 'access' , content . services . fields ) }
component = { Input }
name = "services[0].access"
options = { accessTypeOptions }
/ >
< / >
2021-11-15 13:44:27 +01:00
) }
2021-11-01 17:35:50 +01:00
< Field
2021-11-11 08:51:13 +01:00
{ . . . getFieldContent ( 'providerUrl' , content . services . fields ) }
2021-11-01 17:35:50 +01:00
component = { Input }
2021-11-11 08:51:13 +01:00
name = "services[0].providerUrl"
2021-11-01 17:35:50 +01:00
/ >
2021-10-28 11:38:40 +02:00
< Field
{ . . . getFieldContent ( 'files' , content . services . fields ) }
component = { Input }
name = "services[0].files"
/ >
< Field
{ . . . getFieldContent ( 'links' , content . services . fields ) }
component = { Input }
name = "services[0].links"
/ >
< Field
{ . . . getFieldContent ( 'timeout' , content . services . fields ) }
component = { Input }
name = "services[0].timeout"
/ >
2023-09-07 16:14:27 +02:00
< Field
{ . . . getFieldContent ( 'usesConsumerParameters' , content . services . fields ) }
component = { Input }
name = "services[0].usesConsumerParameters"
/ >
{ values . services [ 0 ] . usesConsumerParameters && (
< Field
{ . . . getFieldContent (
'consumerParameters' ,
consumerParametersContent . consumerParameters . fields
) }
component = { Input }
name = "services[0].consumerParameters"
/ >
) }
2021-10-25 15:26:00 +02:00
< / >
)
}