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 { .hideUrl {
filter: blur(0.2rem); filter: blur(0.2rem);
user-select: none;
} }
.warning { .warning {

View File

@ -6,21 +6,21 @@ import { FileInfo as FileInfoData } from '@oceanprotocol/lib'
export default function FileInfo({ export default function FileInfo({
file, file,
handleClose, handleClose
hideUrl
}: { }: {
file: FileInfoData file: FileInfoData
handleClose(): void handleClose(): void
hideUrl?: boolean
}): ReactElement { }): ReactElement {
const contentTypeCleaned = file.contentType const contentTypeCleaned = file.contentType
? cleanupContentType(file.contentType) ? cleanupContentType(file.contentType)
: null : null
const hideUrl = file.type === 'hidden' || false
return ( return (
<div className={`${styles.info}`}> <div className={`${styles.info}`}>
<h3 className={`${styles.url} ${hideUrl ? styles.hideUrl : null}`}> <h3 className={`${styles.url} ${hideUrl ? styles.hideUrl : null}`}>
{file.url} {hideUrl ? 'https://oceanprotocol/placeholder' : file.url}
</h3> </h3>
<ul> <ul>
<li className={styles.success}> URL confirmed</li> <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 { useField, useFormikContext } from 'formik'
import FileInfo from './Info' import FileInfo from './Info'
import UrlInput from '../URLInput' import UrlInput from '../URLInput'
import { InputProps } from '@shared/FormInput' import { InputProps } from '@shared/FormInput'
import { getFileDidInfo, getFileUrlInfo } from '@utils/provider' import { getFileUrlInfo } from '@utils/provider'
import { FormPublishData } from 'src/components/Publish/_types' import { FormPublishData } from 'src/components/Publish/_types'
import { LoggerInstance } from '@oceanprotocol/lib' import { LoggerInstance } from '@oceanprotocol/lib'
import { useAsset } from '@context/Asset' import { useAsset } from '@context/Asset'
@ -11,7 +11,6 @@ import { useAsset } from '@context/Asset'
export default function FilesInput(props: InputProps): ReactElement { export default function FilesInput(props: InputProps): ReactElement {
const [field, meta, helpers] = useField(props.name) const [field, meta, helpers] = useField(props.name)
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)
const [hideUrl, setHideUrl] = useState(false)
const { values, setFieldError } = useFormikContext<FormPublishData>() const { values, setFieldError } = useFormikContext<FormPublishData>()
const { asset } = useAsset() const { asset } = useAsset()
@ -48,37 +47,11 @@ export default function FilesInput(props: InputProps): ReactElement {
helpers.setValue(meta.initialValue) 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 ( return (
<> <>
{field?.value?.[0]?.valid === true ? ( {field?.value?.[0]?.valid === true ||
<FileInfo field?.value?.[0]?.type === 'hidden' ? (
hideUrl={hideUrl} <FileInfo file={field.value[0]} handleClose={handleClose} />
file={field.value[0]}
handleClose={handleClose}
/>
) : ( ) : (
<UrlInput <UrlInput
submitText="Validate" submitText="Validate"

View File

@ -1,8 +1,10 @@
import React, { ReactElement } from 'react' import React, { ReactElement, useEffect } from 'react'
import { Field, Form } from 'formik' import { Field, Form, useField, useFormikContext } from 'formik'
import Input, { InputProps } from '@shared/FormInput' import Input, { InputProps } from '@shared/FormInput'
import FormActions from './FormActions' import FormActions from './FormActions'
import { useAsset } from '@context/Asset' import { useAsset } from '@context/Asset'
import { FormPublishData } from 'src/components/Publish/_types'
import { getFileUrlInfo } from '@utils/provider'
export function checkIfTimeoutInPredefinedValues( export function checkIfTimeoutInPredefinedValues(
timeout: string, timeout: string,
@ -23,7 +25,8 @@ export default function FormEditMetadata({
showPrice: boolean showPrice: boolean
isComputeDataset: boolean isComputeDataset: boolean
}): ReactElement { }): 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, // This component is handled by Formik so it's not rendered like a "normal" react component,
// so handleTimeoutCustomOption is called only once. // so handleTimeoutCustomOption is called only once.
@ -41,6 +44,34 @@ export default function FormEditMetadata({
timeoutOptionsArray.push('Forever') 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 ( return (
<Form> <Form>
{data.map( {data.map(