-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddPerson.js
456 lines (438 loc) · 17.4 KB
/
AddPerson.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import React from "react";
import * as ajaxcalls from "../services/people.service";
import Ribbon from "../components/Ribbon";
// import PageHeader from "../components/PageHeader";
import FormPanel from "../components/FormPanel";
import {
FormField,
FormFieldConfig,
validate as formFieldValidate
} from "../helpers/form.helper";
import Notifier from "../helpers/notifier";
import PageHeader from "../components/PageHeader";
class AddPerson extends React.Component {
static defaultProps = {
formData: {
firstName: "",
lastName: "",
middleName: "",
prefixName: "",
suffixName: "",
publicEmail: "",
phoneNumber: "",
address: "",
userId: "",
receiveSms: false
}
};
static formDataConfig = {
firstName: new FormFieldConfig("First Name", {
required: { value: true, message: "First Name is required" },
maxLength: { value: 50 }
}),
lastName: new FormFieldConfig("Last Name", {
required: { value: true, message: "Last Name is required" },
maxLength: { value: 50 }
}),
middleName: new FormFieldConfig("Middle Name", {
maxLength: { value: 50 }
}),
suffixName: new FormFieldConfig("Suffix Name", {
maxLength: { value: 5 }
}),
prefixName: new FormFieldConfig("prefix Name", {
maxLength: { value: 5 }
}),
publicEmail: new FormFieldConfig("Public Email", {
required: { value: true, message: "Email is required" },
pattern: {
value: /[\w\d]+@[\w\d]+\.\w+/,
message: "Must be a valid email"
}
}),
phoneNumber: new FormFieldConfig("Phone Number", {
number: { value: true, message: "Must enter numbers only" },
maxLength: { value: 10, message: "Must not exceed 10 numbers" },
required: { value: true }
}),
address: new FormFieldConfig("Address", {
maxLength: { value: 100 }
}),
userId: new FormFieldConfig("Login Id"),
receiveSms: new FormFieldConfig("Receive SMS")
};
constructor(props) {
super(props);
const formFields = this.convertPropsToFormFields();
this.state = {
formFields: formFields,
formValid: this.validateForm(formFields)
};
this.onChange = this.onChange.bind(this);
this.handleAdd = this.handleAdd.bind(this);
}
// Returns true if every field is valid
validateForm(formFields) {
return Object.values(formFields).reduce((valid, formField) => {
return valid && formField.valid;
}, true);
}
// maps model values to full-up FormFields
convertPropsToFormFields() {
let person = AddPerson.defaultProps.formData;
const formFields = {
firstName: new FormField(person.firstName),
lastName: new FormField(person.lastName),
middleName: new FormField(person.middleName),
prefixName: new FormField(person.prefixName),
suffixName: new FormField(person.suffixName),
publicEmail: new FormField(person.publicEmail),
phoneNumber: new FormField(person.phoneNumber),
address: new FormField(person.address),
userId: new FormField(person.userId),
receiveSms: new FormField(person.receiveSms)
};
// Loop through validation for each field
for (let fieldName in formFields) {
let field = formFields[fieldName];
let config = AddPerson.formDataConfig[fieldName];
formFieldValidate(field, config);
}
return formFields;
}
onChange(event) {
const value =
event.target.type === "checkbox"
? event.target.checked
: event.target.value;
const name = event.target.name;
const config = AddPerson.formDataConfig[name];
this.setState(prevState => {
const field = { ...prevState.formFields[name] };
field.value = value;
field.touched = true;
formFieldValidate(field, config);
const formFields = { ...prevState.formFields, [name]: field };
let formValid = this.validateForm(formFields);
return { formFields: formFields, formValid: formValid };
});
}
handleAdd(event) {
event.preventDefault();
if (!this.state.formValid) {
// Mark all fields as touched to display validation errors for all fields
const formFields = JSON.parse(JSON.stringify(this.state.formFields));
for (let fieldIdentifier in formFields) {
formFields[fieldIdentifier].touched = true;
}
this.setState({ formData: formFields });
return;
}
const newPerson = {
firstName: this.state.formFields.firstName.value,
lastName: this.state.formFields.lastName.value,
middleName: this.state.formFields.middleName.value,
prefixName: this.state.formFields.prefixName.value,
suffixName: this.state.formFields.suffixName.value,
publicEmail: this.state.formFields.publicEmail.value,
phoneNumber: this.state.formFields.phoneNumber.value,
address: this.state.formFields.address.value,
userId: this.state.formFields.userId.value,
receiveSms: this.state.formFields.receiveSms.value,
roles: []
};
ajaxcalls
.post(newPerson)
.then(data => {
console.log(data, "Person Added");
this.setState({ person: data.items });
this.setState({
firstName: "",
lastName: "",
middleName: "",
prefixName: "",
suffixName: "",
publicEmail: "",
phoneNumber: "",
address: "",
userId: "",
receiveSms: this.state.receiveSms
});
Notifier.success("Person Added");
window.location.href = "/People";
})
.catch(() => {
console.log("error");
Notifier.error("Unable to Add Person");
});
}
renderErrorMsgs(field) {
return !field.valid && field.touched
? field.brokenRules.map(br => {
return (
<div key={br.rule} className="note note-error">
{br.msg}
</div>
);
})
: null;
}
inputClassName(field) {
return !field.valid && field.touched ? "input state-error" : "input";
}
render() {
return (
<React.Fragment>
<Ribbon breadcrumbArray={["People", "Add Person"]} />
<PageHeader
iconClasses="fa fa-lg fa-fw fa-user-plus"
title=" Add New Person"
/>
<div className="container-fluid">
<div className="row">
<div className="col-lg-12">
<FormPanel>
<article>
<div>
<form id="smart-form-register" className="smart-form">
<fieldset>
<div className="row well">
<header>Personal:</header>
<fieldset>
<div className="row">
<section className="col col-2">
<label
className={this.inputClassName(
this.state.formFields.prefixName
)}
>
Prefix Name
<input
type="text"
name="prefixName"
onChange={this.onChange}
value={
this.state.formFields.prefixName.value
}
/>
</label>
{this.renderErrorMsgs(
this.state.formFields.prefixName
)}
</section>
<section className="col col-6">
<label
className={this.inputClassName(
this.state.formFields.firstName
)}
>
First Name
<input
type="text"
name="firstName"
onChange={this.onChange}
value={
this.state.formFields.firstName.value
}
/>
</label>
{this.renderErrorMsgs(
this.state.formFields.firstName
)}
</section>
</div>
</fieldset>
<fieldset>
<div className="row">
<section className="col col-6">
<label
className={this.inputClassName(
this.state.formFields.middleName
)}
>
Middle Name
<input
type="text"
name="middleName"
onChange={this.onChange}
value={
this.state.formFields.middleName.value
}
/>
</label>
{this.renderErrorMsgs(
this.state.formFields.middleName
)}
</section>
<section className="col col-6">
<label
className={this.inputClassName(
this.state.formFields.lastName
)}
>
Last Name
<input
type="text"
name="lastName"
size="2"
onChange={this.onChange}
value={this.state.formFields.lastName.value}
/>
</label>
{this.renderErrorMsgs(
this.state.formFields.lastName
)}
</section>
</div>
</fieldset>
<section className="col col-2">
<label
className={this.inputClassName(
this.state.formFields.suffixName
)}
>
Suffix Name
<input
type="text"
name="suffixName"
onChange={this.onChange}
value={this.state.formFields.suffixName.value}
/>
</label>
{this.renderErrorMsgs(
this.state.formFields.suffixName
)}
</section>
</div>
</fieldset>
<fieldset>
<div className="row well">
<header>Contact Information:</header>
<fieldset>
<div className="row">
<section className="col col-6">
<label
className={this.inputClassName(
this.state.formFields.publicEmail
)}
>
Public Email
<input
type="text"
name="publicEmail"
onChange={this.onChange}
value={
this.state.formFields.publicEmail.value
}
/>
</label>
{this.renderErrorMsgs(
this.state.formFields.publicEmail
)}
</section>
<section className="col col-6">
<label
className={this.inputClassName(
this.state.formFields.phoneNumber
)}
>
Phone Number
<input
type="text"
name="phoneNumber"
onChange={this.onChange}
value={
this.state.formFields.phoneNumber.value
}
/>
</label>
{this.renderErrorMsgs(
this.state.formFields.phoneNumber
)}
<div className="checkbox">
<section>
<label className="checkbox">
<input
type="checkbox"
name="receiveSms"
value={true}
checked={
this.state.formFields.receiveSms.value
}
onChange={this.onChange}
/>
<i />Receive SMS Notifications
</label>
</section>
</div>
</section>
</div>
</fieldset>
<fieldset>
<div className="row">
<section className="col col-6">
<label
className={this.inputClassName(
this.state.formFields.address
)}
>
Address
<input
type="text"
name="address"
onChange={this.onChange}
value={this.state.formFields.address.value}
/>
</label>
{this.renderErrorMsgs(
this.state.formFields.address
)}
</section>
</div>
</fieldset>
<fieldset>
<div className="row">
<section className="col col-2">
<label
className={this.inputClassName(
this.state.formFields.userId
)}
>
Login ID
<input
type="text"
name="userId"
value={this.state.formFields.userId.value}
onChange={this.onChange}
title="created after add"
/>
</label>
{this.renderErrorMsgs(
this.state.formFields.userId
)}
</section>
</div>
</fieldset>
</div>
</fieldset>
<footer>
<button
className="btn btn-primary"
onClick={this.handleAdd}
>
{" "}
Add Person
</button>
</footer>
</form>
</div>
</article>
</FormPanel>
</div>
</div>
</div>
</React.Fragment>
);
}
}
export default AddPerson;