-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (111 loc) · 4.21 KB
/
index.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
class Form {
constructor(formWrapper) {
this.form = formWrapper;
this.fields = this.form.getElementsByTagName('input');
this.submitButton = document.getElementById('submitButton');
this.resultContainer = document.getElementById('resultContainer');
this.submitButton.addEventListener('click', event => {
event.preventDefault();
this.submit();
})
}
validate() {
const validateEmail = email => /^[a-zA-Z0-9.-]+@(yandex.(ru|com|ua|kz|by)|ya.ru)$/.test(email);
const validatePhone = phone => /^\+7\(\d{3}\)\d{3}-\d{2}-\d{2}$/.test(phone) && phone.match(/\d/g).reduce((p, c) => p + parseInt(c, 10), 0) <= 30;
const validateFio = fio => /^([A-zА-яЁё]+\s[A-zА-яЁё]+){2}$/g.test(fio);
const errorFields = [];
if (!validateFio(this.fields.fio.value)) {
errorFields.push('fio');
}
if (!validateEmail(this.fields.email.value)) {
errorFields.push('email');
}
if (!validatePhone(this.fields.phone.value)) {
errorFields.push('phone');
}
return {
isValid: errorFields.length === 0,
errorFields
}
}
getData() {
let result = {};
for (let i = 0; i < this.fields.length; i++) {
result[this.fields[i].name] = this.fields[i].value;
}
return result;
}
setData(fields) {
Object.keys(fields).forEach(key => {
if (this.fields[key]) {
this.fields[key].value = fields[key]
}
});
}
submit() {
// очистка стилей
for (let i = 0; i < this.fields.length; i++) {
this.fields[i].className = '';
}
this.resultContainer.className = '';
this.resultContainer.innerHTML = '';
// валидация
const validationResult = this.validate();
if (!validationResult.isValid) {
Object.keys(validationResult.errorFields).forEach(key => {
const field = validationResult.errorFields[key];
if (this.fields[field]) {
this.fields[field].classList.add('error');
}
});
return false;
}
this.submitButton.setAttribute('disabled', 'disabled');
// начало запроса
return performRequest(this.form.getAttribute('action'), this.getData()).then(response => {
this.submitButton.removeAttribute('disabled');
switch (response.status) {
case 'success':
this.resultContainer.classList.add('success');
this.resultContainer.innerHTML = 'Success';
break;
case 'progress':
this.resultContainer.classList.add('progress');
this.resultContainer.innerHTML = 'Retry in ' + response.timeout + 'ms';
setTimeout(this.submit.bind(this), response.timeout);
break;
case 'error':
this.resultContainer.classList.add('error');
this.resultContainer.innerHTML = response.reason || 'Unknown error';
break;
default:
this.resultContainer.innerHTML = 'Unknown result';
break;
}
});
}
}
function performRequest(url, data) {
return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest();
const formData = new FormData();
for (let key in data) {
if (data.hasOwnProperty(key)) {
formData.append(key, data[key]);
}
}
xhr.open('POST', url, true);
xhr.onload = function() {
if (xhr.status === 200) {
return resolve(JSON.parse(xhr.responseText));
} else {
return reject(new Error('Request didn\'t perform successfully; error code:' + xhr.statusText));
}
};
xhr.onerror = function(error) {
return reject(error);
};
xhr.send(formData);
})
}
window.MyForm = new Form(document.getElementById('MyForm'));