Sha256: e4df18a63cbb7583af27c97877fdf34c6563de2512f4596f3e80c100376c97ae
Contents?: true
Size: 1.34 KB
Versions: 31
Compression:
Stored size: 1.34 KB
Contents
import { Controller } from "@hotwired/stimulus" import { FetchRequest } from "https://esm.sh/@rails/request.js@0.0.11?standalone" const AUTOSAVE_INTERVAL = 3000 export default class extends Controller { static targets = [ "submitter" ] #timer disconnect() { this.#submit() } change() { !this.#dirty && this.#scheduleSave() !this.#dirty && this.#updateAppearance() } async #submit() { this.#dirty && await this.#save() } async #save() { this.#updateAppearance(true) this.#resetTimer() await this.#submitForm(this.element) this.#updateAppearance() } async #submitForm(form) { const request = new FetchRequest(form.method, form.action, { body: new FormData(form) }) return await request.perform() } #updateAppearance(saving = false) { if (saving) { this.element.setAttribute("aria-busy", true) this.submitterTarget.setAttribute("aria-disabled", true) this.submitterTarget.disabled = true } else { this.element.removeAttribute("aria-busy") this.submitterTarget.removeAttribute("aria-disabled") this.submitterTarget.disabled = false } } #scheduleSave() { this.#timer = setTimeout(() => this.#save(), AUTOSAVE_INTERVAL) } #resetTimer() { clearTimeout(this.#timer); this.#timer = null } get #dirty() { return !!this.#timer } }
Version data entries
31 entries across 31 versions & 1 rubygems