2022-10-27 17:07:18 +02:00
|
|
|
import { ApplicationController } from './application_controller';
|
2023-01-06 17:53:20 +01:00
|
|
|
import { show, hide } from '@utils';
|
2022-11-04 12:22:39 +01:00
|
|
|
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;
|
2023-01-06 17:53:20 +01:00
|
|
|
declare readonly hasSpinnerTarget: boolean;
|
2022-10-27 17:07:18 +02:00
|
|
|
|
|
|
|
submit() {
|
|
|
|
this.formTarget.requestSubmit();
|
|
|
|
}
|
2022-11-04 12:22:39 +01:00
|
|
|
|
|
|
|
debouncedSubmit() {
|
|
|
|
this.debounce(this.submit, AUTOSUBMIT_DEBOUNCE_DELAY);
|
|
|
|
}
|
|
|
|
|
2022-10-27 17:07:18 +02:00
|
|
|
connect() {
|
|
|
|
this.onGlobal('turbo:submit-start', () => {
|
2023-01-06 17:53:20 +01:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|