2021-11-22 17:51:50 +00:00
|
|
|
import { Controller } from "stimulus";
|
2021-04-13 22:26:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-04-13 22:26:09 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
connect() {
|
|
|
|
if (!this.hasSubmitTarget) return;
|
2021-04-13 22:26:09 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
this.submitTarget.disabled = true;
|
2021-04-13 22:26:09 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
this._value = this.submitTarget.value;
|
2021-04-13 22:26:09 +00:00
|
|
|
|
|
|
|
// 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;
|
2021-04-13 22:26:09 +00:00
|
|
|
|
|
|
|
if (this.delay == 0) {
|
2021-11-22 17:51:50 +00:00
|
|
|
clearInterval(this._interval);
|
|
|
|
this.submitTarget.disabled = false;
|
|
|
|
this.submitTarget.value = this._value;
|
2021-04-13 22:26:09 +00:00
|
|
|
} else {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.delay = delay - 1;
|
2021-04-13 22:26:09 +00:00
|
|
|
}
|
2021-11-22 17:51:50 +00:00
|
|
|
}, 1000);
|
2021-04-13 22:26:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
get delay() {
|
|
|
|
const delay = parseInt(this.element.dataset.delay);
|
2021-04-13 22:26:09 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
return isNaN(delay) ? 0 : delay;
|
2021-04-13 22:26:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
set delay(value) {
|
|
|
|
this.element.dataset.delay = value;
|
|
|
|
this.submitTarget.value = `${this._value} (${value})`;
|
2021-04-13 22:26:09 +00:00
|
|
|
}
|
|
|
|
}
|