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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/** @jsx jsx */
import { jsx, css } from "@emotion/react";
import { Link } from "react-router-dom";
import React, { SyntheticEvent, useEffect, useState, createRef } from "react";
import { useParams } from "react-router";
import { PropagateLoader } from "react-spinners";
import HeaderBar from "../components/HeaderBar";
import RenderedQuestion from "../components/Question";
import Loading from "../components/Loading";
import ScrollToTop from "../components/ScrollToTop";
import { Form, FormFeatures, getForm } from "../api/forms";
import colors from "../colors";
import { unselectable } from "../commonStyles";
import { Question, QuestionType } from "../api/question";
import ApiClient from "../api/client";
const containerStyles = css`
margin: auto;
width: 50%;
text-align: center;
font-size: 1.5rem;
> div {
display: inline-block;
margin: 2rem auto;
width: 50%;
}
@media (max-width: 850px) {
width: 100%;
> div {
display: flex;
justify-content: center;
margin: 0 auto;
}
}
.return_button {
text-align: left;
}
.return_button.closed {
text-align: center;
}
`;
const returnStyles = css`
padding: 0.5rem 2rem;
border-radius: 8px;
color: white;
text-decoration: none;
white-space: nowrap;
background-color: ${colors.greyple};
transition: background-color 300ms;
:hover {
background-color: ${colors.darkerGreyple};
}
`;
interface PathParams {
id: string
}
interface NavigationProps {
form_state: boolean // Whether the form is open or not
}
class Navigation extends React.Component<NavigationProps> {
separatorStyles = css`
height: 0;
display: none;
@media (max-width: 850px) {
display: block;
}
`;
submitStyles = css`
text-align: right;
white-space: nowrap;
button {
padding: 0.5rem 4rem;
cursor: pointer;
border: none;
border-radius: 8px;
color: white;
font: inherit;
background-color: ${colors.blurple};
transition: background-color 300ms;
}
button:hover {
background-color: ${colors.darkerBlurple};
}
`;
render(): JSX.Element {
let submit = null;
if (this.props.form_state) {
submit = (
<div css={this.submitStyles}>
<button form="form" type="submit">Submit</button>
</div>
);
}
return (
<div css={[unselectable, containerStyles]}>
<div className={ "return_button" + (this.props.form_state ? "" : " closed") }>
<Link to="/" css={returnStyles}>Return Home</Link>
</div>
<br css={this.separatorStyles}/>
{ submit }
</div>
);
}
}
const formStyles = css`
margin: auto;
width: 50%;
@media (max-width: 800px) {
/* Make form larger on mobile and tablet screens */
width: 80%;
}
`;
const closedHeaderStyles = css`
margin-bottom: 2rem;
padding: 1rem 4rem;
border-radius: 8px;
text-align: center;
font-size: 1.5rem;
background-color: ${colors.error};
@media (max-width: 500px) {
padding: 1rem 1.5rem;
}
`;
function FormPage(): JSX.Element {
const { id } = useParams<PathParams>();
const [form, setForm] = useState<Form>();
const [sending, setSending] = useState<boolean>();
const [sent, setSent] = useState<boolean>();
useEffect(() => {
getForm(id).then(form => {
setForm(form);
});
}, []);
if (form && sent) {
const thanksStyle = css`font-family: "Uni Sans", "Hind", "Arial", sans-serif; margin-top: 15.5rem;`;
const divStyle = css`width: 80%;`;
return (
<div>
<HeaderBar title={form.name} description={form.description}/>
<div css={[unselectable, containerStyles, divStyle]}>
<h3 css={thanksStyle}>{form.submitted_text ?? "Thanks for your response!"}</h3>
<div className={ "return_button closed" }>
<Link to="/" css={returnStyles}>Return Home</Link>
</div>
</div>
</div>
);
}
if (sending) {
return (
<div>
<HeaderBar title={"Submitting..."}/>
<div css={{display: "flex", justifyContent: "center", paddingTop: "40px"}}>
<PropagateLoader color="white"/>
</div>
</div>
);
}
if (!form) {
return <Loading/>;
}
const questions = form.questions.map((question, index) => {
return <RenderedQuestion ref={createRef<RenderedQuestion>()} scroll_ref={createRef<HTMLDivElement>()} question={question} public_state={new Map()} key={index + Date.now()}/>;
});
async function handleSubmit(event: SyntheticEvent) {
event.preventDefault();
// Client-side required validation
const invalidFieldIds: number[] = [];
questions.forEach((prop, i) => {
const question: Question = prop.props.question;
if (!question.required) {
return;
}
prop.ref.current.validateField();
// In case when field is invalid, add this to invalid fields list.
if (prop.props.public_state.get("valid") === false) {
invalidFieldIds.push(i);
}
});
if (invalidFieldIds.length) {
const firstErrored = questions[invalidFieldIds[0]];
if (firstErrored !== undefined) {
firstErrored.props.scroll_ref.current.scrollIntoView({ behavior: "smooth", block: "center" });
}
return;
}
setSending(true);
const answers = {};
questions.forEach(prop => {
const question = prop.props.question;
const options: string | string[] = prop.props.question.data["options"];
// TODO: Parse input from each question, and submit
switch (question.type) {
case QuestionType.Section:
answers[question.id] = false;
break;
case QuestionType.Code:
answers[question.id] = "";
break;
case QuestionType.Checkbox:
if (typeof options !== "string") {
const keys: Map<string, string> = new Map();
options.forEach((val, index) => {
keys.set(val, `${("000" + index).slice(-4)}. ${val}`);
});
const pairs = {};
keys.forEach((val, key) => {
pairs[key] = !!prop.props.public_state.get(val);
});
answers[question.id] = pairs;
}
break;
default:
answers[question.id] = prop.props.public_state.get("value");
}
});
await ApiClient.post(`forms/submit/${id}`, {response: answers});
setSending(false);
setSent(true);
}
const open: boolean = form.features.includes(FormFeatures.Open);
let closed_header = null;
if (!open) {
closed_header = <div css={closedHeaderStyles}>This form is now closed. You will not be able to submit your response.</div>;
}
return (
<div>
<HeaderBar title={form.name} description={form.description}/>
<div>
<form id="form" onSubmit={handleSubmit} css={[formStyles, unselectable]}>
{ closed_header }
{ questions }
</form>
<Navigation form_state={open}/>
</div>
<div css={css`margin-bottom: 10rem`}/>
<ScrollToTop/>
</div>
);
}
export default FormPage;
|