@gudhub/ssg-web-components-library 1.0.106 → 1.0.108

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gudhub/ssg-web-components-library",
3
- "version": "1.0.106",
3
+ "version": "1.0.108",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -7,6 +7,11 @@ import { checkInputsValidations } from './inputsValidation.js';
7
7
  let recaptchaPromise = null;
8
8
 
9
9
  function loadRecaptcha(siteKey) {
10
+ if (!siteKey) {
11
+ console.warn('reCAPTCHA site key is missing');
12
+ return Promise.resolve(null);
13
+ }
14
+
10
15
  if (window.grecaptcha) return Promise.resolve(window.grecaptcha);
11
16
 
12
17
  if (recaptchaPromise) return recaptchaPromise;
@@ -19,10 +24,10 @@ function loadRecaptcha(siteKey) {
19
24
 
20
25
  script.onload = () => {
21
26
  if (window.grecaptcha) resolve(window.grecaptcha);
22
- else reject('grecaptcha not available');
27
+ else reject(new Error('grecaptcha not available'));
23
28
  };
24
29
 
25
- script.onerror = reject;
30
+ script.onerror = () => reject(new Error('Failed to load reCAPTCHA script'));
26
31
 
27
32
  document.head.appendChild(script);
28
33
  });
@@ -60,7 +65,9 @@ class GetInTouchForm extends GHComponent {
60
65
  this.initConfig(this.config);
61
66
  this.attachEventListeners();
62
67
 
63
- loadRecaptcha(this.recaptcha_site_key).catch(console.error);
68
+ if (this.recaptcha_site_key) {
69
+ loadRecaptcha(this.recaptcha_site_key).catch(console.error);
70
+ }
64
71
  }
65
72
  }
66
73
 
@@ -68,6 +75,8 @@ class GetInTouchForm extends GHComponent {
68
75
  this.initConfig(this.config);
69
76
  super.render(html);
70
77
  this.attachEventListeners();
78
+
79
+ loadRecaptcha(this.recaptcha_site_key).catch(console.error);
71
80
  }
72
81
 
73
82
  attachEventListeners() {
@@ -103,6 +112,11 @@ class GetInTouchForm extends GHComponent {
103
112
  }
104
113
 
105
114
  async getRecaptchaToken(action = 'submit') {
115
+ if (!this.recaptcha_site_key) {
116
+ console.warn('reCAPTCHA site key is missing');
117
+ return null;
118
+ }
119
+
106
120
  const grecaptcha = await loadRecaptcha(this.recaptcha_site_key);
107
121
 
108
122
  return new Promise((resolve, reject) => {
@@ -126,7 +140,7 @@ class GetInTouchForm extends GHComponent {
126
140
  try {
127
141
  const token = await this.getRecaptchaToken();
128
142
 
129
- await this.fetchExample(token);
143
+ await this.verifyRecaptcha(token);
130
144
 
131
145
  this.handleSuccessFormValidation(form, token);
132
146
 
@@ -158,7 +172,6 @@ class GetInTouchForm extends GHComponent {
158
172
  window.dispatchEvent(new CustomEvent('submitForm', { detail: { formDataObj } }));
159
173
 
160
174
  this.isFormSubmitted = true;
161
- this.removeLoader();
162
175
  };
163
176
 
164
177
  inputsValidation = async (form) => {
@@ -166,16 +179,25 @@ class GetInTouchForm extends GHComponent {
166
179
  return checkInputsValidations(inputs);
167
180
  }
168
181
 
169
- fetchExample = (recaptchaToken) => {
170
- const isSuccess = true;
182
+ verifyRecaptcha = async (recaptchaToken) => {
183
+ const response = await fetch('/api/verify-recaptcha', {
184
+ method: 'POST',
185
+ headers: {
186
+ 'Content-Type': 'application/json'
187
+ },
188
+ body: JSON.stringify({
189
+ token: recaptchaToken,
190
+ action: 'submit'
191
+ })
192
+ });
171
193
 
172
- return new Promise((resolve, reject) => {
173
- console.log('reCAPTCHA token:', recaptchaToken);
194
+ const data = await response.json();
174
195
 
175
- setTimeout(() => {
176
- isSuccess ? resolve() : reject();
177
- }, 2000);
178
- });
196
+ if (!response.ok || !data.success) {
197
+ throw new Error(data?.error || 'reCAPTCHA verification failed');
198
+ }
199
+
200
+ return data;
179
201
  }
180
202
 
181
203
  createDataObject(form, placement, recaptchaToken) {
@@ -215,15 +237,20 @@ class GetInTouchForm extends GHComponent {
215
237
  setTimeout(() => btn.disabled = false, 500);
216
238
  }
217
239
 
218
- showSuccess({email, phone}) {
219
- if (email) {
220
- this.querySelector('.check_entity').classList.add('provided');
221
- this.getElementsByClassName('email')[0].innerText = email;
240
+ showSuccess({ email, phone }) {
241
+ const emailEntity = this.querySelector('.check_entity');
242
+ const phoneEntity = this.querySelector('.phone_entity');
243
+ const emailEl = this.getElementsByClassName('email')[0];
244
+ const phoneEl = this.getElementsByClassName('phone')[0];
245
+
246
+ if (email && emailEntity && emailEl) {
247
+ emailEntity.classList.add('provided');
248
+ emailEl.innerText = email;
222
249
  }
223
250
 
224
- if (phone) {
225
- this.querySelector('.phone_entity').classList.add('provided');
226
- this.getElementsByClassName('phone')[0].innerText = phone;
251
+ if (phone && phoneEntity && phoneEl) {
252
+ phoneEntity.classList.add('provided');
253
+ phoneEl.innerText = phone;
227
254
  }
228
255
 
229
256
  this.classList.add('success');
@@ -234,9 +261,16 @@ class GetInTouchForm extends GHComponent {
234
261
  }
235
262
 
236
263
  hideSuccess() {
237
- this.getElementsByClassName('email')[0].innerText = '';
238
- this.getElementsByClassName('phone')[0].innerText = '';
239
- this.querySelector('.phone_entity').classList.remove('provided');
264
+ const emailEl = this.getElementsByClassName('email')[0];
265
+ const phoneEl = this.getElementsByClassName('phone')[0];
266
+ const phoneEntity = this.querySelector('.phone_entity');
267
+ const emailEntity = this.querySelector('.check_entity');
268
+
269
+ if (emailEl) emailEl.innerText = '';
270
+ if (phoneEl) phoneEl.innerText = '';
271
+ if (phoneEntity) phoneEntity.classList.remove('provided');
272
+ if (emailEntity) emailEntity.classList.remove('provided');
273
+
240
274
  this.classList.remove('success');
241
275
  }
242
276