1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-11-14 09:14:52 +01:00

more error validation tweaks, reset form

This commit is contained in:
Matthias Kretschmann 2020-07-17 21:21:27 +02:00
parent f9c71174b8
commit f440e178dd
Signed by: m
GPG Key ID: 606EEEF3C479A91F
8 changed files with 90 additions and 32 deletions

View File

@ -6,7 +6,7 @@ import FilesInput from '../../molecules/FormFields/FilesInput'
import Terms from '../../molecules/FormFields/Terms'
export default function InputElement(props: InputProps): ReactElement {
const { type, options, rows, name } = props
const { type, options, rows, name, value } = props
switch (type) {
case 'select':
@ -62,10 +62,11 @@ export default function InputElement(props: InputProps): ReactElement {
return (
<input
id={name}
type={type || 'text'}
className={styles.input}
name={name}
{...props}
value={value || ''}
type={type || 'text'}
/>
)
}

View File

@ -20,3 +20,13 @@
right: 0;
top: 0;
}
.hasError label {
color: var(--brand-alert-red);
}
.hasError input,
.hasError select,
.hasError textarea {
border-color: var(--brand-alert-red);
}

View File

@ -3,8 +3,10 @@ import InputElement from './InputElement'
import Help from './Help'
import Label from './Label'
import styles from './index.module.css'
import { ErrorMessage, FormikState, FieldProps, FieldInputProps } from 'formik'
import { MetadataPublishForm } from '../../../@types/Metadata'
import { ErrorMessage } from 'formik'
import classNames from 'classnames/bind'
const cx = classNames.bind(styles)
export interface InputProps {
name: string
@ -36,9 +38,18 @@ export interface InputProps {
export default function Input(props: Partial<InputProps>): ReactElement {
const { required, name, label, help, additionalComponent, field } = props
const hasError =
props.form.touched[field.name] &&
typeof props.form.errors[field.name] === 'string'
const styleClasses = cx({
field: true,
hasError: hasError
})
return (
<div
className={styles.field}
className={styleClasses}
data-is-submitting={props.form && props.form.isSubmitting ? true : null}
>
<Label htmlFor={name} required={required}>

View File

@ -14,7 +14,7 @@
}
.previewTitle {
font-size: var(--font-size-base);
font-size: var(--font-size-small);
text-transform: uppercase;
color: var(--color-secondary);
margin-bottom: calc(var(--spacer) / 2);
@ -24,10 +24,6 @@
margin-bottom: calc(var(--spacer) / 2);
}
.preview [class*='MetaItem-module--metaItem'] {
margin-bottom: calc(var(--spacer) / 2);
}
.preview [class*='MetaItem-module--metaItem'] h3 {
margin-bottom: calc(var(--spacer) / 12);
}

View File

@ -1,3 +1,9 @@
.form {
composes: box from '../../atoms/Box.module.css';
}
.actions {
display: flex;
justify-content: space-between;
align-items: center;
}

View File

@ -1,4 +1,4 @@
import React, { ReactElement } from 'react'
import React, { ReactElement, useEffect } from 'react'
import styles from './PublishForm.module.css'
import { useOcean, usePublish } from '@oceanprotocol/react'
import { useFormikContext, Form, Field } from 'formik'
@ -15,11 +15,28 @@ export default function PublishForm({
}): ReactElement {
const { ocean, account } = useOcean()
const { publishStepText, isLoading } = usePublish()
const { status, setStatus, isValid } = useFormikContext()
const {
status,
setStatus,
isValid,
touched,
setErrors,
setTouched,
resetForm,
initialValues
} = useFormikContext()
// reset form validation on every mount
useEffect(() => {
setErrors({})
setTouched({})
// setSubmitting(false)
}, [])
return (
<Form
className={styles.form}
// do we need this?
onChange={() => status === 'empty' && setStatus(null)}
>
{content.data.map((field: FormFieldProps) => (
@ -29,13 +46,29 @@ export default function PublishForm({
{isLoading ? (
<Loader message={publishStepText} />
) : (
<Button
style="primary"
type="submit"
disabled={!ocean || !account || !isValid || status === 'empty'}
>
Submit
</Button>
<footer className={styles.actions}>
<Button
style="primary"
type="submit"
disabled={!ocean || !account || !isValid || status === 'empty'}
>
Submit
</Button>
{status !== 'empty' && (
<Button
style="text"
size="small"
onClick={(e) => {
e.preventDefault()
resetForm(initialValues)
setStatus('empty')
}}
>
Reset Form
</Button>
)}
</footer>
)}
<Persist name="ocean-publish-form" />
</Form>

View File

@ -26,7 +26,7 @@ export default function PublishPage({
console.log(`
Collected form values:
----------------------
${JSON.stringify(values)}
${JSON.stringify(values, null, 2)}
`)
const metadata = transformPublishFormToMetadata(values)
@ -36,7 +36,7 @@ export default function PublishPage({
console.log(`
Transformed metadata values:
----------------------
${JSON.stringify(metadata)}
${JSON.stringify(metadata, null, 2)}
Cost: 1
Tokens to mint: ${tokensToMint}
`)
@ -56,6 +56,7 @@ export default function PublishPage({
// TODO: reset form state and make sure persistant form in localStorage is cleared
// Go to new asset detail page
navigate(`/asset/${ddo.id}`)
} catch (error) {
console.error(error.message)

View File

@ -22,15 +22,15 @@ export const validationSchema = Yup.object().shape<MetadataPublishForm>({
})
export const initialValues: MetadataPublishForm = {
name: undefined,
author: undefined,
cost: undefined,
files: undefined,
description: undefined,
license: undefined,
access: undefined,
termsAndConditions: undefined,
copyrightHolder: undefined,
tags: undefined,
links: undefined
name: '',
author: '',
cost: '',
files: '',
description: '',
license: '',
access: '',
termsAndConditions: false,
copyrightHolder: '',
tags: '',
links: ''
}