-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-user.js
More file actions
272 lines (221 loc) · 10.1 KB
/
Copy pathadd-user.js
File metadata and controls
272 lines (221 loc) · 10.1 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
261
262
263
264
265
266
267
268
269
270
271
272
import {Url} from '../url/url';
class AddUserPage {
constructor(store, accountName) {
this.setStateAddUser = () => {
const {setState} = store;
const initializeState = {
stateName: 'ADD USER',
activePage: this.render(),
};
setState(initializeState);
window.history.pushState(initializeState.activePage, initializeState.stateName);
}
this.url = new Url(accountName);
}
render() {
return /*html*/`
<div id="add-user-page">
<header class="container">
<div class="row d-flex justify-content-center">
<span class="add-user-header">Add new user</span>
</div>
</header>
<main class="container add-user-block">
<form>
<div class="form-group">
<label for="fullName-input"><span class="required">*</span>Full name:</label>
<input type="text" name="fullName" class="form-control" id="fullName-input" placeholder="Enter full name" required>
</div>
<div class="form-group">
<label for="email-input"><span class="required">*</span>Email address:</label>
<input type="email" name="email" class="form-control" id="email-input" placeholder="Enter email" required>
</div>
<div class="form-group">
<label for="phoneNumber-input"><span class="required">*</span>Phone Number:</label>
<input type="number" name="phone" class="form-control" id="phoneNumber-input" placeholder="Enter phone number" required>
</div>
<div class="form-group">
<label for="birthDate-input">Birth date:</label>
<input type="date" name="birthdate" class="form-control" id="birthDate-input">
</div>
<div class="form-group">
<label for="address-input">Address:</label>
<input type="text" name="address" class="form-control" id="address-input" placeholder="Enter address">
</div>
<div class="form-group">
<label for="gender-selection">Gender:</label>
<select name="gender" class="form-control" id="gender-selection">
<option>Male</option>
<option>Female</option>
</select>
</div>
<div class="form-group">
<label for="avatarUrl-input">Avatar url:</label>
<input type="text" name="avatarUrl" class="form-control" id="avatarUrl-input" aria-describedby="avatarHelp" placeholder="Enter avatar url">
<small id="avatarHelp" class="form-text text-muted">ex: https://cloud-drive/photo123456.</small>
</div>
<div class="row d-flex justify-content-center">
<button type="submit" class="btn btn-primary">Add user</button>
</div>
</form>
</main>
</div>
`;
}
applyListenersForAddUserPage() {
const addUserForm = document.querySelector('form');
const handlerForInputs = (e) => {
if(e.target.name === 'fullName') {
const VALUE = e.target.value;
if(VALUE.length === 0) {
e.target.classList.remove('wrong')
e.target.classList.remove('correct')
return;
}
if(this.isValidFullName(VALUE)) {
e.target.classList.add('correct')
e.target.classList.remove('wrong')
} else {
e.target.classList.add('wrong')
e.target.classList.remove('correct')
}
}
if(e.target.name === 'email') {
const VALUE = e.target.value;
if(VALUE.length === 0) {
e.target.classList.remove('wrong')
e.target.classList.remove('correct')
return;
}
if(this.isValidEmail(VALUE)) {
e.target.classList.add('correct')
e.target.classList.remove('wrong')
} else {
e.target.classList.add('wrong')
e.target.classList.remove('correct')
}
}
if(e.target.name === 'phone') {
const VALUE = e.target.value;
if(VALUE.length === 0) {
e.target.classList.remove('wrong')
e.target.classList.remove('correct')
return;
}
if(VALUE.length > 9) {
e.target.classList.add('correct')
e.target.classList.remove('wrong')
} else {
e.target.classList.add('wrong')
e.target.classList.remove('correct')
}
}
if(e.target.name === 'birthdate') {
const VALUE = e.target.value;
if(VALUE.length === 0) {
e.target.classList.remove('correct')
return;
}
if(VALUE.length > 9) {
e.target.classList.add('correct')
}
}
if(e.target.name === 'address') {
const VALUE = e.target.value;
if(VALUE.length === 0) {
e.target.classList.remove('correct')
return;
}
if(VALUE.length > 0) {
e.target.classList.add('correct')
}
}
if(e.target.name === "avatarUrl") {
const VALUE = e.target.value;
if(VALUE.length === 0) {
e.target.classList.remove('wrong')
e.target.classList.remove('correct')
return;
}
if(this.isValidURL(VALUE)) {
e.target.classList.add('correct')
e.target.classList.remove('wrong')
} else {
e.target.classList.add('wrong')
e.target.classList.remove('correct')
}
}
};
addUserForm.addEventListener('input', handlerForInputs)
const inputs = [...addUserForm.elements]
.filter(elem => elem.tagName === 'INPUT' || elem.tagName === 'SELECT');
const handlerForSubmit = (e) => {
e.preventDefault();
const user = inputs.reduce((newUser, input) => {
if(input.classList.contains('wrong')) {
alert(`${input.name} is incorrect!`);
return;
};
if(input.value.length !== 0
&& input.name !== 'phone'
&& input.name !== 'gender'
&& input.name !== 'fullName'
) {
newUser[input.name] = input.value;
}
if(input.value.length !== 0 && input.name === 'phone') {
newUser[input.name] = input.value.replace(/(.{3})(.{3})(.{2})/g, '($1) $2-$3-');
}
if(input.name === 'gender') {
input.value === "Male"
? newUser[input.name] = "M"
: newUser[input.name] = "F"
}
if(input.value.length !== 0 && input.name === 'fullName') {
const formatedFullName = input.value.split(' ').reduce((output, word, index) => {
const splitedWord = word.toLowerCase().split('');
const firstLetter = splitedWord[0].toUpperCase();
splitedWord[0] = firstLetter;
output += splitedWord.join('');
if(index === 0) {
output += ' ';
}
return output;
}, '');
newUser[input.name] = formatedFullName;
}
return newUser;
}, {})
if(user) {
this.url.postUser(user);
inputs.forEach(input => {
if(input.tagName !== "SELECT") {
input.value = "";
input.classList.remove('correct');
}
})
} else {
alert('Something is incorrect!')
}
}
addUserForm.addEventListener('submit', handlerForSubmit)
}
isValidFullName(value) {
const splitedValue = value.split(' ');
return splitedValue.length === 2 && splitedValue[0].length > 0 && splitedValue[1].length > 0;
}
isValidEmail(value) {
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(value).toLowerCase());
}
isValidURL(value) {
const re = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return re.test(String(value).toLowerCase());
}
}
export {AddUserPage};