manual submission redesign phase D
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
{% load i18n %}
|
||||
<div class="manual-direction-panel{% if measuring %} manual-direction-panel--measuring{% endif %}" id="manual-direction-panel">
|
||||
<div class="manual-direction-panel{% if measuring %} manual-direction-panel--open{% endif %}" id="manual-direction-panel" data-state="{% if measuring %}ready{% else %}idle{% endif %}">
|
||||
{% if measuring %}
|
||||
<div class="manual-direction-panel__topline">
|
||||
<strong class="manual-direction-panel__heading">{% trans "Capture direction" %}</strong>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<h2 class="manual-report__title" id="manual-report-title">{% trans "Report fireworks now" %}</h2>
|
||||
<p class="manual-report__intro">{% trans "Choose how many times you heard fireworks, then send it straight to Pending. The report is timestamped now." %}</p>
|
||||
</header>
|
||||
<form id="manual-report-form" action="{% url "fireworks_detect:submit_manual_pending" %}" method="post" enctype="multipart/form-data" hx-post="{% url "fireworks_detect:submit_manual_pending" %}" hx-target="#manual-submission" hx-swap="outerHTML">
|
||||
<form id="manual-report-form" action="{% url "fireworks_detect:submit_manual_pending" %}" method="post" enctype="multipart/form-data">
|
||||
{% csrf_token %}
|
||||
<input id="manual-latitude" name="latitude" type="hidden">
|
||||
<input id="manual-longitude" name="longitude" type="hidden">
|
||||
@@ -21,7 +21,7 @@
|
||||
</div>
|
||||
</div>
|
||||
{% include "fireworks_detect/includes/manual_direction_panel.html" %}
|
||||
<button id="manual-submit" class="manual-report__submit" type="button"><span aria-hidden="true">🔥</span> {% trans "I heard fireworks" %}</button>
|
||||
<button id="manual-submit" class="manual-report__submit" type="button"><span aria-hidden="true">🔥</span> <span id="manual-submit-label">{% trans "I heard fireworks" %}</span></button>
|
||||
</div>
|
||||
<section class="manual-report__supporting" data-state="off" aria-labelledby="manual-listening-title">
|
||||
<div class="manual-report__supporting-header">
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
{% load i18n %}
|
||||
<div id="manual-submission" role="status">
|
||||
<div id="manual-submission" class="manual-submission{% if error %} manual-submission--error{% else %} manual-submission--success{% endif %}" role="status" data-result="{% if error %}error{% else %}success{% endif %}">
|
||||
{% if error %}
|
||||
<strong>{% trans "Could not report fireworks" %}</strong><p>{{ error }}</p>
|
||||
<span class="manual-submission__icon" aria-hidden="true">!</span>
|
||||
<strong>{% trans "Could not report fireworks" %}</strong>
|
||||
<p>{{ error }}</p>
|
||||
{% else %}
|
||||
<strong>{% trans "Report sent to Pending" %}</strong><p>{% 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 %}</p>
|
||||
<span class="manual-submission__icon" aria-hidden="true">✓</span>
|
||||
<strong>{% trans "Report received!" %}</strong>
|
||||
<p>{% trans "Thank you. Your report helps map fireworks nearby." %}</p>
|
||||
<p class="manual-submission__summary">
|
||||
{{ 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 %}
|
||||
</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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.");
|
||||
});
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 }}",
|
||||
|
||||
Reference in New Issue
Block a user