@alexsab-ru/scripts 0.6.1 → 0.7.0
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 +14 -11
- package/package.json +1 -1
package/lib/form.js
CHANGED
|
@@ -22,17 +22,14 @@ export function maskphone(e) {
|
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
export const phoneChecker = (phone) => {
|
|
25
|
-
let form = phone.closest("form")
|
|
26
|
-
btn = form.querySelector('[type="submit"]');
|
|
25
|
+
let form = phone.closest("form");
|
|
27
26
|
if (!phone.value.length) {
|
|
28
27
|
showErrorMes(form, ".phone", "Телефон является обязательным полем");
|
|
29
|
-
stateBtn(btn, "Отправить");
|
|
30
28
|
return false;
|
|
31
29
|
} else {
|
|
32
30
|
const phoneRe = new RegExp(/^\+7 [0-9]{3} [0-9]{3}-[0-9]{2}-[0-9]{2}$/);
|
|
33
31
|
if (!phoneRe.test(phone.value) || noValidPhone(phone.value)) {
|
|
34
32
|
showErrorMes(form, ".phone", "Введен некорректный номер телефона");
|
|
35
|
-
stateBtn(btn, "Отправить");
|
|
36
33
|
return false;
|
|
37
34
|
}
|
|
38
35
|
}
|
|
@@ -121,6 +118,7 @@ const showMessageModal = (messageModal, icon, message) => {
|
|
|
121
118
|
const propsParams = {
|
|
122
119
|
callback: null,
|
|
123
120
|
callback_error: null,
|
|
121
|
+
validation: null,
|
|
124
122
|
ct_routeKey: '',
|
|
125
123
|
confirmModalText: '',
|
|
126
124
|
verbose: false,
|
|
@@ -131,7 +129,7 @@ export const connectForms = (url, props = propsParams) => {
|
|
|
131
129
|
|
|
132
130
|
async function submitForm(form){
|
|
133
131
|
const btn = form.querySelector('[type="submit"]');
|
|
134
|
-
|
|
132
|
+
const btnText = btn.value || btn.innerText;
|
|
135
133
|
const agree = form.querySelector('[name="agree"]');
|
|
136
134
|
const phone = form.querySelector('[name="phone"]');
|
|
137
135
|
const errorIcon =
|
|
@@ -147,13 +145,18 @@ async function submitForm(form){
|
|
|
147
145
|
return;
|
|
148
146
|
}
|
|
149
147
|
|
|
148
|
+
if (props.validation && typeof props.validation === 'function') {
|
|
149
|
+
props.validation(form);
|
|
150
|
+
}
|
|
151
|
+
|
|
150
152
|
// если флажок не установлен - фронт
|
|
151
153
|
if (!agree.checked) {
|
|
152
154
|
showErrorMes(form, ".agree", "Чтобы продолжить, установите флажок");
|
|
153
|
-
stateBtn(btn, "Отправить");
|
|
154
155
|
return;
|
|
155
156
|
}
|
|
156
157
|
|
|
158
|
+
stateBtn(btn, "Отправляем...", true);
|
|
159
|
+
|
|
157
160
|
// Отпрвка цели что форма submit только после всех проверок
|
|
158
161
|
reachGoal("form_submit");
|
|
159
162
|
|
|
@@ -222,7 +225,7 @@ async function submitForm(form){
|
|
|
222
225
|
.then((res) => res.json())
|
|
223
226
|
.then((data) => {
|
|
224
227
|
props.verbose && console.log(data);
|
|
225
|
-
stateBtn(btn,
|
|
228
|
+
stateBtn(btn, btnText);
|
|
226
229
|
if (data.answer == "required") {
|
|
227
230
|
reachGoal("form_required");
|
|
228
231
|
showErrorMes(form, data.field, data.message);
|
|
@@ -232,7 +235,7 @@ async function submitForm(form){
|
|
|
232
235
|
|
|
233
236
|
// Вызов callback_error при ошибке
|
|
234
237
|
if (props.callback_error && typeof props.callback_error === 'function') {
|
|
235
|
-
props.callback_error();
|
|
238
|
+
props.callback_error(data);
|
|
236
239
|
} else if (messageModal) {
|
|
237
240
|
showMessageModal(messageModal, errorIcon, errorText + "<br>" + data.error);
|
|
238
241
|
}
|
|
@@ -242,7 +245,7 @@ async function submitForm(form){
|
|
|
242
245
|
setCookie('SEND_MAIL', true, {'domain': window.location.hostname,'path':'/','expires':600});
|
|
243
246
|
// Вызов callback при успехе
|
|
244
247
|
if (props.callback && typeof props.callback === 'function') {
|
|
245
|
-
props.callback();
|
|
248
|
+
props.callback(data);
|
|
246
249
|
} else if (messageModal) {
|
|
247
250
|
showMessageModal(messageModal, successIcon, successText);
|
|
248
251
|
}
|
|
@@ -255,11 +258,11 @@ async function submitForm(form){
|
|
|
255
258
|
deleteCookie('SEND_MAIL');
|
|
256
259
|
// Вызов callback_error при ошибке
|
|
257
260
|
if (props.callback_error && typeof props.callback_error === 'function') {
|
|
258
|
-
props.callback_error();
|
|
261
|
+
props.callback_error(data);
|
|
259
262
|
} else if (messageModal) {
|
|
260
263
|
showMessageModal(messageModal, errorIcon, errorText + "<br>" + error);
|
|
261
264
|
}
|
|
262
|
-
stateBtn(btn,
|
|
265
|
+
stateBtn(btn, btnText);
|
|
263
266
|
});
|
|
264
267
|
return false;
|
|
265
268
|
}
|