From acb3b1f0427b12778d394951e651eaa87683872f Mon Sep 17 00:00:00 2001 From: Joren de Graaf Date: Tue, 6 Oct 2026 19:50:52 +0200 Subject: [PATCH] manual submission redesign phase D --- .../includes/manual_direction_panel.html | 2 +- .../includes/manual_report_form.html | 4 +- .../manual_pending_submission.html | 15 +++- fireworks_detect/tests.py | 8 +- frontend/src/scripts/manual-report.js | 77 +++++++++++++++---- .../components/manual-report-structure.css | 17 ++++ templates/js_translations.html | 2 + 7 files changed, 103 insertions(+), 22 deletions(-) diff --git a/fireworks_detect/templates/fireworks_detect/includes/manual_direction_panel.html b/fireworks_detect/templates/fireworks_detect/includes/manual_direction_panel.html index 051bf1c..60d62f1 100644 --- a/fireworks_detect/templates/fireworks_detect/includes/manual_direction_panel.html +++ b/fireworks_detect/templates/fireworks_detect/includes/manual_direction_panel.html @@ -1,5 +1,5 @@ {% load i18n %} -
+
{% if measuring %}
{% trans "Capture direction" %} diff --git a/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html b/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html index 2ba1d0e..b339dd8 100644 --- a/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html +++ b/fireworks_detect/templates/fireworks_detect/includes/manual_report_form.html @@ -5,7 +5,7 @@

{% trans "Report fireworks now" %}

{% trans "Choose how many times you heard fireworks, then send it straight to Pending. The report is timestamped now." %}

-
+ {% csrf_token %} @@ -21,7 +21,7 @@
{% include "fireworks_detect/includes/manual_direction_panel.html" %} - +
diff --git a/fireworks_detect/templates/fireworks_detect/manual_pending_submission.html b/fireworks_detect/templates/fireworks_detect/manual_pending_submission.html index f45ea87..8c7a58e 100644 --- a/fireworks_detect/templates/fireworks_detect/manual_pending_submission.html +++ b/fireworks_detect/templates/fireworks_detect/manual_pending_submission.html @@ -1,8 +1,17 @@ {% load i18n %} -
+
{% if error %} - {% trans "Could not report fireworks" %}

{{ error }}

+ + {% trans "Could not report fireworks" %} +

{{ error }}

{% else %} - {% trans "Report sent to Pending" %}

{% blocktrans count count=pending.occurrences with time=pending.created_at|date:"H:i" %}{{ count }} occurrence reported at {{ time }}.{% plural %}{{ count }} occurrences reported at {{ time }}.{% endblocktrans %}

+ + {% trans "Report received!" %} +

{% trans "Thank you. Your report helps map fireworks nearby." %}

+

+ {{ pending.created_at|date:"H:i" }} · + {% blocktrans count count=pending.occurrences %}{{ count }} occurrence{% plural %}{{ count }} occurrences{% endblocktrans %} + {% if pending.direction_degrees is not None %} · {{ pending.direction_cardinal }} {{ pending.direction_degrees|floatformat:0 }}°{% endif %} +

{% endif %}
diff --git a/fireworks_detect/tests.py b/fireworks_detect/tests.py index 76bfec8..39eaf9d 100644 --- a/fireworks_detect/tests.py +++ b/fireworks_detect/tests.py @@ -62,7 +62,9 @@ class ManualPendingEventTests(TestCase): }) self.assertEqual(response.status_code, 200) - self.assertContains(response, "Report sent to Pending") + self.assertContains(response, "Report received!") + self.assertContains(response, "4 occurrences") + self.assertContains(response, 'data-result="success"') pending = PendingEvent.objects.get() self.assertEqual(pending.original_filename, "Manual fireworks report") self.assertEqual(pending.source_type, PendingEvent.SourceType.MANUAL) @@ -83,6 +85,7 @@ class ManualPendingEventTests(TestCase): self.assertEqual(response.status_code, 400) self.assertContains(response, "Choose between 1 and 1,000 occurrences.", status_code=400) + self.assertContains(response, 'data-result="error"', status_code=400) self.assertFalse(PendingEvent.objects.exists()) def test_manual_report_accepts_optional_wav_on_the_same_pending_event(self): @@ -290,6 +293,9 @@ class ManualPendingEventTests(TestCase): "direction_accuracy_degrees": "8", }) + self.assertContains(response, "2 occurrences") + self.assertContains(response, "SW 237°") + self.assertEqual(response.status_code, 200) pending = PendingEvent.objects.get() self.assertEqual(pending.direction_degrees, 237) diff --git a/frontend/src/scripts/manual-report.js b/frontend/src/scripts/manual-report.js index 4a25be0..fdca8fe 100644 --- a/frontend/src/scripts/manual-report.js +++ b/frontend/src/scripts/manual-report.js @@ -3,16 +3,17 @@ const manualForm = document.querySelector("#manual-report-form"); const manualSubmit = document.querySelector("#manual-submit"); + const submitLabel = document.querySelector("#manual-submit-label"); const occurrences = document.querySelector("#manual-occurrences"); const decrease = document.querySelector("#manual-decrease"); const increase = document.querySelector("#manual-increase"); - const manualStatus = document.querySelector("#manual-submission"); const latitude = document.querySelector("#manual-latitude"); const longitude = document.querySelector("#manual-longitude"); const directionDegrees = document.querySelector("#manual-direction-degrees"); const directionAccuracy = document.querySelector("#manual-direction-accuracy"); const captureDirectionButton = () => document.querySelector("#manual-capture-direction"); const directionStatus = () => document.querySelector("#manual-direction-status"); + const directionPanel = () => document.querySelector("#manual-direction-panel"); const compass = document.querySelector("#manual-compass"); const compassRose = document.querySelector("#manual-compass-rose"); const compassHeading = document.querySelector("#manual-compass-heading"); @@ -24,6 +25,29 @@ const isUsableNumber = (value) => Number.isFinite(value); const cardinalDirection = (heading) => ["N", "NE", "E", "SE", "S", "SW", "W", "NW"][Math.floor(((heading + 22.5) % 360) / 45)]; const displayDegrees = (value) => Number(value.toFixed(1)).toString(); + const idleSubmitLabel = submitLabel?.textContent || "I heard fireworks"; + + const setSubmissionState = (next, message = "") => { + manualForm.dataset.state = next; + manualSubmit.disabled = next === "submitting"; + if (submitLabel) submitLabel.textContent = next === "submitting" ? (t.sendingReport || "Sending…") : idleSubmitLabel; + if (message) { + const status = document.querySelector("#manual-submission"); + status.className = "manual-report__submission"; + status.textContent = message; + } + }; + + const showResponse = (html, successful) => { + const parsed = new DOMParser().parseFromString(html, "text/html").querySelector("#manual-submission"); + if (!parsed) { + setSubmissionState("error", t.reportFailed || "Could not send the report. Please try again."); + return false; + } + document.querySelector("#manual-submission").replaceWith(parsed); + setSubmissionState(successful ? "submitted" : "error"); + return successful; + }; const setCount = (value) => { const count = Number.parseInt(value, 10); @@ -75,6 +99,7 @@ const listenForCompassReading = () => { if (!directionStatus()) return; stopCompassReading(); + if (directionPanel()) directionPanel().dataset.state = "measuring"; directionStatus().textContent = (t.pointPhone || "Point the top of your phone toward the sound — direction will update live."); orientationListener = (event) => { const isWebKitCompass = "webkitCompassHeading" in event; @@ -101,6 +126,7 @@ const status = directionStatus(); if (button) button.textContent = (t.captureDirection || "Start measuring direction"); if (status) status.textContent = (t.compassUnavailable || "Compass unavailable on this device."); + if (directionPanel()) directionPanel().dataset.state = "unavailable"; }, 15000); }; @@ -108,14 +134,17 @@ if (orientationListener) { stopCompassReading(); captureDirectionButton().textContent = (t.captureAgain || "Measure again"); + if (directionPanel()) directionPanel().dataset.state = directionDegrees.value ? "captured" : "ready"; return; } if (!window.isSecureContext) { directionStatus().textContent = (t.secureCompass || "Compass capture requires a secure HTTPS connection."); + if (directionPanel()) directionPanel().dataset.state = "unavailable"; return; } if (typeof window.DeviceOrientationEvent === "undefined") { directionStatus().textContent = (t.compassUnavailable || "Compass unavailable on this device."); + if (directionPanel()) directionPanel().dataset.state = "unavailable"; return; } if (typeof DeviceOrientationEvent.requestPermission === "function") { @@ -123,10 +152,12 @@ try { if (await DeviceOrientationEvent.requestPermission(true) !== "granted") { if (directionStatus()) directionStatus().textContent = (t.compassDenied || "Compass permission denied."); + if (directionPanel()) directionPanel().dataset.state = "unavailable"; return; } } catch (error) { if (directionStatus()) directionStatus().textContent = (t.compassDenied || "Compass permission denied."); + if (directionPanel()) directionPanel().dataset.state = "unavailable"; return; } } @@ -136,7 +167,7 @@ const submitManualReport = async () => { setCount(occurrences.value); stopCompassReading(); - manualSubmit.disabled = true; + setSubmissionState("submitting", t.sendingReport || "Sending…"); if (burstListener?.active) { try { const audio = await burstListener.clipForSubmission(); @@ -144,21 +175,22 @@ if (audio) data.append("audio", audio); const response = await fetch(manualForm.action, { method: "POST", body: data, credentials: "same-origin" }); const html = await response.text(); - document.querySelector("#manual-submission").outerHTML = html; - if (response.ok) burstListener.submissionSucceeded(); + if (showResponse(html, response.ok)) burstListener.submissionSucceeded(); } catch (error) { - document.querySelector("#manual-submission").textContent = "Could not send the report. Please try again."; - } finally { - manualSubmit.disabled = false; + setSubmissionState("error", t.reportFailed || "Could not send the report. Please try again."); } return; } if (window.htmx) { - htmx.ajax("POST", manualForm.action, { - source: manualForm, - target: "#manual-submission", - swap: "outerHTML", - }); + try { + htmx.ajax("POST", manualForm.action, { + source: manualForm, + target: "#manual-submission", + swap: "outerHTML", + }); + } catch (error) { + setSubmissionState("error", t.reportFailed || "Could not send the report. Please try again."); + } } else { manualForm.submit(); } @@ -174,16 +206,16 @@ if (summary && directionDegrees.value !== "") { const heading = Number(directionDegrees.value); summary.textContent = `${cardinalDirection(heading)} · ${displayDegrees(heading)}°`; + directionPanel().dataset.state = "captured"; } }); manualSubmit.addEventListener("click", () => { if (manualSubmit.disabled) return; - manualSubmit.disabled = true; + setSubmissionState("submitting", t.requestingLocation || "Requesting your optional location…"); if (!navigator.geolocation) { submitManualReport(); return; } - manualStatus.textContent = (t.requestingLocation || "Requesting your optional location…"); navigator.geolocation.getCurrentPosition( (position) => { latitude.value = position.coords.latitude; @@ -194,7 +226,22 @@ { enableHighAccuracy: false, maximumAge: 300000, timeout: 10000 }, ); }); + manualForm.addEventListener("submit", (event) => { + event.preventDefault(); + if (!manualSubmit.disabled) manualSubmit.click(); + }); document.body.addEventListener("htmx:afterRequest", (event) => { - if (event.detail.elt === manualForm) manualSubmit.disabled = false; + if (event.detail.elt !== manualForm) return; + const response = event.detail.xhr; + if (!response) { + setSubmissionState("error", t.reportFailed || "Could not send the report. Please try again."); + } else if (response.status >= 400) { + showResponse(response.responseText, false); + } else { + setSubmissionState("submitted"); + } + }); + document.body.addEventListener("htmx:sendError", (event) => { + if (event.detail.elt === manualForm) setSubmissionState("error", t.reportFailed || "Could not send the report. Please try again."); }); diff --git a/frontend/src/styles/components/manual-report-structure.css b/frontend/src/styles/components/manual-report-structure.css index d15660c..e45e87e 100644 --- a/frontend/src/styles/components/manual-report-structure.css +++ b/frontend/src/styles/components/manual-report-structure.css @@ -44,6 +44,9 @@ border-radius: 1rem; background: #1a1739; } +.manual-direction-panel[data-state="measuring"] { border-color: #a27cff; } +.manual-direction-panel[data-state="captured"] { border-color: #68dba1; } +.manual-direction-panel[data-state="unavailable"] { border-color: #d38b7a; } .manual-direction-panel__topline { display: flex; align-items: center; justify-content: space-between; gap: 1rem; } .manual-direction-panel__summary { margin: .35rem 0 .8rem; color: #f5f0ff; font-weight: 700; } @@ -56,6 +59,16 @@ min-height: 4.5rem; } +.manual-report__submit:disabled { cursor: wait; opacity: .72; } +.manual-submission { display: grid; justify-items: center; gap: .5rem; max-width: 36rem; margin-top: 1rem; padding: 1.3rem; border: 1px solid #4caa82; border-radius: 1rem; color: #eafff2; text-align: center; background: #10352e; } +.manual-submission__icon { display: grid; width: 3.5rem; height: 3.5rem; place-items: center; border: 2px solid #6ce4a8; border-radius: 50%; color: #eafff2; font-size: 2rem; background: #226c51; } +.manual-submission strong { font-size: 1.35rem; } +.manual-submission p { margin: 0; line-height: 1.45; } +.manual-submission__summary { margin-top: .35rem !important; color: #b2e8cf; font-size: .9rem; } +.manual-submission--error { border-color: #d37c6a; color: #fff0e8; background: #3a1c2b; } +.manual-submission--error .manual-submission__icon { border-color: #ffac8c; background: #9b493a; } +.manual-report__submission:empty { display: none; } + .manual-report button:focus-visible, .manual-report input:focus-visible { outline: 3px solid #c9b4ff; outline-offset: 3px; } .manual-report__supporting { @@ -85,6 +98,10 @@ :root[data-theme="light"] .manual-direction-panel__open, :root[data-theme="light"] .manual-direction-panel__close { color: #321d42; border-color: #b68bb5; background: #ead2eb; } :root[data-theme="light"] .manual-direction-panel__summary { color: #321d42; } +:root[data-theme="light"] .manual-submission { border-color: #54a67b; color: #164d34; background: #e8fff2; } +:root[data-theme="light"] .manual-submission__icon { color: #fff; } +:root[data-theme="light"] .manual-submission__summary { color: #2b6b48; } +:root[data-theme="light"] .manual-submission--error { border-color: #ca7d65; color: #743520; background: #fff0e8; } :root[data-theme="light"] .manual-report__supporting { border-color: #d9b4d8; diff --git a/templates/js_translations.html b/templates/js_translations.html index c94abbb..57085df 100644 --- a/templates/js_translations.html +++ b/templates/js_translations.html @@ -21,6 +21,8 @@ window.BOOMCAST_I18N = { compassPermission: "{% trans 'Requesting compass permission…' as value %}{{ value|escapejs }}", compassDenied: "{% trans 'Compass permission denied.' as value %}{{ value|escapejs }}", requestingLocation: "{% trans 'Requesting your optional location…' as value %}{{ value|escapejs }}", + sendingReport: "{% trans 'Sending…' as value %}{{ value|escapejs }}", + reportFailed: "{% trans 'Could not send the report. Please try again.' as value %}{{ value|escapejs }}", mapCompass: "{% trans 'Map compass: north is up' as value %}{{ value|escapejs }}", reports: "{% trans 'reports' as value %}{{ value|escapejs }}", occurrences: "{% trans 'occurrences' as value %}{{ value|escapejs }}",