Chapter 3 Complete.

This commit is contained in:
Colin Diem
2022-04-21 20:36:02 -04:00
parent bc1cf7b5c3
commit 11919b6b5f
2 changed files with 9 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
import { validate } from '@redwoodjs/api'
import { db } from 'src/lib/db' import { db } from 'src/lib/db'
export const contacts = () => { export const contacts = () => {
@@ -11,6 +12,7 @@ export const contact = ({ id }) => {
} }
export const createContact = ({ input }) => { export const createContact = ({ input }) => {
validate(input.email, 'email', { email: true })
return db.contact.create({ return db.contact.create({
data: input, data: input,
}) })

View File

@@ -3,10 +3,12 @@ import { toast, Toaster } from '@redwoodjs/web/toast'
import { import {
FieldError, FieldError,
Form, Form,
FormError,
Label, Label,
TextField,
Submit, Submit,
TextAreaField, TextAreaField,
TextField,
useForm,
} from '@redwoodjs/forms' } from '@redwoodjs/forms'
const CREATE_CONTACT = gql` const CREATE_CONTACT = gql`
@@ -18,9 +20,11 @@ const CREATE_CONTACT = gql`
` `
const ContactPage = () => { const ContactPage = () => {
const formMethods = useForm({ mode: 'onBlur' })
const [create, { loading, error }] = useMutation(CREATE_CONTACT, { const [create, { loading, error }] = useMutation(CREATE_CONTACT, {
onCompleted: () => { onCompleted: () => {
toast.success('Thank you for your submission!') toast.success('Thank you for your submission!')
formMethods.reset()
}, },
}) })
@@ -33,7 +37,8 @@ const ContactPage = () => {
<MetaTags title="Contact" description="Contact page" /> <MetaTags title="Contact" description="Contact page" />
<Toaster /> <Toaster />
<Form onSubmit={onSubmit} config={{ mode: 'onBlur' }}> <Form onSubmit={onSubmit} error={error} formMethods={formMethods}>
<FormError error={error} wrapperClassName="form-error" />
<Label name="name" errorClassName="error"> <Label name="name" errorClassName="error">
Name Name
</Label> </Label>