sutty-base-jekyll-theme/_packs/controllers/contact_controller.js

43 lines
999 B
JavaScript
Raw Normal View History

2021-11-22 17:51:50 +00:00
import { Controller } from "stimulus";
/*
* Sólo permite enviar el formulario de contacto después de unos
* segundos, para evitar el spam.
*/
export default class extends Controller {
2021-11-22 17:51:50 +00:00
static targets = ["submit"];
2021-11-22 17:51:50 +00:00
connect() {
if (!this.hasSubmitTarget) return;
2021-11-22 17:51:50 +00:00
this.submitTarget.disabled = true;
2021-11-22 17:51:50 +00:00
this._value = this.submitTarget.value;
// Esperar un minuto desde que se carga la página hasta que se
// permite enviar.
this._interval = setInterval(() => {
2021-11-22 17:51:50 +00:00
const delay = this.delay;
if (this.delay == 0) {
2021-11-22 17:51:50 +00:00
clearInterval(this._interval);
this.submitTarget.disabled = false;
this.submitTarget.value = this._value;
} else {
2021-11-22 17:51:50 +00:00
this.delay = delay - 1;
}
2021-11-22 17:51:50 +00:00
}, 1000);
}
2021-11-22 17:51:50 +00:00
get delay() {
const delay = parseInt(this.element.dataset.delay);
2021-11-22 17:51:50 +00:00
return isNaN(delay) ? 0 : delay;
}
2021-11-22 17:51:50 +00:00
set delay(value) {
this.element.dataset.delay = value;
this.submitTarget.value = `${this._value} (${value})`;
}
}