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

25 lines
594 B
TypeScript
Raw Normal View History

2022-10-27 17:07:18 +02:00
import { ApplicationController } from './application_controller';
import { toggle } 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;
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', () => {
toggle(this.spinnerTarget);
});
}
}