forked from ReactTraining/hooks-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignupForm.js
73 lines (68 loc) · 2.13 KB
/
SignupForm.js
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
68
69
70
71
72
73
import React, { Fragment, useState } from "react"
import VisuallyHidden from "@reach/visually-hidden"
import { signup } from "app/utils"
import TabsButton from "app/TabsButton"
import { FaDumbbell } from "react-icons/fa"
import { DateFields, MonthField, DayField, YearField } from "app/DateFields"
function TextInput({ id, label, type = "text" }) {
return (
<Fragment>
<VisuallyHidden>
<label htmlFor={id}>{label}</label>
</VisuallyHidden>
<input id={id} placeholder={label} type={type} required />
</Fragment>
)
}
export default function SignupForm() {
const [error, setError] = useState(null)
const [loading, setLoading] = useState(false)
const [startDate, setStartDate] = useState(new Date("March 1, 2019"))
const handleSignup = async event => {
event.preventDefault()
setLoading(true)
const [displayName, photoURL, email, password] = event.target.elements
try {
await signup({
displayName: displayName.value,
email: email.value,
password: password.value,
photoURL: photoURL.value,
startDate
})
} catch (error) {
setLoading(false)
setError(error)
}
}
return (
<div>
{error && (
<div>
<p>Oops, there was an error logging you in.</p>
<p>
<i>{error.message}</i>
</p>
</div>
)}
<form onSubmit={handleSignup}>
<TextInput id="displayName" label="Display Name" />
<TextInput id="photoURL" label="Avatar URL" />
<TextInput id="email" label="Email" />
<TextInput id="password" type="password" label="Password" />
<p>
<span aria-hidden="true">Start:</span>{" "}
<DateFields value={startDate} onChange={setStartDate}>
<MonthField aria-label="Start Month" /> /{" "}
<DayField aria-label="Start Day" /> /{" "}
<YearField start={2018} end={2019} aria-label="Start year" />
</DateFields>
</p>
<TabsButton>
<FaDumbbell />
<span>{loading ? "Loading..." : "Sign Up"}</span>
</TabsButton>
</form>
</div>
)
}