Files
rickyandClaude Opus 5.5 89ed0a76f9 Move all user-facing text into config/locales/en.yml for localization
- Views use lazy t(".key") lookups; sentences with values or links stay
  whole (interpolation, _html keys); counts use one/other plurals
- Stored-value labels (statuses, types, priorities...) are LabelSets read
  from labels: in the current locale; field names and validation messages
  under activerecord:; flash and activity text under flash:
- Services, models, helpers, mailer subjects, the logbook PDF, procedures
  templates, records package README and transcription warnings localized;
  the PDF reuses LogbookEntry#certification_statement instead of a copy
- Scripts read js: strings from a JSON tag via app/javascript/i18n.js
- Dates through I18n.l (app, weekday, month_year, long formats); html lang
  follows the locale
- APP_LOCALE picks the language (falls back to English); missing
  translations raise in development and tests
- README: Translating section and APP_LOCALE

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:08:20 -05:00

68 lines
2.4 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
import { t } from "i18n"
import { nextDue, describeDue } from "next_due"
// Live next-due preview (and prefill of an AD's next-due tach reading) from
// the selected AD/inspection type's recurrence rule.
//
// data-controller="next-due" data-next-due-rules-value='{"12": {"hours": 100, "months": 12, "days": 0}}'
// targets: rule (select), date, hours, dueHours (optional), permanent (optional), preview
export default class extends Controller {
static targets = ["rule", "date", "hours", "dueHours", "permanent", "preview"]
static values = { rules: Object }
connect() {
// Don't overwrite a reading that's already filled in (e.g. editing a record).
this.manual = this.hasDueHoursTarget && parseFloat(this.dueHoursTarget.value) > 0
this.recalc()
}
// The user typed their own next-due reading: stop overwriting it.
markManual() {
this.manual = this.dueHoursTarget.value.trim() !== "" && parseFloat(this.dueHoursTarget.value) > 0
this.renderPreview()
}
// Inputs that the rule depends on changed: recompute, unless overridden.
changed() {
this.manual = false
this.recalc()
}
recalc() {
const due = this.computeDue()
if (this.hasDueHoursTarget && !this.manual && !this.isPermanent) {
this.dueHoursTarget.value = due.hours != null ? due.hours.toFixed(1) : "0"
}
this.renderPreview(due)
}
renderPreview(due = this.computeDue()) {
if (!this.hasPreviewTarget) return
const rule = this.currentRule
let text
if (this.isPermanent) text = t("next_due.permanent")
else if (!this.ruleTarget.value) text = ""
else if (!rule) text = t("next_due.one_time")
else {
if (this.hasDueHoursTarget && this.manual) due = { ...due, hours: parseFloat(this.dueHoursTarget.value) }
text = describeDue(due) ? t("next_due.next_due", { due: describeDue(due) }) : t("next_due.enter")
}
this.previewTarget.textContent = text
this.previewTarget.hidden = text === ""
}
computeDue() {
return nextDue(this.currentRule, this.dateTarget.value, this.hoursTarget.value)
}
get currentRule() {
return this.rulesValue[this.ruleTarget.value] || null
}
get isPermanent() {
// A checkbox ticked, or the method select on terminating action / N/A.
return this.permanentTargets.some((el) => el.tagName === "SELECT" ? [ "terminating", "not_applicable" ].includes(el.value) : el.checked)
}
}