manual submission redesign phase D
This commit is contained in:
@@ -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.");
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user