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

fix edit form validation (#1797)

* fix edit form validation

* more validation edit form
This commit is contained in:
EnzoVezzaro 2022-11-25 12:41:17 -04:00 committed by GitHub
parent f6d11e5e6f
commit aa8dcdaa79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 30 deletions

View File

@ -2,24 +2,37 @@ import { isCID } from '@utils/ipfs'
import isUrl from 'is-url-superb' import isUrl from 'is-url-superb'
import * as Yup from 'yup' import * as Yup from 'yup'
export function testLinks() { export function testLinks(isEdit?: boolean) {
return Yup.string().test((value, context) => { return Yup.string().test((value, context) => {
const { type } = context.parent const { type } = context.parent
let validField let validField
let errorMessage let errorMessage
switch (type) { switch (type) {
// we allow submit if the type input is hidden as will be ignore
case 'hidden':
validField = true
break
case 'url': case 'url':
validField = isUrl(value?.toString() || '') validField = isUrl(value?.toString() || '')
// if we're in publish, the field must be valid
if (!validField) { if (!validField) {
validField = false
errorMessage = 'Must be a valid url.' errorMessage = 'Must be a valid url.'
} else { }
// we allow submit if we're in the edit page and the field is empty
if (
(!value?.toString() && isEdit) ||
(!value?.toString() && context.path === 'services[0].links[0].url')
) {
validField = true
}
// if the url has google drive, we need to block the user from submit
if (value?.toString().includes('drive.google')) { if (value?.toString().includes('drive.google')) {
validField = false validField = false
errorMessage = errorMessage =
'Google Drive is not a supported hosting service. Please use an alternative.' 'Google Drive is not a supported hosting service. Please use an alternative.'
} }
}
break break
case 'ipfs': case 'ipfs':
validField = isCID(value?.toString()) validField = isCID(value?.toString())

View File

@ -25,6 +25,10 @@
border: 0; border: 0;
} }
.tabHidden {
display: none !important;
}
.tab, .tab,
.tab label { .tab label {
cursor: pointer; cursor: pointer;

View File

@ -55,11 +55,11 @@ export default function TabsFile({
<div className={styles.tabListContainer}> <div className={styles.tabListContainer}>
<TabList className={styles.tabList}> <TabList className={styles.tabList}>
{items.map((item, index) => { {items.map((item, index) => {
if (isHidden) return null
return ( return (
<Tab <Tab
className={styles.tab} className={`${styles.tab} ${
isHidden ? styles.tabHidden : null
}`}
key={`tab_${items[tabIndex].props.name}_${index}`} key={`tab_${items[tabIndex].props.name}_${index}`}
onClick={ onClick={
handleTabChange ? () => handleTabChange(item.title) : null handleTabChange ? () => handleTabChange(item.title) : null

View File

@ -47,12 +47,14 @@ export default function FormEditMetadata({
useEffect(() => { useEffect(() => {
// let's initiate files with empty url (we can't access the asset url) with type hidden (for UI frontend) // let's initiate files with empty url (we can't access the asset url) with type hidden (for UI frontend)
setTimeout(() => {
setFieldValue('files', [ setFieldValue('files', [
{ {
url: '', url: '',
type: 'hidden' type: 'hidden'
} }
]) ])
}, 500)
const providerUrl = values?.services const providerUrl = values?.services
? values?.services[0].providerUrl.url ? values?.services[0].providerUrl.url

View File

@ -12,13 +12,11 @@ export const validationSchema = Yup.object().shape({
files: Yup.array<FileInfo[]>() files: Yup.array<FileInfo[]>()
.of( .of(
Yup.object().shape({ Yup.object().shape({
url: testLinks(), url: testLinks(true),
valid: Yup.boolean().test((value, context) => { valid: Yup.boolean().test((value, context) => {
const { type } = context.parent const { type } = context.parent
// allow user to submit if the value type is hidden // allow user to submit if the value type is hidden
if (type === 'hidden') return true if (type === 'hidden') return true
return value || false return value || false
}) })
}) })
@ -26,14 +24,12 @@ export const validationSchema = Yup.object().shape({
.nullable(), .nullable(),
links: Yup.array<FileInfo[]>().of( links: Yup.array<FileInfo[]>().of(
Yup.object().shape({ Yup.object().shape({
url: testLinks(), url: testLinks(true),
valid: Yup.boolean().test((value, context) => { valid: Yup.boolean().test((value, context) => {
// allow user to submit if the value is null // allow user to submit if the value is null
const { valid, url } = context.parent const { valid, url } = context.parent
// allow user to continue if the url is empty // allow user to continue if the url is empty
if (!url) return true if (!url) return true
return valid return valid
}) })
}) })

View File

@ -79,8 +79,6 @@ export default function SearchPage({
fetchAssets(parsed, chainIds) fetchAssets(parsed, chainIds)
}, [parsed, chainIds, newCancelToken, fetchAssets]) }, [parsed, chainIds, newCancelToken, fetchAssets])
console.log(queryResult?.results)
return ( return (
<> <>
<div className={styles.search}> <div className={styles.search}>