1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +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' import Terms from '../../molecules/FormFields/Terms'
export default function InputElement(props: InputProps): ReactElement { export default function InputElement(props: InputProps): ReactElement {
const { type, options, rows, name } = props const { type, options, rows, name, value } = props
switch (type) { switch (type) {
case 'select': case 'select':
@ -62,10 +62,11 @@ export default function InputElement(props: InputProps): ReactElement {
return ( return (
<input <input
id={name} id={name}
type={type || 'text'}
className={styles.input} className={styles.input}
name={name} name={name}
{...props} {...props}
value={value || ''}
type={type || 'text'}
/> />
) )
} }

View File

@ -20,3 +20,13 @@
right: 0; right: 0;
top: 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 Help from './Help'
import Label from './Label' import Label from './Label'
import styles from './index.module.css' import styles from './index.module.css'
import { ErrorMessage, FormikState, FieldProps, FieldInputProps } from 'formik' import { ErrorMessage } from 'formik'
import { MetadataPublishForm } from '../../../@types/Metadata' import classNames from 'classnames/bind'
const cx = classNames.bind(styles)
export interface InputProps { export interface InputProps {
name: string name: string
@ -36,9 +38,18 @@ export interface InputProps {
export default function Input(props: Partial<InputProps>): ReactElement { export default function Input(props: Partial<InputProps>): ReactElement {
const { required, name, label, help, additionalComponent, field } = props 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 ( return (
<div <div
className={styles.field} className={styleClasses}
data-is-submitting={props.form && props.form.isSubmitting ? true : null} data-is-submitting={props.form && props.form.isSubmitting ? true : null}
> >
<Label htmlFor={name} required={required}> <Label htmlFor={name} required={required}>

View File

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

View File

@ -1,3 +1,9 @@
.form { .form {
composes: box from '../../atoms/Box.module.css'; 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 styles from './PublishForm.module.css'
import { useOcean, usePublish } from '@oceanprotocol/react' import { useOcean, usePublish } from '@oceanprotocol/react'
import { useFormikContext, Form, Field } from 'formik' import { useFormikContext, Form, Field } from 'formik'
@ -15,11 +15,28 @@ export default function PublishForm({
}): ReactElement { }): ReactElement {
const { ocean, account } = useOcean() const { ocean, account } = useOcean()
const { publishStepText, isLoading } = usePublish() 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 ( return (
<Form <Form
className={styles.form} className={styles.form}
// do we need this?
onChange={() => status === 'empty' && setStatus(null)} onChange={() => status === 'empty' && setStatus(null)}
> >
{content.data.map((field: FormFieldProps) => ( {content.data.map((field: FormFieldProps) => (
@ -29,13 +46,29 @@ export default function PublishForm({
{isLoading ? ( {isLoading ? (
<Loader message={publishStepText} /> <Loader message={publishStepText} />
) : ( ) : (
<Button <footer className={styles.actions}>
style="primary" <Button
type="submit" style="primary"
disabled={!ocean || !account || !isValid || status === 'empty'} type="submit"
> disabled={!ocean || !account || !isValid || status === 'empty'}
Submit >
</Button> 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" /> <Persist name="ocean-publish-form" />
</Form> </Form>

View File

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

View File

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