diff --git a/app/controllers/aircraft_controller.rb b/app/controllers/aircraft_controller.rb index eea050f..6092d0d 100644 --- a/app/controllers/aircraft_controller.rb +++ b/app/controllers/aircraft_controller.rb @@ -52,10 +52,9 @@ class AircraftController < ApplicationController # Pilots and above: set absolute tach/hobbs readings. def update_hours - delta = HoursService.new(@aircraft).update_hours!( - tach_time: params.require(:tach_time), hobbs_time: params[:hobbs_time] - ) - redirect_to @aircraft, notice: t("flash.aircraft.hours_updated", delta: "#{'+' if delta >= 0}#{delta}") + before = { tach: @aircraft.tach_time.to_d, hobbs: @aircraft.hobbs_time.to_d } + HoursService.new(@aircraft).update_hours!(tach_time: params.require(:tach_time), hobbs_time: params[:hobbs_time]) + redirect_to @aircraft, notice: hours_notice(before) rescue AircraftGroundedError => e redirect_to @aircraft, alert: e.message rescue ArgumentError @@ -64,6 +63,19 @@ class AircraftController < ApplicationController private + # "Tach 4,211.6 → 4,213.0 (+1.4). In-service components moved +1.4 hrs; no flight was logged." + def hours_notice(before) + after = { tach: @aircraft.tach_time.to_d, hobbs: @aircraft.hobbs_time.to_d } + signed = ->(d) { "#{d.negative? ? '−' : '+'}#{helpers.hours(d.abs)}" } + changes = %i[tach hobbs].filter_map do |meter| + d = after[meter] - before[meter] + t("flash.aircraft.meter_changed", meter: Aircraft::TIME_BASES[meter.to_s], from: helpers.hours(before[meter]), to: helpers.hours(after[meter]), delta: signed.(d)) unless d.zero? + end + moved = @aircraft.hobbs? ? after[:hobbs] - before[:hobbs] : after[:tach] - before[:tach] + return t("flash.aircraft.hours_unchanged") if changes.empty? + t(moved.negative? ? "flash.aircraft.hours_corrected" : "flash.aircraft.hours_updated", changes: changes.join(" · "), delta: signed.(moved)) + end + def aircraft_param = params[:id] def aircraft_params diff --git a/app/javascript/controllers/hours_confirm_controller.js b/app/javascript/controllers/hours_confirm_controller.js new file mode 100644 index 0000000..e04587f --- /dev/null +++ b/app/javascript/controllers/hours_confirm_controller.js @@ -0,0 +1,30 @@ +import { Controller } from "@hotwired/stimulus" +import { t, locale } from "i18n" + +// Update Hours (Overview): confirms the new meter readings first, since they +// change every in-service component's hours and log no flight. +export default class extends Controller { + static targets = [ "tach", "hobbs" ] + static values = { tach: Number, hobbs: Number } + + confirm(event) { + const lines = [ this.line("tach", this.tachValue, this.tachTarget.value), + this.hasHobbsTarget ? this.line("hobbs", this.hobbsValue, this.hobbsTarget.value) : null ].filter(Boolean) + if (!lines.length) { + event.preventDefault() + return alert(t("hours.no_change")) + } + const lower = parseFloat(this.tachTarget.value) < this.tachValue + const message = [ ...lines, "", t("hours.effect"), lower ? t("hours.lower") : null ].filter((l) => l !== null).join("\n") + if (!confirm(message)) event.preventDefault() + } + + // "Tach 4,211.6 → 4,213.0 (+1.4)", or null when unchanged or blank. + line(meter, now, entered) { + const value = parseFloat(entered) + if (Number.isNaN(value) || Math.abs(value - now) < 0.05) return null + const fmt = (n) => n.toLocaleString(locale(), { minimumFractionDigits: 1, maximumFractionDigits: 1 }) + const delta = value - now + return t(`hours.${meter}`, { from: fmt(now), to: fmt(value), delta: `${delta > 0 ? "+" : "−"}${fmt(Math.abs(delta))}` }) + } +} diff --git a/app/views/aircraft/show.html.erb b/app/views/aircraft/show.html.erb index c1672d8..68f251c 100644 --- a/app/views/aircraft/show.html.erb +++ b/app/views/aircraft/show.html.erb @@ -59,12 +59,14 @@
<%= t(".enter_the_current_meter_readings_html", link: (link_to t(".log_flight"), new_aircraft_flight_log_path(@aircraft), class: "link")) %>
<%= f.submit t(".update_hours"), class: "btn-primary" %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 9cd1962..5ff1929 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -800,9 +800,12 @@ en: added: "%{tail_number} added." aircraft_updated: Aircraft updated. deleted: "%{tail_number} deleted." - hours_updated: Hours updated (%{delta}). + hours_updated: "%{changes}. In-service components moved %{delta} hrs; no flight was logged." enter_a_valid_tach: Enter a valid tach reading. aircraft_details_updated: Aircraft details updated + meter_changed: "%{meter} %{from} → %{to} (%{delta})" + hours_corrected: 'Corrected: %{changes}. In-service components moved %{delta} hrs; no flight was logged.' + hours_unchanged: The readings didn't change. sso: try_again_later: Try again later. there_s_no_account: There's no account for %{email} here yet. Ask an administrator to add you, or sign in with your password and link %{provider} from your profile. @@ -3845,6 +3848,12 @@ en: ios_install_first: On iPhone and iPad, install the app first (above); notifications can then be turned on in it. failed: Couldn't turn notifications on. Try again. test_sent: Test notification sent; it should arrive in a few seconds. + hours: + tach: Tach %{from} → %{to} (%{delta}) + hobbs: Hobbs %{from} → %{to} (%{delta}) + effect: Every in-service component's hours change by the same amount, and no flight is logged. To record a flight, use Log Flight instead. + lower: 'This lowers the reading: use it only to correct a mistake.' + no_change: 'These are the current readings: nothing to update.' counts: problem: one: "%{count} problem" diff --git a/test/integration/pwa_features_test.rb b/test/integration/pwa_features_test.rb index c2c8b08..07516dd 100644 --- a/test/integration/pwa_features_test.rb +++ b/test/integration/pwa_features_test.rb @@ -31,6 +31,20 @@ class PwaFeaturesTest < ActionDispatch::IntegrationTest assert_select "form[action='#{session_path}'][data-controller=sign-out]", minimum: 1 end + test "updating the meters without a flight asks for confirmation, showing the change" do + sign_in @owner + get aircraft_path(@a) + assert_select "form[action='#{hours_aircraft_path(@a)}'][data-controller=hours-confirm][data-action='submit->hours-confirm#confirm'][data-hours-confirm-tach-value='1000.0']" do + assert_select "input[name=tach_time][data-hours-confirm-target=tach]" + assert_select "input[name=hobbs_time][data-hours-confirm-target=hobbs]" + end + + patch hours_aircraft_path(@a), params: { tach_time: "1001.4", hobbs_time: "1.2" } + assert_equal "Tach 1,000.0 → 1,001.4 (+1.4) · Hobbs 0.0 → 1.2 (+1.2). In-service components moved +1.4 hrs; no flight was logged.", flash[:notice] + patch hours_aircraft_path(@a), params: { tach_time: "1001.0" } + assert_equal "Corrected: Tach 1,001.4 → 1,001.0 (−0.4). In-service components moved −0.4 hrs; no flight was logged.", flash[:notice] + end + test "every page lists what to save ahead of time for working offline: each of your aircraft's forms and pages" do other = create_aircraft(tail_number: "N99999") # not this pilot's sign_in @pilot