demarches-normaliennes/app/javascript/controllers/autosubmit_controller.ts

33 lines
818 B
TypeScript
Raw Normal View History

2022-10-27 17:07:18 +02:00
import { ApplicationController } from './application_controller';
import { show, hide } from '@utils';
const AUTOSUBMIT_DEBOUNCE_DELAY = 5000;
2022-10-27 17:07:18 +02:00
export class AutosubmitController extends ApplicationController {
static targets = ['form', 'spinner'];
declare readonly formTarget: HTMLFormElement;
declare readonly spinnerTarget: HTMLElement;
declare readonly hasSpinnerTarget: boolean;
2022-10-27 17:07:18 +02:00
submit() {
this.formTarget.requestSubmit();
}
debouncedSubmit() {
this.debounce(this.submit, AUTOSUBMIT_DEBOUNCE_DELAY);
}
2022-10-27 17:07:18 +02:00
connect() {
this.onGlobal('turbo:submit-start', () => {
if (this.hasSpinnerTarget) {
show(this.spinnerTarget);
}
});
this.onGlobal('turbo:submit-end', () => {
if (this.hasSpinnerTarget) {
hide(this.spinnerTarget);
}
2022-10-27 17:07:18 +02:00
});
}
}