@alexsab-ru/scripts 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/form.js +34 -12
- package/package.json +6 -6
package/lib/form.js
CHANGED
|
@@ -24,7 +24,7 @@ export function maskphone(e) {
|
|
|
24
24
|
|
|
25
25
|
export const phoneChecker = (phone) => {
|
|
26
26
|
let form = phone.closest("form"),
|
|
27
|
-
btn = form.querySelector('
|
|
27
|
+
btn = form.querySelector('[type="submit"]');
|
|
28
28
|
if (!phone.value.length) {
|
|
29
29
|
showErrorMes(form, ".phone", "Телефон является обязательным полем");
|
|
30
30
|
stateBtn(btn, "Отправить");
|
|
@@ -91,8 +91,17 @@ document.querySelectorAll("textarea").forEach(function (textarea) {
|
|
|
91
91
|
// BUTTON
|
|
92
92
|
// Состояние кнопки
|
|
93
93
|
const stateBtn = (btn, value, disable = false) => {
|
|
94
|
-
btn.
|
|
95
|
-
|
|
94
|
+
if (btn.tagName == 'INPUT') {
|
|
95
|
+
btn.value = value;
|
|
96
|
+
btn.disabled = disable;
|
|
97
|
+
} else {
|
|
98
|
+
btn.innerText = value;
|
|
99
|
+
if (disable) {
|
|
100
|
+
btn.setAttribute('disabled', true);
|
|
101
|
+
} else {
|
|
102
|
+
btn.removeAttribute('disabled');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
96
105
|
};
|
|
97
106
|
|
|
98
107
|
const showErrorMes = (form, el, text) => {
|
|
@@ -111,11 +120,11 @@ const showMessageModal = (messageModal, icon, message) => {
|
|
|
111
120
|
};
|
|
112
121
|
|
|
113
122
|
|
|
114
|
-
export const connectForms = (url, callback) => {
|
|
123
|
+
export const connectForms = (url, callback, callback_error) => {
|
|
115
124
|
|
|
116
125
|
// Отправка всех форм
|
|
117
126
|
document.querySelectorAll("form").forEach((form) => {
|
|
118
|
-
const btn = form.querySelector('
|
|
127
|
+
const btn = form.querySelector('[type="submit"]');
|
|
119
128
|
form.onsubmit = async (event) => {
|
|
120
129
|
event.preventDefault();
|
|
121
130
|
stateBtn(btn, "Отправляем...", true);
|
|
@@ -128,7 +137,7 @@ document.querySelectorAll("form").forEach((form) => {
|
|
|
128
137
|
'<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 52 52"><path fill="#279548" d="M26,0A26,26,0,1,0,52,26,26,26,0,0,0,26,0Zm9.6,17.5a1.94,1.94,0,0,1,2,2,2,2,0,1,1-2-2Zm-19.2,0a2,2,0,1,1-2,2A2,2,0,0,1,16.4,17.5ZM40.09,32.15a15.8,15.8,0,0,1-28.18,0,1,1,0,0,1,1.78-.9,13.81,13.81,0,0,0,24.62,0,1,1,0,1,1,1.78.9Z"></path></svg>';
|
|
129
138
|
const errorText =
|
|
130
139
|
'<b class="text-bold block text-2xl mb-4">Упс!</b> Что-то пошло не так. Перезагрузите страницу и попробуйте снова. ';
|
|
131
|
-
|
|
140
|
+
const successText = '<b class="text-bold block text-2xl mb-4">Спасибо!</b> В скором времени мы свяжемся с Вами!';
|
|
132
141
|
const messageModal = document.getElementById("message-modal");
|
|
133
142
|
|
|
134
143
|
if (!phoneChecker(phone)) {
|
|
@@ -145,7 +154,7 @@ document.querySelectorAll("form").forEach((form) => {
|
|
|
145
154
|
reachGoal("form_submit");
|
|
146
155
|
|
|
147
156
|
// Отправка заявки на обратный возов в CallTouch
|
|
148
|
-
createRequest("ct_callback", phone);
|
|
157
|
+
createRequest("ct_callback", phone.value);
|
|
149
158
|
|
|
150
159
|
let formData = new FormData(form);
|
|
151
160
|
if(getCookie('fta')) {
|
|
@@ -194,7 +203,7 @@ document.querySelectorAll("form").forEach((form) => {
|
|
|
194
203
|
})
|
|
195
204
|
.then((res) => res.json())
|
|
196
205
|
.then((data) => {
|
|
197
|
-
console.log(data);
|
|
206
|
+
// console.log(data);
|
|
198
207
|
stateBtn(btn, "Отправить");
|
|
199
208
|
if (data.answer == "required") {
|
|
200
209
|
reachGoal("form_required");
|
|
@@ -202,15 +211,22 @@ document.querySelectorAll("form").forEach((form) => {
|
|
|
202
211
|
return;
|
|
203
212
|
} else if (data.answer == "error") {
|
|
204
213
|
reachGoal("form_error");
|
|
205
|
-
|
|
214
|
+
|
|
215
|
+
// Вызов callback_error при ошибке
|
|
216
|
+
if (callback_error && typeof callback_error === 'function') {
|
|
217
|
+
callback_error();
|
|
218
|
+
} else if (messageModal) {
|
|
219
|
+
showMessageModal(messageModal, errorIcon, errorText + "<br>" + data.error);
|
|
220
|
+
}
|
|
206
221
|
return;
|
|
207
222
|
} else {
|
|
208
223
|
reachGoal("form_success", formDataObj);
|
|
209
|
-
showMessageModal(messageModal, successIcon, successText);
|
|
210
224
|
|
|
211
|
-
// Вызов callback
|
|
225
|
+
// Вызов callback при успехе
|
|
212
226
|
if (callback && typeof callback === 'function') {
|
|
213
227
|
callback();
|
|
228
|
+
} else if (messageModal) {
|
|
229
|
+
showMessageModal(messageModal, successIcon, successText);
|
|
214
230
|
}
|
|
215
231
|
}
|
|
216
232
|
form.reset();
|
|
@@ -218,7 +234,13 @@ document.querySelectorAll("form").forEach((form) => {
|
|
|
218
234
|
.catch((error) => {
|
|
219
235
|
reachGoal("form_error");
|
|
220
236
|
console.error("Ошибка отправки данных формы: " + error);
|
|
221
|
-
|
|
237
|
+
|
|
238
|
+
// Вызов callback_error при ошибке
|
|
239
|
+
if (callback_error && typeof callback_error === 'function') {
|
|
240
|
+
callback_error();
|
|
241
|
+
} else if (messageModal) {
|
|
242
|
+
showMessageModal(messageModal, errorIcon, errorText + "<br>" + error);
|
|
243
|
+
}
|
|
222
244
|
stateBtn(btn, "Отправить");
|
|
223
245
|
});
|
|
224
246
|
return false;
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexsab-ru/scripts",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "common libs for websites",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
6
9
|
"repository": {
|
|
7
10
|
"type": "git",
|
|
8
11
|
"url": "git+https://github.com/alexsab-ru/scripts.git"
|
|
@@ -22,8 +25,5 @@
|
|
|
22
25
|
"publishConfig": {
|
|
23
26
|
"access": "public"
|
|
24
27
|
},
|
|
25
|
-
"homepage": "https://github.com/alexsab-ru/scripts#readme"
|
|
26
|
-
|
|
27
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
28
|
-
}
|
|
29
|
-
}
|
|
28
|
+
"homepage": "https://github.com/alexsab-ru/scripts#readme"
|
|
29
|
+
}
|