@websy/websy-designs 1.4.15 → 1.4.16

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.
@@ -2202,25 +2202,26 @@ class WebsyForm {
2202
2202
  checkRecaptcha () {
2203
2203
  return new Promise((resolve, reject) => {
2204
2204
  if (this.options.useRecaptcha === true) {
2205
- // if (this.recaptchaValue) {
2206
- grecaptcha.ready(() => {
2207
- grecaptcha.execute(ENVIRONMENT.RECAPTCHA_KEY, { action: 'submit' }).then(token => {
2208
- this.apiService.add('google/checkrecaptcha', {grecaptcharesponse: token}).then(response => {
2209
- if (response.success && response.success === true) {
2210
- resolve(true)
2211
- }
2212
- else {
2213
- reject(false)
2214
- }
2215
- })
2216
- }, err => {
2217
- reject(err)
2205
+ if (this.recaptchaValue) {
2206
+ // grecaptcha.ready(() => {
2207
+ // grecaptcha.execute(this.recaptchaValue, { action: 'submit' }).then(token => {
2208
+ this.apiService.add('google/checkrecaptcha', {grecaptcharesponse: this.recaptchaValue}).then(response => {
2209
+ if (response.success && response.success === true) {
2210
+ resolve(true)
2211
+ grecaptcha.reset(`${this.elementId}_recaptcha`, {sitekey: ENVIRONMENT.RECAPTCHA_KEY})
2212
+ }
2213
+ else {
2214
+ resolve(false)
2215
+ }
2218
2216
  })
2219
- })
2220
- // }
2221
- // else {
2222
- // reject(false)
2223
- // }
2217
+ // }, err => {
2218
+ // reject(err)
2219
+ // })
2220
+ // })
2221
+ }
2222
+ else {
2223
+ resolve(false)
2224
+ }
2224
2225
  }
2225
2226
  else {
2226
2227
  resolve(true)
@@ -2290,10 +2291,12 @@ class WebsyForm {
2290
2291
  recaptchaReady () {
2291
2292
  const el = document.getElementById(`${this.elementId}_recaptcha`)
2292
2293
  if (el) {
2293
- grecaptcha.render(`${this.elementId}_recaptcha`, {
2294
- sitekey: ENVIRONMENT.RECAPTCHA_KEY,
2295
- callback: this.validateRecaptcha.bind(this)
2296
- })
2294
+ grecaptcha.ready(() => {
2295
+ grecaptcha.render(`${this.elementId}_recaptcha`, {
2296
+ sitekey: ENVIRONMENT.RECAPTCHA_KEY,
2297
+ callback: this.validateRecaptcha.bind(this)
2298
+ })
2299
+ })
2297
2300
  }
2298
2301
  }
2299
2302
  render (update, data) {
@@ -2348,6 +2351,12 @@ class WebsyForm {
2348
2351
  `
2349
2352
  }
2350
2353
  })
2354
+ if (this.options.useRecaptcha === true) {
2355
+ html += `
2356
+ --><div id='${this.elementId}_recaptcha' data-sitekey='${ENVIRONMENT.RECAPTCHA_KEY}' class='websy-form-recaptcha'></div>
2357
+ <div id='${this.elementId}_recaptchaError' class='websy-alert websy-alert-error websy-hidden'>Invalid recaptcha response</div><!--
2358
+ `
2359
+ }
2351
2360
  html += `
2352
2361
  --><button class="websy-btn submit ${this.options.submit.classes || ''}">${this.options.submit.text || 'Save'}</button>${this.options.cancel ? '<!--' : ''}
2353
2362
  `
@@ -2360,24 +2369,30 @@ class WebsyForm {
2360
2369
  </form>
2361
2370
  <div id="${this.elementId}_validationFail" class="websy-validation-failure"></div>
2362
2371
  `
2363
- if (this.options.useRecaptcha === true) {
2364
- html += `
2365
- <div id='${this.elementId}_recaptcha'></div>
2366
- `
2367
- }
2368
2372
  el.innerHTML = html
2369
2373
  this.processComponents(componentsToProcess, () => {
2370
2374
  if (this.options.useRecaptcha === true && typeof grecaptcha !== 'undefined') {
2371
- // this.recaptchaReady()
2375
+ this.recaptchaReady()
2372
2376
  }
2373
2377
  })
2374
2378
  }
2375
2379
  }
2376
2380
  submitForm () {
2377
2381
  const formEl = document.getElementById(`${this.elementId}Form`)
2382
+ const buttonEl = formEl.querySelector('button.websy-btn.submit')
2383
+ const recaptchErrEl = document.getElementById(`${this.elementId}_recaptchaError`)
2378
2384
  if (formEl.reportValidity() === true) {
2385
+ if (buttonEl) {
2386
+ buttonEl.setAttribute('disabled', true)
2387
+ }
2379
2388
  this.checkRecaptcha().then(result => {
2380
- if (result === true) {
2389
+ if (buttonEl) {
2390
+ buttonEl.removeAttribute('disabled')
2391
+ }
2392
+ if (result === true) {
2393
+ if (recaptchErrEl) {
2394
+ recaptchErrEl.classList.add('websy-hidden')
2395
+ }
2381
2396
  const formData = new FormData(formEl)
2382
2397
  const data = {}
2383
2398
  const temp = new FormData(formEl)
@@ -2397,6 +2412,7 @@ class WebsyForm {
2397
2412
  // this.render()
2398
2413
  formEl.reset()
2399
2414
  }
2415
+ buttonEl.removeAttribute('disabled')
2400
2416
  this.options.onSuccess.call(this, result)
2401
2417
  }, err => {
2402
2418
  console.log('Error submitting form data:', err)
@@ -2413,7 +2429,12 @@ class WebsyForm {
2413
2429
  }
2414
2430
  }
2415
2431
  else {
2416
- console.log('bad recaptcha')
2432
+ if (buttonEl) {
2433
+ buttonEl.removeAttribute('disabled')
2434
+ }
2435
+ if (recaptchErrEl) {
2436
+ recaptchErrEl.classList.remove('websy-hidden')
2437
+ }
2417
2438
  }
2418
2439
  })
2419
2440
  }
@@ -8602,7 +8623,7 @@ function recaptchaReadyCallBack () {
8602
8623
  // need a way of initializing these based on environment variables
8603
8624
  function useGoogleRecaptcha (key) {
8604
8625
  const rcs = document.createElement('script')
8605
- rcs.src = `//www.google.com/recaptcha/api.js?render=${key}`
8626
+ rcs.src = `//www.google.com/recaptcha/api.js`
8606
8627
  document.body.appendChild(rcs)
8607
8628
  }
8608
8629