mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
fix file validation on edit form (#1677)
* split validation / constants * some minor refactor * fix edit form (duplication ErrorMessage) * fix Feedback UI * remove logs * fix empty space when loading total sales * added isTrue to valid * remove hardcoded FileInfo typing in publish and edit * fix more FileInfo typing * fix missing error message on inputs
This commit is contained in:
parent
e9ff108ea3
commit
74a9d074ed
@ -71,9 +71,7 @@ function checkError(
|
|||||||
parsedFieldName: string[],
|
parsedFieldName: string[],
|
||||||
field: FieldInputProps<any>
|
field: FieldInputProps<any>
|
||||||
) {
|
) {
|
||||||
if (form?.errors && Object.entries(form?.errors).length === 0) {
|
if (
|
||||||
return false
|
|
||||||
} else if (
|
|
||||||
(form?.touched?.[parsedFieldName[0]]?.[parsedFieldName[1]] &&
|
(form?.touched?.[parsedFieldName[0]]?.[parsedFieldName[1]] &&
|
||||||
form?.errors?.[parsedFieldName[0]]?.[parsedFieldName[1]]) ||
|
form?.errors?.[parsedFieldName[0]]?.[parsedFieldName[1]]) ||
|
||||||
(form?.touched[field.name] &&
|
(form?.touched[field.name] &&
|
||||||
@ -140,11 +138,13 @@ export default function Input(props: Partial<InputProps>): ReactElement {
|
|||||||
</Label>
|
</Label>
|
||||||
<InputElement size={size} {...field} {...props} />
|
<InputElement size={size} {...field} {...props} />
|
||||||
{help && prominentHelp && <FormHelp>{help}</FormHelp>}
|
{help && prominentHelp && <FormHelp>{help}</FormHelp>}
|
||||||
{isFormikField && hasFormikError && (
|
|
||||||
|
{field?.name !== 'files' && isFormikField && hasFormikError && (
|
||||||
<div className={styles.error}>
|
<div className={styles.error}>
|
||||||
<ErrorMessage name={field.name} />
|
<ErrorMessage name={field.name} />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{disclaimer && (
|
{disclaimer && (
|
||||||
<Disclaimer visible={disclaimerVisible}>{disclaimer}</Disclaimer>
|
<Disclaimer visible={disclaimerVisible}>{disclaimer}</Disclaimer>
|
||||||
)}
|
)}
|
||||||
|
@ -52,7 +52,7 @@ export default function Conversion({
|
|||||||
setPriceConverted(convertedFormattedHTMLstring)
|
setPriceConverted(convertedFormattedHTMLstring)
|
||||||
}, [price, prices, currency, locale, isFiat, priceTokenId])
|
}, [price, prices, currency, locale, isFiat, priceTokenId])
|
||||||
|
|
||||||
return price > 0 ? (
|
return Number(price) >= 0 ? (
|
||||||
<span
|
<span
|
||||||
className={`${styles.conversion} ${className || ''}`}
|
className={`${styles.conversion} ${className || ''}`}
|
||||||
title="Approximation based on the current spot price on Coingecko"
|
title="Approximation based on the current spot price on Coingecko"
|
||||||
|
@ -12,10 +12,8 @@ import { useUserPreferences } from '@context/UserPreferences'
|
|||||||
import styles from './index.module.css'
|
import styles from './index.module.css'
|
||||||
import Web3Feedback from '@shared/Web3Feedback'
|
import Web3Feedback from '@shared/Web3Feedback'
|
||||||
import { useCancelToken } from '@hooks/useCancelToken'
|
import { useCancelToken } from '@hooks/useCancelToken'
|
||||||
import {
|
import { getComputeSettingsInitialValues } from './_constants'
|
||||||
getComputeSettingsInitialValues,
|
import { computeSettingsValidationSchema } from './_validation'
|
||||||
computeSettingsValidationSchema
|
|
||||||
} from './_constants'
|
|
||||||
import content from '../../../../content/pages/editComputeDataset.json'
|
import content from '../../../../content/pages/editComputeDataset.json'
|
||||||
import { getServiceByName } from '@utils/ddo'
|
import { getServiceByName } from '@utils/ddo'
|
||||||
import { setMinterToPublisher, setMinterToDispenser } from '@utils/dispenser'
|
import { setMinterToPublisher, setMinterToDispenser } from '@utils/dispenser'
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
|
||||||
.feedback h3 {
|
.feedback h3 {
|
||||||
|
@ -7,7 +7,8 @@ import {
|
|||||||
Asset,
|
Asset,
|
||||||
Service
|
Service
|
||||||
} from '@oceanprotocol/lib'
|
} from '@oceanprotocol/lib'
|
||||||
import { validationSchema, getInitialValues } from './_constants'
|
import { validationSchema } from './_validation'
|
||||||
|
import { getInitialValues } from './_constants'
|
||||||
import { MetadataEditForm } from './_types'
|
import { MetadataEditForm } from './_types'
|
||||||
import { useWeb3 } from '@context/Web3'
|
import { useWeb3 } from '@context/Web3'
|
||||||
import { useUserPreferences } from '@context/UserPreferences'
|
import { useUserPreferences } from '@context/UserPreferences'
|
||||||
|
@ -1,20 +1,7 @@
|
|||||||
import { FileInfo, Metadata, ServiceComputeOptions } from '@oceanprotocol/lib'
|
import { Metadata, ServiceComputeOptions } from '@oceanprotocol/lib'
|
||||||
import { secondsToString } from '@utils/ddo'
|
import { secondsToString } from '@utils/ddo'
|
||||||
import * as Yup from 'yup'
|
|
||||||
import { ComputeEditForm, MetadataEditForm } from './_types'
|
import { ComputeEditForm, MetadataEditForm } from './_types'
|
||||||
|
|
||||||
export const validationSchema = Yup.object().shape({
|
|
||||||
name: Yup.string()
|
|
||||||
.min(4, (param) => `Title must be at least ${param.min} characters`)
|
|
||||||
.required('Required'),
|
|
||||||
description: Yup.string().required('Required').min(10),
|
|
||||||
price: Yup.number().required('Required'),
|
|
||||||
links: Yup.array<any[]>().nullable(),
|
|
||||||
files: Yup.array<FileInfo[]>().nullable(),
|
|
||||||
timeout: Yup.string().required('Required'),
|
|
||||||
author: Yup.string().nullable()
|
|
||||||
})
|
|
||||||
|
|
||||||
export function getInitialValues(
|
export function getInitialValues(
|
||||||
metadata: Metadata,
|
metadata: Metadata,
|
||||||
timeout: number,
|
timeout: number,
|
||||||
@ -24,19 +11,13 @@ export function getInitialValues(
|
|||||||
name: metadata?.name,
|
name: metadata?.name,
|
||||||
description: metadata?.description,
|
description: metadata?.description,
|
||||||
price,
|
price,
|
||||||
links: metadata?.links,
|
links: metadata?.links as any,
|
||||||
files: '',
|
files: [{ url: '', type: '' }],
|
||||||
timeout: secondsToString(timeout),
|
timeout: secondsToString(timeout),
|
||||||
author: metadata?.author
|
author: metadata?.author
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const computeSettingsValidationSchema = Yup.object().shape({
|
|
||||||
allowAllPublishedAlgorithms: Yup.boolean().nullable(),
|
|
||||||
publisherTrustedAlgorithms: Yup.array().nullable(),
|
|
||||||
publisherTrustedAlgorithmPublishers: Yup.array().nullable()
|
|
||||||
})
|
|
||||||
|
|
||||||
export function getComputeSettingsInitialValues({
|
export function getComputeSettingsInitialValues({
|
||||||
publisherTrustedAlgorithms,
|
publisherTrustedAlgorithms,
|
||||||
publisherTrustedAlgorithmPublishers
|
publisherTrustedAlgorithmPublishers
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
|
import { FileInfo } from '@oceanprotocol/lib'
|
||||||
export interface MetadataEditForm {
|
export interface MetadataEditForm {
|
||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
timeout: string
|
timeout: string
|
||||||
price?: string
|
price?: string
|
||||||
links?: string | any[]
|
files: FileInfo[]
|
||||||
files: string | any[]
|
links?: FileInfo[]
|
||||||
author?: string
|
author?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
34
src/components/Asset/Edit/_validation.ts
Normal file
34
src/components/Asset/Edit/_validation.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { FileInfo } from '@oceanprotocol/lib'
|
||||||
|
import * as Yup from 'yup'
|
||||||
|
|
||||||
|
export const validationSchema = Yup.object().shape({
|
||||||
|
name: Yup.string()
|
||||||
|
.min(4, (param) => `Title must be at least ${param.min} characters`)
|
||||||
|
.required('Required'),
|
||||||
|
description: Yup.string().required('Required').min(10),
|
||||||
|
price: Yup.number().required('Required'),
|
||||||
|
files: Yup.array<FileInfo[]>()
|
||||||
|
.of(
|
||||||
|
Yup.object().shape({
|
||||||
|
url: Yup.string().url('Must be a valid URL.'),
|
||||||
|
valid: Yup.boolean().isTrue()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.nullable(),
|
||||||
|
links: Yup.array<FileInfo[]>()
|
||||||
|
.of(
|
||||||
|
Yup.object().shape({
|
||||||
|
url: Yup.string().url('Must be a valid URL.'),
|
||||||
|
valid: Yup.boolean().isTrue()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.nullable(),
|
||||||
|
timeout: Yup.string().required('Required'),
|
||||||
|
author: Yup.string().nullable()
|
||||||
|
})
|
||||||
|
|
||||||
|
export const computeSettingsValidationSchema = Yup.object().shape({
|
||||||
|
allowAllPublishedAlgorithms: Yup.boolean().nullable(),
|
||||||
|
publisherTrustedAlgorithms: Yup.array().nullable(),
|
||||||
|
publisherTrustedAlgorithmPublishers: Yup.array().nullable()
|
||||||
|
})
|
@ -72,8 +72,8 @@ export const initialValues: FormPublishData = {
|
|||||||
},
|
},
|
||||||
services: [
|
services: [
|
||||||
{
|
{
|
||||||
files: [{ url: '' }],
|
files: [{ url: '', type: '' }],
|
||||||
links: [{ url: '' }],
|
links: [{ url: '', type: '' }],
|
||||||
dataTokenOptions: { name: '', symbol: '' },
|
dataTokenOptions: { name: '', symbol: '' },
|
||||||
timeout: '',
|
timeout: '',
|
||||||
access: 'access',
|
access: 'access',
|
||||||
|
@ -1,14 +1,6 @@
|
|||||||
import { ServiceComputeOptions } from '@oceanprotocol/lib'
|
import { FileInfo, ServiceComputeOptions } from '@oceanprotocol/lib'
|
||||||
import { NftMetadata } from '@utils/nft'
|
import { NftMetadata } from '@utils/nft'
|
||||||
import { ReactElement } from 'react'
|
import { ReactElement } from 'react'
|
||||||
|
|
||||||
interface FileInfo {
|
|
||||||
url: string
|
|
||||||
valid?: boolean
|
|
||||||
contentLength?: string
|
|
||||||
contentType?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface FormPublishService {
|
export interface FormPublishService {
|
||||||
files: FileInfo[]
|
files: FileInfo[]
|
||||||
links?: FileInfo[]
|
links?: FileInfo[]
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { MAX_DECIMALS } from '@utils/constants'
|
import { MAX_DECIMALS } from '@utils/constants'
|
||||||
import * as Yup from 'yup'
|
import * as Yup from 'yup'
|
||||||
import { getMaxDecimalsValidation } from '@utils/numbers'
|
import { getMaxDecimalsValidation } from '@utils/numbers'
|
||||||
|
import { FileInfo } from '@oceanprotocol/lib'
|
||||||
|
|
||||||
// TODO: conditional validation
|
// TODO: conditional validation
|
||||||
// e.g. when algo is selected, Docker image is required
|
// e.g. when algo is selected, Docker image is required
|
||||||
@ -28,7 +29,7 @@ const validationMetadata = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const validationService = {
|
const validationService = {
|
||||||
files: Yup.array<{ url: string; valid: boolean }[]>()
|
files: Yup.array<FileInfo[]>()
|
||||||
.of(
|
.of(
|
||||||
Yup.object().shape({
|
Yup.object().shape({
|
||||||
url: Yup.string().url('Must be a valid URL.').required('Required'),
|
url: Yup.string().url('Must be a valid URL.').required('Required'),
|
||||||
@ -37,7 +38,7 @@ const validationService = {
|
|||||||
)
|
)
|
||||||
.min(1, `At least one file is required.`)
|
.min(1, `At least one file is required.`)
|
||||||
.required('Enter a valid URL and click ADD FILE.'),
|
.required('Enter a valid URL and click ADD FILE.'),
|
||||||
links: Yup.array<{ url: string; valid: boolean }[]>()
|
links: Yup.array<FileInfo[]>()
|
||||||
.of(
|
.of(
|
||||||
Yup.object().shape({
|
Yup.object().shape({
|
||||||
url: Yup.string().url('Must be a valid URL.'),
|
url: Yup.string().url('Must be a valid URL.'),
|
||||||
|
Loading…
Reference in New Issue
Block a user