Organising

This commit is contained in:
Fred Boniface 2025-06-26 16:41:46 +01:00
parent eb580132cc
commit b6c590eab0
3 changed files with 62 additions and 36 deletions

View File

@ -13,11 +13,6 @@ app.get("/test", (req: Request, res: Response) => {
app.post('/submit', (req, res) => { app.post('/submit', (req, res) => {
const report = req.body; const report = req.body;
// For now, just log it
console.log('📥 New form submission:');
// TODO: Validate and write to MongoDB later
handleFormData(report); handleFormData(report);
res.status(200).json({ status: 'ok', message: 'Form received' }); res.status(200).json({ status: 'ok', message: 'Form received' });
}); });

View File

@ -14,11 +14,9 @@ const dataPromise = fetch("units.converted.json")
window.location.reload(); window.location.reload();
}); });
window.addEventListener('DOMContentLoaded', () => { window.addEventListener('DOMContentLoaded', () => {
const input = document.getElementById('unitNumber'); const input = document.getElementById('unitNumber');
const resultDiv = document.getElementById('formExpansion'); const resultDiv = document.getElementById('formExpansion');
input.addEventListener('input', async () => { input.addEventListener('input', async () => {
await dataPromise; await dataPromise;
@ -41,9 +39,7 @@ window.addEventListener("load", retryOfflineReports);
async function loadForm(values) { async function loadForm(values) {
const formExpansion = document.getElementById('formExpansion'); const formExpansion = document.getElementById('formExpansion');
const formHidden = document.getElementById('formHidden'); const formHidden = document.getElementById('formHidden');
formExpansion.innerHTML = ''; formExpansion.innerHTML = '';
const heading = document.createElement('h2'); const heading = document.createElement('h2');
heading.textContent = "Choose all areas where A/C failed"; heading.textContent = "Choose all areas where A/C failed";
formExpansion.appendChild(heading); formExpansion.appendChild(heading);
@ -52,29 +48,23 @@ async function loadForm(values) {
const coachTitle = document.createElement('h3'); const coachTitle = document.createElement('h3');
coachTitle.textContent = vehicle.id; coachTitle.textContent = vehicle.id;
formExpansion.appendChild(coachTitle); formExpansion.appendChild(coachTitle);
for (const zone of vehicle.zones) { for (const zone of vehicle.zones) {
const checkboxId = `${zone}-${vehicle.id}`; const checkboxId = `${zone}-${vehicle.id}`;
const wrapper = document.createElement('div'); const wrapper = document.createElement('div');
wrapper.className = 'checkbox-wrapper'; wrapper.className = 'checkbox-wrapper';
const checkbox = document.createElement('input'); const checkbox = document.createElement('input');
checkbox.type = 'checkbox'; checkbox.type = 'checkbox';
checkbox.id = checkboxId; checkbox.id = checkboxId;
checkbox.name = checkboxId; checkbox.name = checkboxId;
checkbox.title = zone; checkbox.title = zone;
const label = document.createElement('label'); const label = document.createElement('label');
label.htmlFor = checkboxId; label.htmlFor = checkboxId;
label.textContent = zone; label.textContent = zone;
wrapper.appendChild(checkbox); wrapper.appendChild(checkbox);
wrapper.appendChild(label); wrapper.appendChild(label);
formExpansion.appendChild(wrapper); formExpansion.appendChild(wrapper);
} }
} }
formHidden.style = 'display:block'; formHidden.style = 'display:block';
} }
@ -85,13 +75,10 @@ function reset() {
async function formSubmit(event) { async function formSubmit(event) {
event.preventDefault(); event.preventDefault();
const form = event.target; const form = event.target;
const formData = new FormData(form); const formData = new FormData(form);
const submitButton = form.querySelector('button[type="submit"]'); const submitButton = form.querySelector('button[type="submit"]');
submitButton.disabled = true; submitButton.disabled = true;
const data = {}; const data = {};
for (const [key, value] of formData.entries()) { for (const [key, value] of formData.entries()) {
if (data[key]) { if (data[key]) {
@ -101,16 +88,13 @@ async function formSubmit(event) {
data[key] = value; data[key] = value;
} }
} }
data.utcTimestamp = new Date().toISOString(); data.utcTimestamp = new Date().toISOString();
try { try {
const res = await fetch("/submit", { const res = await fetch("/submit", {
method: "POST", method: "POST",
headers: {"Content-Type": "application/json"}, headers: {"Content-Type": "application/json"},
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
if (!res.ok) throw new Error("Submission failed"); if (!res.ok) throw new Error("Submission failed");
console.log("Form Submitted") console.log("Form Submitted")
showStatus("✅ Form received, thank-you.", type="success"); showStatus("✅ Form received, thank-you.", type="success");
@ -134,13 +118,9 @@ function saveReportOffline(report) {
async function retryOfflineReports() { async function retryOfflineReports() {
const key = "offlineReports"; const key = "offlineReports";
const stored = JSON.parse(localStorage.getItem(key) || "[]"); const stored = JSON.parse(localStorage.getItem(key) || "[]");
if (stored.length === 0) return; if (stored.length === 0) return;
console.log(`Attempting to resend ${stored.length} stored report(s)...`); console.log(`Attempting to resend ${stored.length} stored report(s)...`);
const remaining = []; const remaining = [];
for (const report of stored) { for (const report of stored) {
try { try {
const res = await fetch("/submit", { const res = await fetch("/submit", {
@ -156,7 +136,6 @@ async function retryOfflineReports() {
remaining.push(report); remaining.push(report);
} }
} }
if (remaining.length === 0) { if (remaining.length === 0) {
localStorage.removeItem(key); localStorage.removeItem(key);
} else { } else {
@ -169,7 +148,6 @@ function showStatus(message, type) {
statusDiv.textContent = message; statusDiv.textContent = message;
statusDiv.className = `status-${type}`; statusDiv.className = `status-${type}`;
statusDiv.style = 'display:flex'; statusDiv.style = 'display:flex';
setTimeout(() => { setTimeout(() => {
statusDiv.style = 'display:none'; statusDiv.style = 'display:none';
}, 5000); }, 5000);

View File

@ -1416,20 +1416,73 @@
} }
], ],
"175002": [ "175002": [
{ "id": "A", "zones": ["saloon", "cab"] }, {
{ "id": "B", "zones": ["saloon", "cab"] } "id": "A",
], "zones": [
"175007": [ "saloon",
{ "id": "A", "zones": ["saloon", "cab"] }, "cab"
{ "id": "B", "zones": ["saloon", "cab"] } ]
], },
"175009": [ {
{ "id": "A", "zones": ["saloon", "cab"] }, "id": "B",
{ "id": "B", "zones": ["saloon", "cab"] } "zones": [
], "saloon",
"175114": [ "cab"
{ "id": "A", "zones": ["saloon", "cab"] }, ]
{ "id": "B", "zones": ["saloon"] }, }
{ "id": "C", "zones": ["saloon", "cab"] } ],
"175007": [
{
"id": "A",
"zones":[
"saloon",
"cab"
]
},
{
"id": "B",
"zones": [
"saloon",
"cab"
]
}
],
"175009": [
{
"id": "A",
"zones": [
"saloon",
"cab"
]
},
{
"id": "B",
"zones": [
"saloon",
"cab"
]
}
],
"175114": [
{
"id": "A",
"zones": [
"saloon",
"cab"
]
},
{
"id": "B",
"zones": [
"saloon"
]
},
{
"id": "C",
"zones": [
"saloon",
"cab"
]
}
] ]
} }