mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
With a signal so weak requests hang, the phone still says it's online, so nothing fell back: Log Flight waited minutes with no feedback, and pages never reached the offline page. - Log Flight and Report Squawk say "Sending…" and, after 10 seconds without an answer (60 with photos), keep the entry on the device as with no signal. When the top bar is already red they keep it right away. If it got through after all, the copy sent later is dropped (same client_key). - What's kept on the device isn't sent while the top bar is red, gives up after 15 seconds, and goes again as soon as the server answers. - Every page of the app falls back to its kept copy or the offline page: after 10 seconds, then 3 while requests keep failing, and at once right after a failure. Downloads made while you wait (.sam.zip, records package, PDFs) have no time limit. Checked on an Android 14 emulator with a connection that accepts requests and never answers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
4.5 KiB
JavaScript
115 lines
4.5 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
import { t } from "i18n"
|
|
import { queue, formFields, newKey, currentUser } from "controllers/offline_queue"
|
|
import { serverReachable } from "controllers/connection_controller"
|
|
|
|
// How long to wait for the server before keeping it here instead. Photos
|
|
// on a squawk take longer to send.
|
|
const WAIT = 10 * 1000
|
|
const WAIT_WITH_FILES = 60 * 1000
|
|
|
|
// Log Flight and Report Squawk: with no connection, the form is kept on this
|
|
// device and sent later (see offline_sync_controller). For flights, the next
|
|
// leg starts where the last saved one ended. A signal so weak the server
|
|
// doesn't answer counts as none: it's kept here too, and if it got through
|
|
// after all, the server drops the copy sent later (same client_key).
|
|
export default class extends Controller {
|
|
static targets = [ "key", "notice" ]
|
|
static values = { kind: { type: String, default: "flight" }, tail: String }
|
|
|
|
async connect() {
|
|
if (!this.keyTarget.value) this.keyTarget.value = newKey()
|
|
try {
|
|
const mine = (await queue.mine()).filter((i) => i.url === this.element.action)
|
|
if (mine.length) {
|
|
if (this.flight) this.prefillFrom(mine[mine.length - 1])
|
|
this.say(t(`offline.${this.kindValue}.queued_here`, { count: mine.length }))
|
|
}
|
|
} catch (_) { /* no IndexedDB: nothing queued */ }
|
|
this.rememberForm()
|
|
}
|
|
|
|
get flight() { return this.kindValue === "flight" }
|
|
|
|
// No connection (or the top bar is red): keep it here instead of submitting.
|
|
async submit(event) {
|
|
if (navigator.onLine && serverReachable()) return
|
|
event.preventDefault()
|
|
event.stopImmediatePropagation()
|
|
await this.save(formFields(this.element))
|
|
}
|
|
|
|
// Give up waiting after a while; the request then fails (networkFailed).
|
|
limit(event) {
|
|
const options = event.detail.fetchOptions
|
|
const files = [ ...this.element.querySelectorAll("input[type=file]") ].some((input) => input.files?.length)
|
|
options.signal = AbortSignal.any([ options.signal, AbortSignal.timeout(files ? WAIT_WITH_FILES : WAIT) ].filter(Boolean))
|
|
}
|
|
|
|
// Kept in case the request fails on the way (no signal but the browser thinks it's online).
|
|
remember() { this.pending = formFields(this.element) }
|
|
|
|
async networkFailed() {
|
|
if (this.pending) await this.save(this.pending)
|
|
}
|
|
|
|
// A proxy answered because the app is unreachable (502/503/504): keep it
|
|
// instead of showing the proxy's error page.
|
|
async gatewayFailed(event) {
|
|
if (![ 502, 503, 504 ].includes(event.detail.fetchResponse.statusCode) || !this.pending) return
|
|
event.preventDefault()
|
|
await this.save(this.pending)
|
|
}
|
|
|
|
async save(fields) {
|
|
const item = { id: this.keyTarget.value, kind: this.kindValue, url: this.element.action, tail: this.tailValue,
|
|
user: currentUser(), savedAt: new Date().toISOString(), fields }
|
|
try {
|
|
await queue.add(item)
|
|
} catch (_) {
|
|
this.say(t(`offline.${this.kindValue}.no_storage`))
|
|
return
|
|
}
|
|
if (this.flight) this.prefillFrom(item)
|
|
this.clearFields()
|
|
this.keyTarget.value = newKey()
|
|
this.pending = null
|
|
window.scrollTo({ top: 0 })
|
|
this.say(t(`offline.${this.kindValue}.saved_on_device`))
|
|
document.dispatchEvent(new CustomEvent("offline-queue:changed"))
|
|
}
|
|
|
|
prefillFrom(item) {
|
|
const value = (name) => (item.fields.find(([ n ]) => n === `flight_log[${name}]`) || [])[1]
|
|
const set = (name, v) => { const el = this.element.querySelector(`[name="flight_log[${name}]"]`); if (el && v) el.value = v }
|
|
set("tach_out", value("tach_in"))
|
|
set("hobbs_out", value("hobbs_in"))
|
|
set("departure_location", value("destination_location"))
|
|
}
|
|
|
|
clearFields() {
|
|
const names = this.flight
|
|
? [ "tach_in", "tach_time", "hobbs_in", "hobbs_time", "destination_location", "route", "notes",
|
|
"oil_added", "oil_level_after", "fuel_added", "fuel_level_after", "track_log", "engine_data" ].map((n) => `flight_log[${n}]`)
|
|
: [ "squawk[issue_reported]", "squawk[attachments][]" ]
|
|
for (const name of names) {
|
|
const el = this.element.querySelector(`[name="${name}"]`)
|
|
if (el) el.value = ""
|
|
}
|
|
}
|
|
|
|
// Lets the offline page and the home-screen shortcuts offer this form.
|
|
rememberForm() {
|
|
try {
|
|
const forms = JSON.parse(localStorage.getItem("sam-offline-forms") || "{}")
|
|
forms[location.pathname] = { tail: this.tailValue, kind: this.kindValue }
|
|
localStorage.setItem("sam-offline-forms", JSON.stringify(forms))
|
|
} catch (_) { /* storage off */ }
|
|
}
|
|
|
|
say(text) {
|
|
this.noticeTarget.textContent = text
|
|
this.noticeTarget.classList.remove("hidden")
|
|
}
|
|
}
|