fix(autosave): prevent autosave if current champ is detached from the form
This commit is contained in:
parent
438c80d849
commit
6d0b3d93ed
1 changed files with 8 additions and 6 deletions
|
@ -1,4 +1,3 @@
|
||||||
import invariant from 'tiny-invariant';
|
|
||||||
import { httpRequest, ResponseError } from '@utils';
|
import { httpRequest, ResponseError } from '@utils';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
@ -131,9 +130,14 @@ export class AutosaveController extends ApplicationController {
|
||||||
// Returns a promise fulfilled when the request completes.
|
// Returns a promise fulfilled when the request completes.
|
||||||
private sendAutosaveRequest(): Promise<void> {
|
private sendAutosaveRequest(): Promise<void> {
|
||||||
this.#abortController = new AbortController();
|
this.#abortController = new AbortController();
|
||||||
|
const { form, inputs } = this;
|
||||||
|
|
||||||
|
if (!form || inputs.length == 0) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
for (const input of this.inputs) {
|
for (const input of inputs) {
|
||||||
if (input.type == 'checkbox') {
|
if (input.type == 'checkbox') {
|
||||||
formData.append(input.name, input.checked ? input.value : '');
|
formData.append(input.name, input.checked ? input.value : '');
|
||||||
} else if (input.type == 'radio') {
|
} 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',
|
method: 'patch',
|
||||||
body: formData,
|
body: formData,
|
||||||
signal: this.#abortController.signal,
|
signal: this.#abortController.signal,
|
||||||
|
@ -154,9 +158,7 @@ export class AutosaveController extends ApplicationController {
|
||||||
}
|
}
|
||||||
|
|
||||||
private get form() {
|
private get form() {
|
||||||
const form = this.element.closest('form');
|
return this.element.closest('form');
|
||||||
invariant(form, 'Could not find the form element.');
|
|
||||||
return form;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private get inputs() {
|
private get inputs() {
|
||||||
|
|
Loading…
Reference in a new issue