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

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