-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REF] google_recaptcha: convert widget to interaction
- Loading branch information
Showing
1 changed file
with
33 additions
and
26 deletions.
There are no files selected for viewing
59 changes: 33 additions & 26 deletions
59
addons/google_recaptcha/static/src/interactions/recaptcha_form.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,44 @@ | ||
/** @odoo-module **/ | ||
|
||
import publicWidget from "@web/legacy/js/public/public_widget"; | ||
import { Interaction } from "@web/public/interaction"; | ||
import { registry } from "@web/core/registry"; | ||
|
||
import { ReCaptcha } from "@google_recaptcha/js/recaptcha"; | ||
import { addLoadingEffect } from "@web/core/utils/ui"; | ||
|
||
publicWidget.registry.reCaptcha = publicWidget.Widget.extend({ | ||
selector: "form[data-captcha]", | ||
events: { | ||
submit: "_onSubmit", | ||
}, | ||
export class RecaptchaForm extends Interaction { | ||
static selector = "form[data-captcha]"; | ||
dynamicContent = { | ||
_root: { "t-on-submit": this.onSubmit }, | ||
}; | ||
|
||
init() { | ||
this._super(...arguments); | ||
this._recaptcha = new ReCaptcha(); | ||
}, | ||
setup() { | ||
this.recaptcha = new ReCaptcha(); | ||
} | ||
|
||
async willStart() { | ||
this._recaptcha.loadLibs(); | ||
}, | ||
await this.recaptcha.loadLibs(); | ||
} | ||
|
||
async _onSubmit(event) { | ||
const btn = this.$('button[type="submit"]'); | ||
if (!btn.prop("disabled")) { | ||
btn.attr("disabled", "disabled"); | ||
btn.prepend('<i class="fa fa-circle-o-notch fa-spin"/> '); | ||
async onSubmit(ev) { | ||
const submitEl = this.el.querySelector("button[type='submit']"); | ||
if (!submitEl.classList.contains("disabled")) { | ||
addLoadingEffect(submitEl); | ||
} | ||
if (!this.$el.find("input[name='recaptcha_token_response']")[0]) { | ||
event.preventDefault(); | ||
if (!this.el.querySelector("input[name='recaptcha_token_response']")) { | ||
ev.preventDefault(); | ||
const action = this.el.dataset.captcha || "generic"; | ||
const tokenCaptcha = await this._recaptcha.getToken(action); | ||
this.$el.append( | ||
`<input name="recaptcha_token_response" type="hidden" value="${tokenCaptcha.token}"/>`, | ||
); | ||
this.$el.submit(); | ||
const tokenCaptcha = await this.waitFor(this.recaptcha.getToken(action)); | ||
const inputEl = document.createElement("input"); | ||
inputEl.name = "recaptcha_token_response"; | ||
inputEl.type = "hidden"; | ||
inputEl.value = tokenCaptcha.token; | ||
this.insert(inputEl); | ||
this.el.submit(); | ||
} | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
registry | ||
.category("public.interactions") | ||
.add("google_recaptcha.recaptcha_form", RecaptchaForm); |