2022-07-12 11:41:38 +02:00
|
|
|
import {
|
|
|
|
httpRequest,
|
|
|
|
ResponseError,
|
|
|
|
isSelectElement,
|
|
|
|
isCheckboxOrRadioInputElement,
|
|
|
|
isTextInputElement,
|
|
|
|
getConfig
|
|
|
|
} from '@utils';
|
2022-05-06 19:42:40 +02:00
|
|
|
|
|
|
|
import { ApplicationController } from './application_controller';
|
|
|
|
import { AutoUpload } from '../shared/activestorage/auto-upload';
|
2022-05-11 12:32:22 +02:00
|
|
|
import {
|
|
|
|
FileUploadError,
|
|
|
|
FAILURE_CLIENT,
|
|
|
|
ERROR_CODE_READ
|
|
|
|
} from '../shared/activestorage/file-upload-error';
|
2022-05-06 19:42:40 +02:00
|
|
|
|
2022-07-07 11:39:03 +02:00
|
|
|
const {
|
|
|
|
autosave: { debounce_delay }
|
|
|
|
} = getConfig();
|
2022-05-06 19:42:40 +02:00
|
|
|
|
|
|
|
const AUTOSAVE_DEBOUNCE_DELAY = debounce_delay;
|
|
|
|
const AUTOSAVE_TIMEOUT_DELAY = 60000;
|
|
|
|
|
|
|
|
// This is a controller we attach to each "champ" in the main form. It performs
|
|
|
|
// the save and dispatches a few events that allow `AutosaveStatusController` to
|
|
|
|
// coordinate notifications and retries:
|
|
|
|
// * `autosave:enqueue` - dispatched when a new save attempt starts
|
|
|
|
// * `autosave:end` - dispatched after sucessful save
|
|
|
|
// * `autosave:error` - dispatched when an error occures
|
|
|
|
//
|
|
|
|
// The controller also listens to the following events:
|
|
|
|
// * `autosave:retry` - dispatched by `AutosaveStatusController` when the user
|
|
|
|
// clicks the retry button in the form status bar
|
|
|
|
//
|
|
|
|
export class AutosaveController extends ApplicationController {
|
|
|
|
#abortController?: AbortController;
|
|
|
|
#latestPromise = Promise.resolve();
|
|
|
|
#needsRetry = false;
|
2022-10-24 20:03:44 +02:00
|
|
|
#pendingPromiseCount = 0;
|
2022-05-06 19:42:40 +02:00
|
|
|
|
|
|
|
connect() {
|
|
|
|
this.#latestPromise = Promise.resolve();
|
|
|
|
this.onGlobal('autosave:retry', () => this.didRequestRetry());
|
2022-07-12 11:41:38 +02:00
|
|
|
this.on('change', (event) => this.onChange(event));
|
2022-09-08 11:26:18 +02:00
|
|
|
|
|
|
|
if (this.saveOnInput) {
|
|
|
|
this.on('input', (event) => this.onInput(event));
|
|
|
|
}
|
2022-05-06 19:42:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
this.#abortController?.abort();
|
|
|
|
this.#latestPromise = Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
onClickRetryButton(event: Event) {
|
|
|
|
const target = event.target as HTMLButtonElement;
|
|
|
|
const inputTargetSelector = target.dataset.inputTarget;
|
|
|
|
if (inputTargetSelector) {
|
|
|
|
const target =
|
|
|
|
this.element.querySelector<HTMLInputElement>(inputTargetSelector);
|
|
|
|
if (
|
|
|
|
target &&
|
|
|
|
target.type == 'file' &&
|
|
|
|
target.dataset.autoAttachUrl &&
|
|
|
|
target.files?.length
|
|
|
|
) {
|
|
|
|
this.enqueueAutouploadRequest(target, target.files[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 11:41:38 +02:00
|
|
|
private onChange(event: Event) {
|
2022-05-06 19:42:40 +02:00
|
|
|
const target = event.target as HTMLInputElement;
|
2022-07-12 11:41:38 +02:00
|
|
|
if (!target.disabled) {
|
|
|
|
if (target.type == 'file') {
|
|
|
|
if (target.dataset.autoAttachUrl && target.files?.length) {
|
|
|
|
this.enqueueAutouploadRequest(target, target.files[0]);
|
|
|
|
}
|
|
|
|
} else if (target.type == 'hidden') {
|
|
|
|
// In React comboboxes we dispatch a "change" event on hidden inputs to trigger autosave.
|
|
|
|
// We want to debounce them.
|
|
|
|
this.debounce(this.enqueueAutosaveRequest, AUTOSAVE_DEBOUNCE_DELAY);
|
|
|
|
} else if (
|
|
|
|
isSelectElement(target) ||
|
2022-09-08 11:26:18 +02:00
|
|
|
isCheckboxOrRadioInputElement(target) ||
|
|
|
|
(!this.saveOnInput && isTextInputElement(target))
|
2022-07-12 11:41:38 +02:00
|
|
|
) {
|
|
|
|
this.enqueueAutosaveRequest();
|
|
|
|
}
|
2022-05-06 19:42:40 +02:00
|
|
|
}
|
2022-07-12 11:41:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private onInput(event: Event) {
|
|
|
|
const target = event.target as HTMLInputElement;
|
2022-05-06 19:42:40 +02:00
|
|
|
if (
|
2022-07-12 11:41:38 +02:00
|
|
|
!target.disabled &&
|
|
|
|
// Ignore input from React comboboxes. We trigger "change" events on them when selection is changed.
|
|
|
|
target.getAttribute('role') != 'combobox' &&
|
|
|
|
isTextInputElement(target)
|
2022-05-06 19:42:40 +02:00
|
|
|
) {
|
|
|
|
this.debounce(this.enqueueAutosaveRequest, AUTOSAVE_DEBOUNCE_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:26:18 +02:00
|
|
|
private get saveOnInput() {
|
|
|
|
return !!this.form?.dataset.saveOnInput;
|
|
|
|
}
|
|
|
|
|
2022-05-06 19:42:40 +02:00
|
|
|
private didRequestRetry() {
|
|
|
|
if (this.#needsRetry) {
|
|
|
|
this.enqueueAutosaveRequest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private didEnqueue() {
|
|
|
|
this.#needsRetry = false;
|
|
|
|
this.globalDispatch('autosave:enqueue');
|
|
|
|
}
|
|
|
|
|
|
|
|
private didSucceed() {
|
2022-10-24 20:03:44 +02:00
|
|
|
this.#pendingPromiseCount -= 1;
|
|
|
|
if (this.#pendingPromiseCount == 0) {
|
|
|
|
this.globalDispatch('autosave:end');
|
|
|
|
}
|
2022-05-06 19:42:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private didFail(error: ResponseError) {
|
|
|
|
this.#needsRetry = true;
|
2022-10-24 20:03:44 +02:00
|
|
|
this.#pendingPromiseCount -= 1;
|
2022-05-06 19:42:40 +02:00
|
|
|
this.globalDispatch('autosave:error', { error });
|
|
|
|
}
|
|
|
|
|
|
|
|
private enqueueAutouploadRequest(target: HTMLInputElement, file: File) {
|
|
|
|
const autoupload = new AutoUpload(target, file);
|
2022-12-14 23:28:18 +01:00
|
|
|
autoupload.start().catch((e) => {
|
2022-05-11 12:32:22 +02:00
|
|
|
const error = e as FileUploadError;
|
|
|
|
// Report unexpected client errors to Sentry.
|
|
|
|
// (But ignore usual client errors, or errors we can monitor better on the server side.)
|
|
|
|
if (
|
|
|
|
error.failureReason == FAILURE_CLIENT &&
|
|
|
|
error.code != ERROR_CODE_READ
|
|
|
|
) {
|
|
|
|
throw error;
|
|
|
|
}
|
2022-12-14 23:28:18 +01:00
|
|
|
});
|
2022-05-06 19:42:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new autosave request to the queue.
|
|
|
|
// It will be started after the previous one finishes (to prevent older form data
|
|
|
|
// to overwrite newer data if the server does not respond in order.)
|
|
|
|
private enqueueAutosaveRequest() {
|
|
|
|
this.#latestPromise = this.#latestPromise.finally(() =>
|
|
|
|
this.sendAutosaveRequest()
|
|
|
|
.then(() => this.didSucceed())
|
|
|
|
.catch((error) => this.didFail(error))
|
|
|
|
);
|
|
|
|
this.didEnqueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a fetch request that saves the form.
|
|
|
|
// Returns a promise fulfilled when the request completes.
|
|
|
|
private sendAutosaveRequest(): Promise<void> {
|
|
|
|
this.#abortController = new AbortController();
|
2022-05-14 10:39:01 +02:00
|
|
|
const { form, inputs } = this;
|
|
|
|
|
|
|
|
if (!form || inputs.length == 0) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2022-05-06 19:42:40 +02:00
|
|
|
|
|
|
|
const formData = new FormData();
|
2022-05-14 10:39:01 +02:00
|
|
|
for (const input of inputs) {
|
2022-05-06 19:42:40 +02:00
|
|
|
if (input.type == 'checkbox') {
|
|
|
|
formData.append(input.name, input.checked ? input.value : '');
|
|
|
|
} else if (input.type == 'radio') {
|
|
|
|
if (input.checked) {
|
|
|
|
formData.append(input.name, input.value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
formData.append(input.name, input.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 20:03:44 +02:00
|
|
|
this.#pendingPromiseCount++;
|
|
|
|
|
2022-05-14 10:39:01 +02:00
|
|
|
return httpRequest(form.action, {
|
2022-12-14 23:28:18 +01:00
|
|
|
method: 'post',
|
2022-05-06 19:42:40 +02:00
|
|
|
body: formData,
|
2022-12-14 23:28:18 +01:00
|
|
|
headers: { 'x-http-method-override': 'PATCH' },
|
2022-05-06 19:42:40 +02:00
|
|
|
signal: this.#abortController.signal,
|
|
|
|
timeout: AUTOSAVE_TIMEOUT_DELAY
|
|
|
|
}).turbo();
|
|
|
|
}
|
|
|
|
|
|
|
|
private get form() {
|
2022-05-14 10:39:01 +02:00
|
|
|
return this.element.closest('form');
|
2022-05-06 19:42:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private get inputs() {
|
|
|
|
const element = this.element as HTMLElement;
|
2022-09-29 14:50:02 +02:00
|
|
|
|
2022-11-29 11:30:06 +01:00
|
|
|
return [
|
2022-05-06 19:42:40 +02:00
|
|
|
...element.querySelectorAll<HTMLInputElement>(
|
|
|
|
'input:not([type=file]), textarea, select'
|
|
|
|
)
|
2022-09-29 14:50:02 +02:00
|
|
|
].filter((element) => !element.disabled);
|
2022-05-06 19:42:40 +02:00
|
|
|
}
|
|
|
|
}
|