Skip to content

Commit 4032e2d

Browse files
authored
✨ feat: Add webform resolver (#154)
1 parent 3c913d9 commit 4032e2d

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

starters/react-router/app/integration/forms/ContactForm/ContactForm.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { Button } from '~/components/ui/button'
77
import { Label } from '~/components/ui/label'
88
import { contactFormSchema } from '~/integration/forms/ContactForm/schema'
99
import { useFetcher } from 'react-router'
10-
import { action } from '~/routes/contact_form'
10+
import { action } from '~/routes/forms/contact_form'
1111

12-
export const ContactForm = () => {
12+
export const ContactForm = ({ id }: { id: string }) => {
1313
const fetcher = useFetcher<typeof action>()
1414

1515
const [form, fields] = useForm({
16-
id: 'contact-form',
16+
id,
1717
lastResult: fetcher.data?.reply,
1818
constraint: getZodConstraint(contactFormSchema),
1919
onValidate({ formData }) {
@@ -46,7 +46,7 @@ export const ContactForm = () => {
4646
<fetcher.Form
4747
{...getFormProps(form)}
4848
method="post"
49-
action="/contact_form"
49+
action="/form/contact_form"
5050
className="space-y-4"
5151
>
5252
{form.errors && (

starters/react-router/app/integration/node/NodeArticle.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export default function NodeArticleComponent({
2828

2929
return (
3030
<>
31-
{}
3231
<Article
3332
title={title}
3433
content={body.processed.toString()}

starters/react-router/app/integration/resolvers/ParagraphWebformResolver.tsx

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
import { FragmentOf, readFragment } from 'gql.tada'
22
import { ContactForm } from '~/integration/forms/ContactForm/ContactForm'
3-
4-
import { WebformFragment } from '~/graphql/fragments/webform'
53
import { graphql } from '~/graphql/gql.tada'
64

75
interface ParagraphWebformProps {
86
paragraph: FragmentOf<typeof ParagraphWebformFragment>
97
}
108

11-
export const ParagraphWebformFragment = graphql(
12-
`
13-
fragment ParagraphWebformFragment on ParagraphWebform {
14-
__typename
9+
export const ParagraphWebformFragment = graphql(`
10+
fragment ParagraphWebformFragment on ParagraphWebform {
11+
__typename
12+
id
13+
heading
14+
subheadingOptional: subheading
15+
descriptionOptional: description
16+
form {
1517
id
16-
heading
17-
subheadingOptional: subheading
18-
descriptionOptional: description
19-
form {
20-
id
21-
__typename
22-
...WebformFragment
23-
}
18+
__typename
2419
}
25-
`,
26-
[WebformFragment]
27-
)
20+
}
21+
`)
22+
23+
type FormComponents = React.FC<{ id: string }>
24+
25+
const WebformComponentResolver: Record<string, FormComponents> = {
26+
contact_form: ContactForm,
27+
}
2828

2929
export const ParagraphWebformResolver = ({
3030
paragraph,
3131
}: ParagraphWebformProps) => {
3232
const { heading, subheadingOptional, descriptionOptional, form } =
3333
readFragment(ParagraphWebformFragment, paragraph)
3434

35+
if (!form || !form.id) {
36+
return null
37+
}
38+
39+
const Webform = WebformComponentResolver[form.id]
3540
return (
3641
<div className="container mx-auto py-8 md:py-16 lg:py-24">
3742
<div>
@@ -47,11 +52,9 @@ export const ParagraphWebformResolver = ({
4752
dangerouslySetInnerHTML={{ __html: descriptionOptional }}
4853
/>
4954
)}
50-
{form && (
51-
<div className="py-8 md:py-16 lg:py-24">
52-
<ContactForm />
53-
</div>
54-
)}
55+
<div className="py-8 md:py-16 lg:py-24">
56+
<Webform id={form.id} />
57+
</div>
5558
</div>
5659
</div>
5760
)

starters/react-router/app/routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { type RouteConfig, index, route } from '@react-router/dev/routes'
33
export default [
44
index('routes/$.tsx', { id: 'index' }),
55
route('*', 'routes/$.tsx'),
6-
route('contact_form', 'routes/contact_form.ts')
6+
route('form/contact_form', 'routes/forms/contact_form.ts')
77
] satisfies RouteConfig

0 commit comments

Comments
 (0)