From 78d4abfb3d0c75c21b593b34bf54597fe0676e0b Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 29 Sep 2022 18:18:56 +0200 Subject: [PATCH] chore(js): remove CheckConditionsController --- .../check_conditions_controller.ts | 94 ------------------- 1 file changed, 94 deletions(-) delete mode 100644 app/javascript/controllers/check_conditions_controller.ts diff --git a/app/javascript/controllers/check_conditions_controller.ts b/app/javascript/controllers/check_conditions_controller.ts deleted file mode 100644 index f99e99947..000000000 --- a/app/javascript/controllers/check_conditions_controller.ts +++ /dev/null @@ -1,94 +0,0 @@ -import { - httpRequest, - isSelectElement, - isCheckboxOrRadioInputElement, - isTextInputElement, - getConfig -} from '@utils'; - -import { ApplicationController } from './application_controller'; - -const { - autosave: { debounce_delay } -} = getConfig(); - -const AUTOSAVE_DEBOUNCE_DELAY = debounce_delay; -const AUTOSAVE_TIMEOUT_DELAY = 60000; - -export class CheckConditionsController extends ApplicationController { - #abortController?: AbortController; - #latestPromise = Promise.resolve(); - - connect() { - this.#latestPromise = Promise.resolve(); - this.on('change', (event) => this.onChange(event)); - this.on('input', (event) => this.onInput(event)); - } - - disconnect() { - this.#abortController?.abort(); - this.#latestPromise = Promise.resolve(); - } - - private onChange(event: Event) { - const target = event.target as HTMLInputElement; - if (!target.disabled) { - if (target.type == 'hidden') { - this.debounce(this.enqueueCheckRequest, AUTOSAVE_DEBOUNCE_DELAY); - } else if ( - isSelectElement(target) || - isCheckboxOrRadioInputElement(target) - ) { - this.enqueueCheckRequest(); - } - } - } - - private onInput(event: Event) { - const target = event.target as HTMLInputElement; - if (!target.disabled) { - if ( - target.getAttribute('role') != 'combobox' && - isTextInputElement(target) - ) { - this.debounce(this.enqueueCheckRequest, AUTOSAVE_DEBOUNCE_DELAY); - } - } - } - - private enqueueCheckRequest() { - this.#latestPromise = this.#latestPromise.finally(() => - this.sendCheckRequest().catch(() => null) - ); - } - - private sendCheckRequest(): Promise { - this.#abortController = new AbortController(); - const form = this.form; - - if (!form) { - return Promise.resolve(); - } - - const fileInputs = form.querySelectorAll('input[type="file"]'); - for (const input of fileInputs) { - input.setAttribute('disabled', 'disabled'); - } - const formData = new FormData(form); - for (const input of fileInputs) { - input.removeAttribute('disabled'); - } - formData.set('check_conditions', 'true'); - - return httpRequest(form.action, { - method: 'patch', - body: formData, - signal: this.#abortController.signal, - timeout: AUTOSAVE_TIMEOUT_DELAY - }).turbo(); - } - - private get form() { - return this.element.closest('form'); - } -}