@alexsab-ru/scripts 0.4.0 → 0.4.2

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.
Files changed (2) hide show
  1. package/lib/form.js +37 -11
  2. 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('input[type="submit"]');
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.value = value;
95
- btn.disabled = disable;
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('input[type="submit"]');
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
- let successText = '<b class="text-bold block text-2xl mb-4">Спасибо!</b> В скором времени мы свяжемся с Вами!';
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)) {
@@ -163,6 +172,10 @@ document.querySelectorAll("form").forEach((form) => {
163
172
  window.location.origin + window.location.pathname
164
173
  );
165
174
 
175
+ if(window.re) {
176
+ formData.append("re", window.re);
177
+ }
178
+
166
179
  window.location.search
167
180
  .slice(1)
168
181
  .split("&")
@@ -190,7 +203,7 @@ document.querySelectorAll("form").forEach((form) => {
190
203
  })
191
204
  .then((res) => res.json())
192
205
  .then((data) => {
193
- console.log(data);
206
+ // console.log(data);
194
207
  stateBtn(btn, "Отправить");
195
208
  if (data.answer == "required") {
196
209
  reachGoal("form_required");
@@ -198,15 +211,22 @@ document.querySelectorAll("form").forEach((form) => {
198
211
  return;
199
212
  } else if (data.answer == "error") {
200
213
  reachGoal("form_error");
201
- showMessageModal(messageModal, errorIcon, errorText + "<br>" + data.error);
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
+ }
202
221
  return;
203
222
  } else {
204
223
  reachGoal("form_success", formDataObj);
205
- showMessageModal(messageModal, successIcon, successText);
206
224
 
207
- // Вызов callback в конце функции
225
+ // Вызов callback при успехе
208
226
  if (callback && typeof callback === 'function') {
209
227
  callback();
228
+ } else if (messageModal) {
229
+ showMessageModal(messageModal, successIcon, successText);
210
230
  }
211
231
  }
212
232
  form.reset();
@@ -214,7 +234,13 @@ document.querySelectorAll("form").forEach((form) => {
214
234
  .catch((error) => {
215
235
  reachGoal("form_error");
216
236
  console.error("Ошибка отправки данных формы: " + error);
217
- showMessageModal(messageModal, errorIcon, errorText + "<br>" + error);
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
+ }
218
244
  stateBtn(btn, "Отправить");
219
245
  });
220
246
  return false;
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@alexsab-ru/scripts",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
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
- "scripts": {
27
- "test": "echo \"Error: no test specified\" && exit 1"
28
- }
29
- }
28
+ "homepage": "https://github.com/alexsab-ru/scripts#readme"
29
+ }