-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontactForm.tsx
More file actions
67 lines (57 loc) · 1.79 KB
/
contactForm.tsx
File metadata and controls
67 lines (57 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { useState } from 'react';
import { FormField } from './formField';
import { TextInput } from './textInput';
import styles from './contactForm.module.css';
import { Textarea } from './textarea';
import { Button } from './button';
import { toast } from 'react-hot-toast';
import { RequestBody } from '../../pages/api/contact-form';
export function ContactForm(props: { onSubmit: () => void }) {
const [name, setName] = useState('');
const [email, setEmailAddress] = useState('');
const [message, setMessage] = useState('');
const [isLoading, setIsLoading] = useState(false);
async function onSubmit(e: React.FormEvent) {
e.preventDefault();
setIsLoading(true);
const body: RequestBody = {
name,
email,
message,
};
try {
const result = await fetch('/api/contact-form/', {
method: 'POST',
body: JSON.stringify(body),
});
if (result.ok) {
props.onSubmit();
toast.success("Nice, we'll be in touch shortly!");
} else {
toast.error('Oops');
}
} catch (error) {
console.error(error);
toast.error('Oops');
}
setIsLoading(false);
}
return (
<form className={styles.form} onSubmit={onSubmit}>
<FormField label="Your name">
<TextInput value={name} onChange={setName} placeholder="e.g. Mr. Robot" />
</FormField>
<FormField label="Your email">
<TextInput
value={email}
onChange={setEmailAddress}
placeholder="e.g. elliot@protonmail.com"
/>
</FormField>
<FormField label="Your message">
<Textarea value={message} onChange={setMessage} placeholder={`Hi there, do you...`} />
</FormField>
<Button label="Send" isLoading={isLoading} isDisabled={isLoading} />
</form>
);
}