1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

refactor default values (files / links) edit form

This commit is contained in:
EnzoVezzaro 2022-10-17 09:41:39 -04:00
parent cf0c31fc7b
commit 52a69a1e37
4 changed files with 44 additions and 39 deletions

View File

@ -33,6 +33,7 @@
.hideUrl {
filter: blur(0.2rem);
user-select: none;
}
.warning {

View File

@ -6,21 +6,21 @@ import { FileInfo as FileInfoData } from '@oceanprotocol/lib'
export default function FileInfo({
file,
handleClose,
hideUrl
handleClose
}: {
file: FileInfoData
handleClose(): void
hideUrl?: boolean
}): ReactElement {
const contentTypeCleaned = file.contentType
? cleanupContentType(file.contentType)
: null
const hideUrl = file.type === 'hidden' || false
return (
<div className={`${styles.info}`}>
<h3 className={`${styles.url} ${hideUrl ? styles.hideUrl : null}`}>
{file.url}
{hideUrl ? 'https://oceanprotocol/placeholder' : file.url}
</h3>
<ul>
<li className={styles.success}> URL confirmed</li>

View File

@ -1,9 +1,9 @@
import React, { ReactElement, useEffect, useState } from 'react'
import React, { ReactElement, useState } from 'react'
import { useField, useFormikContext } from 'formik'
import FileInfo from './Info'
import UrlInput from '../URLInput'
import { InputProps } from '@shared/FormInput'
import { getFileDidInfo, getFileUrlInfo } from '@utils/provider'
import { getFileUrlInfo } from '@utils/provider'
import { FormPublishData } from 'src/components/Publish/_types'
import { LoggerInstance } from '@oceanprotocol/lib'
import { useAsset } from '@context/Asset'
@ -11,7 +11,6 @@ import { useAsset } from '@context/Asset'
export default function FilesInput(props: InputProps): ReactElement {
const [field, meta, helpers] = useField(props.name)
const [isLoading, setIsLoading] = useState(false)
const [hideUrl, setHideUrl] = useState(false)
const { values, setFieldError } = useFormikContext<FormPublishData>()
const { asset } = useAsset()
@ -48,37 +47,11 @@ export default function FilesInput(props: InputProps): ReactElement {
helpers.setValue(meta.initialValue)
}
useEffect(() => {
if (field.name === 'links' && asset?.metadata?.links) {
handleValidation(null, asset.metadata.links[0])
} else if (field.name === 'files') {
getFileDidInfo(
asset?.id,
asset?.services[0]?.id,
asset?.services[0]?.serviceEndpoint
).then((fileDidInfo) => {
setHideUrl(true)
// setting placeholder for url file to avoid txs for the url file during initializing
helpers.setValue([
{
url: fileDidInfo[0].valid
? 'http://oceanprotocol.com/placeholder'
: '',
...fileDidInfo[0]
}
])
})
}
}, [])
return (
<>
{field?.value?.[0]?.valid === true ? (
<FileInfo
hideUrl={hideUrl}
file={field.value[0]}
handleClose={handleClose}
/>
{field?.value?.[0]?.valid === true ||
field?.value?.[0]?.type === 'hidden' ? (
<FileInfo file={field.value[0]} handleClose={handleClose} />
) : (
<UrlInput
submitText="Validate"

View File

@ -1,8 +1,10 @@
import React, { ReactElement } from 'react'
import { Field, Form } from 'formik'
import React, { ReactElement, useEffect } from 'react'
import { Field, Form, useField, useFormikContext } from 'formik'
import Input, { InputProps } from '@shared/FormInput'
import FormActions from './FormActions'
import { useAsset } from '@context/Asset'
import { FormPublishData } from 'src/components/Publish/_types'
import { getFileUrlInfo } from '@utils/provider'
export function checkIfTimeoutInPredefinedValues(
timeout: string,
@ -23,7 +25,8 @@ export default function FormEditMetadata({
showPrice: boolean
isComputeDataset: boolean
}): ReactElement {
const { oceanConfig } = useAsset()
const { oceanConfig, asset } = useAsset()
const { values, setFieldValue } = useFormikContext<FormPublishData>()
// This component is handled by Formik so it's not rendered like a "normal" react component,
// so handleTimeoutCustomOption is called only once.
@ -41,6 +44,34 @@ export default function FormEditMetadata({
timeoutOptionsArray.push('Forever')
}
useEffect(() => {
// let's initiate files with empty url (we can't access the asset url) with type hidden (for UI frontend)
setFieldValue('files', [
{
url: '',
type: 'hidden'
}
])
const providerUrl = values?.services
? values?.services[0].providerUrl.url
: asset.services[0].serviceEndpoint
// if we have a sample file, we need to get the files' info before setting defaults links value
asset?.metadata?.links?.[0] &&
getFileUrlInfo(asset.metadata.links[0], providerUrl).then(
(checkedFile) => {
console.log(checkedFile)
// initiate link with values from asset metadata
setFieldValue('links', [
{
url: asset.metadata.links[0],
...checkedFile[0]
}
])
}
)
}, [])
return (
<Form>
{data.map(