-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SLB-218): contact form and mutation example in FE
- Loading branch information
Showing
10 changed files
with
288 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Contact } from '@custom/ui/routes/Contact'; | ||
import React from 'react'; | ||
|
||
export default function ContentHubPage() { | ||
return <Contact /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
mutation ContactRequest ( | ||
$contact: ContactInput | ||
){ | ||
createContact(contact: $contact) { | ||
errors { | ||
key | ||
field | ||
message | ||
} | ||
contact { | ||
name | ||
message | ||
subject | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { ContactForm as Component } from './ContactForm'; | ||
|
||
export default { | ||
component: Component, | ||
} satisfies Meta<typeof Component>; | ||
|
||
export const Empty = {} satisfies StoryObj<typeof Component>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { ContactRequestMutation } from '@custom/schema'; | ||
import React from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { useIntl } from 'react-intl'; | ||
import { z } from 'zod'; | ||
|
||
import { useMutation } from '../../utils/operation'; | ||
import { Messages } from './Messages'; | ||
|
||
const formValueSchema = z.object({ | ||
name: z.string(), | ||
email: z.string(), | ||
subject: z.string().optional(), | ||
message: z.string(), | ||
}); | ||
|
||
export function ContactForm() { | ||
const intl = useIntl(); | ||
type FormValue = z.infer<typeof formValueSchema>; | ||
const { register, handleSubmit } = useForm<FormValue>(); | ||
|
||
const { data, trigger, isMutating } = useMutation(ContactRequestMutation); | ||
const errorMessages = (!isMutating && data && data.createContact?.errors && data.createContact.errors.length > 0 ) ? | ||
data.createContact.errors.map(error => { | ||
return error?.message || ''; | ||
}) | ||
: | ||
null; | ||
return ( | ||
<div> | ||
{ errorMessages ? <Messages messages={errorMessages} /> : null} | ||
<form | ||
className="mt-5 sm:items-center" | ||
onSubmit={handleSubmit((values) => { | ||
// @todo: fix this | ||
// @ts-ignore | ||
trigger({ | ||
contact: values, | ||
}); | ||
})} | ||
> | ||
<div className="w-full sm:max-w-sm"> | ||
<label htmlFor="name" className="sr-only"> | ||
{intl.formatMessage({ | ||
defaultMessage: 'Name', | ||
id: 'HAlOn1', | ||
})} | ||
</label> | ||
<input | ||
{...register('name', { required: true })} | ||
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" | ||
placeholder={intl.formatMessage({ | ||
defaultMessage: 'Name', | ||
id: 'HAlOn1', | ||
})} | ||
/> | ||
</div> | ||
<div className="w-full sm:max-w-sm pt-2"> | ||
<label htmlFor="email" className="sr-only"> | ||
{intl.formatMessage({ | ||
defaultMessage: 'Email', | ||
id: 'sy+pv5', | ||
})} | ||
</label> | ||
<input | ||
{...register('email', { required: true })} | ||
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" | ||
placeholder={intl.formatMessage({ | ||
defaultMessage: 'Email', | ||
id: 'sy+pv5', | ||
})} | ||
/> | ||
</div> | ||
<div className="w-full sm:max-w-sm pt-2"> | ||
<label htmlFor="subject" className="sr-only"> | ||
{intl.formatMessage({ | ||
defaultMessage: 'Subject', | ||
id: 'LLtKhp', | ||
})} | ||
</label> | ||
<input | ||
{...register('subject')} | ||
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" | ||
placeholder={intl.formatMessage({ | ||
defaultMessage: 'Subject', | ||
id: 'LLtKhp', | ||
})} | ||
/> | ||
</div> | ||
<div className="w-full sm:max-w-sm pt-2"> | ||
<label htmlFor="message" className="sr-only"> | ||
{intl.formatMessage({ | ||
defaultMessage: 'Message', | ||
id: 'T7Ry38', | ||
})} | ||
</label> | ||
<textarea | ||
{...register('message', { required: true })} | ||
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" | ||
placeholder={intl.formatMessage({ | ||
defaultMessage: 'Message', | ||
id: 'T7Ry38', | ||
})} | ||
/> | ||
</div> | ||
<div className="w-full pt-2"> | ||
<button | ||
type="submit" | ||
disabled={isMutating} | ||
className="mt-3 inline-flex w-full items-center justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 sm:ml-3 sm:mt-0 sm:w-auto" | ||
> | ||
{isMutating ? intl.formatMessage({ defaultMessage: 'Sending...', id: '82Y7Sa' }) : intl.formatMessage({ defaultMessage: 'Submit', id: 'wSZR47' })} | ||
</button> | ||
</div> | ||
</form> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
|
||
import { ContactForm } from '../Molecules/ContactForm'; | ||
|
||
export function Contact() { | ||
return ( | ||
<div className="mx-auto max-w-6xl"> | ||
<ContactForm /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Locale } from '@custom/schema'; | ||
import React from 'react'; | ||
|
||
import { useTranslations } from '../../utils/translations'; | ||
import { PageTransition } from '../Molecules/PageTransition'; | ||
import { Contact as ContactOrganism } from '../Organisms/Contact'; | ||
|
||
export function Contact() { | ||
// Initialize the contact page in each language. | ||
useTranslations( | ||
Object.fromEntries( | ||
Object.values(Locale).map((locale) => [locale, `/${locale}/contact`]), | ||
), | ||
); | ||
return ( | ||
<PageTransition> | ||
<ContactOrganism /> | ||
</PageTransition> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,7 @@ describe('create contact', () => { | |
{ | ||
"field": "email", | ||
"key": "invalid_field_email", | ||
"message": "The email address <em class=\"placeholder\">invalid_email</em> is not valid. Use the format [email protected].", | ||
"message": "The email address <em class="placeholder">invalid_email</em> is not valid. Use the format [email protected].", | ||
}, | ||
], | ||
}, | ||
|