-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormikForm.js
192 lines (187 loc) · 5.76 KB
/
FormikForm.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
import './FormikForm.css';
import { Formik } from 'formik';
const handleOnSubmit = (values) => {
const data = JSON.stringify(values);
const file = new Blob([data], { type: 'application/json' });
const fileName = 'employee.json'
uploadToS3(file, fileName)
};
const uploadToS3 = async (file, fileName) => {
const url = `${'INSERT API ENDPOINT CREATED BY CDK HERE'}/document?file=${fileName}&folder=documents`;
const options = {
method: 'PUT'
}
let preSignedUrl
await fetch(url, options)
.then(response => response.json())
.then(data => preSignedUrl = data)
console.log(preSignedUrl.url)
const s3Options = {
method: 'PUT',
body: file
}
await fetch(preSignedUrl.url, s3Options)
};
const downloadFromS3 = () => {
const url = `${'INSERT API ENDPOINT CREATED BY CDK HERE'}/document?file=documents/employee.json`;
const options = {
method: 'GET'
}
fetch(url, options)
.then(response => response.json())
.then(data => {
const s3Options = {
method: 'GET'
}
fetch(data.url, s3Options).then(
response => response.json()
).then(data => {
const a = document.createElement('a');
const file = new Blob([JSON.stringify(data)], {type: 'application/json'});
a.href= URL.createObjectURL(file);
a.download = 'employee';
a.click()
URL.revokeObjectURL(a.href)
})
})
}
const FormikForm = () => {
let initialValues = {
firstName: '',
lastName: '',
email: '',
employeeId: '',
gender: '',
dob: '',
};
return (
<div className='App'>
<header className='App-header'>
<div>ABC Company Employee Portal</div>
</header>
<div className='body'>
<div className='query-form'>
<Formik
initialValues={initialValues}
validate={(values) => {
const errors = {};
if (!values.email) {
errors.email = 'Required';
} else if (
!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(values.email)
) {
errors.email = 'Invalid email address';
}
return errors;
}}
onSubmit={(values, { setSubmitting }) => {
handleOnSubmit(values);
setSubmitting(false);
values = {};
}}>
{({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
/* and other goodies */
}) => (
<form onSubmit={handleSubmit}>
<div className='dataField'>
<label>First Name</label>
<input
className='input'
type='text'
id='firstName'
value={values.firstName}
onChange={handleChange}
onBlur={handleBlur}
/>
</div>
<div className='dataField'>
<label>Last Name</label>
<input
className='input'
type='text'
id='lastName'
value={values.lastName}
onChange={handleChange}
onBlur={handleBlur}
/>
</div>
<div className='dataField'>
<label>Email</label>
<input
className='input'
type='text'
id='email'
value={values.email}
onChange={handleChange}
onBlur={handleBlur}
/>
</div>
<div className='dataField'>
<label>Employee ID</label>
<input
className='input'
type='text'
id='employeeId'
value={values.employeeId}
onChange={handleChange}
onBlur={handleBlur}
/>
</div>
<div className='dataField'>
<label>Gender</label>
<input
className='input'
type='text'
id='gender'
value={values.gender}
onChange={handleChange}
onBlur={handleBlur}
/>
</div>
<div className='dataField'>
<label>Date of Birth</label>
<input
className='input'
type='date'
id='dob'
value={values.dob}
onChange={handleChange}
onBlur={handleBlur}
/>
</div>
{errors.firstName && touched.firstName && errors.firstName}
{errors.lastName && touched.lastName && errors.lastName}
{errors.email && touched.email && errors.email}
{errors.employeeId && touched.employeeId && errors.employeeId}
<div className='button'>
<button
type='submit'
className='input'
disabled={isSubmitting}>
Upload Data
</button>
</div>
</form>
)}
</Formik>
</div>
<div className='button'>
<button
type='submit'
className='input'
onClick={downloadFromS3}>
Download Data
</button>
</div>
</div>
</div>
);
};
export default FormikForm;