-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
260 lines (243 loc) · 7.6 KB
/
index.js
File metadata and controls
260 lines (243 loc) · 7.6 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
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
import { Formik, Form } from 'formik'
import { CheckCircle } from 'phosphor-react'
import { rem } from 'polished'
import { useEffect, useState } from 'react'
import styled from 'styled-components'
import * as Yup from 'yup'
import {
Button,
FadeIn,
FadeOut,
Input,
Loader,
LoaderContainer,
Select,
Text,
TextArea,
} from 'components'
import ErrorMessage from './error'
const FormContainer = styled.div`
width: 33vw;
@media only screen and (max-width: 1023px) {
width: 100%;
}
height: 100vh;
`
const SuccessHeader = styled.h2`
font-family: Montserrat;
font-weight: bold;
font-size: 24px;
line-height: 36px;
margin-left: ${rem('23px')};
@media only screen and (max-width: 767px) {
margin: 0;
}
`
const SuccessHeaderContainer = styled.div`
display: flex;
flex-direction: row;
@media only screen and (max-width: 1023px) {
width: 50%;
margin: 0 auto;
}
`
const SelectOptions = [
{ value: 'angellist', label: 'AngelList' },
{ value: 'blog', label: 'Blog' },
{ value: 'event', label: 'Event' },
{ value: 'linkedin', label: 'LinkedIn' },
{ value: 'linkedin_connection', label: 'LinkedIn Connection' },
{ value: 'twitter', label: 'Twitter' },
{ value: 'wordofmouth', label: 'Word of Mouth' },
{ value: 'youtube', label: 'YouTube' },
{ value: 'other', label: 'Other...' },
]
const ApplicationValidationSchema = Yup.object().shape({
name: Yup.string().required('Please fill out this empty field'),
email: Yup.string()
.email(
'The email you input seems to be invalid. Please enter a valid email',
)
.required('Please fill out this empty field'),
resources: Yup.array()
.of(
Yup.object().shape({
value: Yup.string(),
label: Yup.string(),
}),
)
.min(1, 'Please select at least one option')
.required(),
other: Yup.string().when('resources', {
is: (resources) =>
resources && resources.find((res) => res.value === 'other'),
then: Yup.string().max(50, 'Please limit it to 50 characters').required('Please describe the other resource'),
}),
info: Yup.string().max(250, 'Please limit your response to 250 characters'),
})
const initialValues = {
email: '',
name: '',
resources: [],
other: '',
info: '',
}
const parseValues = (values) => {
const parsedValues = values
const resources = parsedValues.resources
? parsedValues.resources
.map((resource) =>
resource.value === 'other' ? parsedValues.other : resource.label,
)
.join(', ')
: '' // Should not return empty string. This field is required, if resources = '', an error has occurred
return { ...parsedValues, resources }
}
const ApplicationForm = () => {
const [isSubmitted, setIsSubmitted] = useState(true)
const [isSubmitting, setIsSubmitting] = useState(false)
const [hasErrors, setHasErrors] = useState(false)
const [hideOther, setHideOther] = useState(true)
const onClick = async (values) => {
setIsSubmitting(true)
setHasErrors(false)
try {
const parsedVales = parseValues(values)
const res = await fetch(process.env.NEXT_PUBLIC_HELIX_HOST, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(parsedVales),
})
if (res.status === 200) {
setIsSubmitted(true)
} else {
setHasErrors(true)
}
} catch (err) {
setHasErrors(true)
} finally {
setIsSubmitting(false)
}
}
useEffect(() => {
setIsSubmitted(localStorage.getItem('formSubmitted') === 'true')
}, [])
useEffect(() => {
localStorage.setItem('formSubmitted', isSubmitted)
}, [isSubmitted])
const renderForm = () => (
<FormContainer>
<FadeIn show={isSubmitted}>
<SuccessHeaderContainer>
<CheckCircle
{...{
color: '#FF68BA',
size: rem('37px'),
weight: 'fill',
}}
/>
<SuccessHeader>Application Sent!</SuccessHeader>
</SuccessHeaderContainer>
<Text successText>
Look out for your inbox! Someone from Commit will be in touch with
you.
</Text>
<Text successText>
In the meantime take a look at some{' '}
<a href="https://blog.commit.dev/">blog posts</a> from our engineers.
</Text>
</FadeIn>
<>
<FadeIn show={hasErrors}>
<Text errorText>
Woops, the application failed to send. Please try again.
</Text>
</FadeIn>
<LoaderContainer>
<Loader show={isSubmitting} />
{!isSubmitted && (
<FadeOut show={!isSubmitting}>
<Formik
initialValues={initialValues}
validationSchema={ApplicationValidationSchema}
onSubmit={onClick}
>
{({
errors,
touched,
handleChange,
handleBlur,
setFieldValue,
setFieldTouched,
}) => (
<Form>
{errors.name && touched.name ? (
<ErrorMessage message={errors.name} />
) : null}
<Input id="name" name="name" placeholder="Name" />
{errors.email && touched.email ? (
<ErrorMessage message={errors.email} />
) : null}
<Input
id="email"
name="email"
placeholder="E-mail"
type="email"
/>
{errors.resources && touched.resources ? (
<ErrorMessage message={errors.resources} />
) : null}
<Select
instanceId="resources"
name="resources"
onChange={(e, value) => {
setHideOther(
!value.find((res) => res.value === 'other'),
)
setFieldValue(e, value)
}}
onBlur={setFieldTouched}
options={SelectOptions}
/>
{!hideOther && errors.other && touched.other ? (
<ErrorMessage message={errors.other} />
) : null}
<Input
id="other"
name="other"
placeholder="Please describe the other resource(s)"
type={hideOther ? 'hidden' : 'text'}
/>
{errors.info && touched.info ? (
<ErrorMessage message={errors.info} />
) : null}
<TextArea
name="info"
as="textarea"
onChange={handleChange}
onBlur={handleBlur}
placeholder='Let us know where to learn more about you (Ex. Website, blog, youtube, etc)'
/>
<Button
{...{
'data-test-id': 'button',
disabled: isSubmitting,
}}
>
Apply To Join
</Button>
</Form>
)}
</Formik>
</FadeOut>
)}
</LoaderContainer>
</>
</FormContainer>
)
return renderForm()
}
export default ApplicationForm