-
Notifications
You must be signed in to change notification settings - Fork 2
/
form.js
207 lines (182 loc) · 6.14 KB
/
form.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import React, { useState, useEffect } from "react"
import { Router, useLocation, navigate } from "@reach/router"
import { window, exists } from "browser-monads"
import "url-search-params-polyfill"
import { GridContainer, Button, ButtonGroup } from "@trussworks/react-uswds"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { nanoid } from 'nanoid'
import {
VoteStep,
LocationStep,
IssuesStep,
SkillsStep,
TimeStep,
MoneyStep,
ReachStep,
SignupStep,
} from "./steps"
import ProgressBar from "./progress"
import { useAppState } from "../../state/context"
import { isEmpty } from "../../utils/object"
const headers = {
"Content-Type": "application/json",
}
const STEPS = [
[<VoteStep path="vote" key="vote" />, state => !isEmpty(state.registered) && !isEmpty(state.vbm)],
[<LocationStep path="location" key="location" />, state => !isEmpty(state.geocode),
"Please select a location from the drop-down. Be specific, so we can find your voting district."
],
[
<IssuesStep path="issues" key="issues" />,
state => !isEmpty(state.issues) && state.issues.length <= 3,
"Please select between one and three issues."
],
[
<SkillsStep path="skills" key="skills" />,
state => !isEmpty(state.skills) && state.skills.length <= 3,
"Please select between one and three skills."
],
[<TimeStep path="time" key="time" />, () => true],
[<MoneyStep path="money" key="money" />, () => true],
[<ReachStep path="reach" key="reach" />, () => true],
[
<SignupStep path="signup" key="signup" />,
state => !isEmpty(state.name) && !isEmpty(state.contact.email) && state.contact.email.includes("@"),
"Please enter your name and email, so we can contact you about your plan."
],
]
function useFormProgress() {
function goForward(currentStep, dispatch) {
let nextIndex = currentStep + 1
dispatch({ type: "STEP_CHANGE", payload: nextIndex})
let nextStep = STEPS[nextIndex]
exists(window) && navigate(`/form/${nextStep[0].props.path}`)
}
function goBack(currentStep, dispatch) {
let prevIndex = currentStep - 1
if (prevIndex < 0) { return false }
dispatch({ type: "STEP_CHANGE", payload: prevIndex})
// don't actually navigate, because this is only called when by the popstate event
// triggered by a real browser back button
// let prevStep = STEPS[prevIndex]
// exists(window) && navigate(`/form/${prevStep[0].props.path}`)
}
return [goForward, goBack]
}
function PlanForm() {
const {state, dispatch} = useAppState()
const location = useLocation()
const [goForward, goBack] = useFormProgress()
const [validate, setValidate] = useState(false)
const currentStep = state.step || 0
const isLast = currentStep === STEPS.length - 1
// start at first step
if(location.pathname === "/form/" || location.pathname === "/form") {
exists(window) && navigate("/form/vote")
}
// if we are in the form, but the url doesn't match
const pathParts = location.pathname.split('/')
if(pathParts && pathParts[1] === 'form' && currentStep) {
let stepPath = STEPS[currentStep][0].props.path
if (location.pathname !== `/form/${stepPath}`) {
exists(window) && navigate(`/form/${stepPath}`)
// redirect to correct path
}
}
// make the back button work as expected
useEffect(() => {
let triggerBack = () => goBack(currentStep, dispatch)
if(exists(window)) {
window.addEventListener('popstate', triggerBack)
// unfortunately there's no event for "pushstate"
// so we can't make forward work as well
}
return function cleanup() {
window.removeEventListener('popstate', triggerBack)
}
}, [currentStep, dispatch, goBack])
useEffect(() => {
// capture source from url param
if (location.search && !state.source) {
let urlParams = new URLSearchParams(location.search)
dispatch({ type: "SOURCE_CHANGE", payload: urlParams.get('source')})
}
})
function handleSubmit() {
dispatch({ type: "SUBMIT" })
let submit = state
submit.uid = nanoid() // create a new key for each submission
fetch("/.netlify/functions/create-form ", {
method: "POST",
headers: headers,
body: JSON.stringify(submit),
}).then(response => {
if (!response.ok) {
dispatch({ type: "SUBMISSION_RECEIVED", payload: false})
throw Error(response.statusText)
} else {
dispatch({ type: "SUBMISSION_RECEIVED", payload: submit.uid})
}
}).then(response => {
return fetch("/.netlify/functions/signup ", {
method: "POST",
headers: headers,
body: JSON.stringify(submit),
})
})
.catch(err => {
console.error(err)
})
}
if (state.isSubmitLoading) {
return (
<div className="form-container" path="submit">
<p>Loading...</p>
</div>
)
}
if (state.isSubmissionReceived) {
exists(window) && navigate("/plan")
}
// eslint-disable-next-line no-unused-vars
let [stepRender, stepValid, stepError] = STEPS[currentStep]
let isValid = stepValid(state)
return (
<GridContainer><div className="form-container">
<ProgressBar value={currentStep} max={STEPS.length - 1} />
<Router basepath="/form">
{ STEPS.map(([stepRender]) => stepRender) }
</Router>
<ButtonGroup className="nav-container">
<Button
type="submit"
className={[
"icon-right",
validate && !isValid ? "usa-button--secondary" : "",
].join(" ")}
onClick={e => {
e.preventDefault()
setValidate(true)
if (isValid) {
if (isLast) {
handleSubmit()
} else {
setValidate(false)
goForward(currentStep, dispatch)
}
}
}}
>
{isLast ? "Done" : "Next"}
<FontAwesomeIcon icon={["fas", isLast ? "check" : "arrow-right"]} />
</Button>
</ButtonGroup>
{validate && !isValid && (
<div className="error-container">
{stepError ? stepError : "Please select your answers"}
</div>
)}
</div></GridContainer>
)
}
export default PlanForm