From 6d0b3d93ed8ba8d095832475f80d319d89ebe682 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Sat, 14 May 2022 10:39:01 +0200 Subject: [PATCH] fix(autosave): prevent autosave if current champ is detached from the form --- app/javascript/controllers/autosave_controller.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/javascript/controllers/autosave_controller.ts b/app/javascript/controllers/autosave_controller.ts index 547321fca..798be80d2 100644 --- a/app/javascript/controllers/autosave_controller.ts +++ b/app/javascript/controllers/autosave_controller.ts @@ -1,4 +1,3 @@ -import invariant from 'tiny-invariant'; import { httpRequest, ResponseError } from '@utils'; import { z } from 'zod'; @@ -131,9 +130,14 @@ export class AutosaveController extends ApplicationController { // Returns a promise fulfilled when the request completes. private sendAutosaveRequest(): Promise { this.#abortController = new AbortController(); + const { form, inputs } = this; + + if (!form || inputs.length == 0) { + return Promise.resolve(); + } const formData = new FormData(); - for (const input of this.inputs) { + for (const input of inputs) { if (input.type == 'checkbox') { formData.append(input.name, input.checked ? input.value : ''); } else if (input.type == 'radio') { @@ -145,7 +149,7 @@ export class AutosaveController extends ApplicationController { } } - return httpRequest(this.form.action, { + return httpRequest(form.action, { method: 'patch', body: formData, signal: this.#abortController.signal, @@ -154,9 +158,7 @@ export class AutosaveController extends ApplicationController { } private get form() { - const form = this.element.closest('form'); - invariant(form, 'Could not find the form element.'); - return form; + return this.element.closest('form'); } private get inputs() {