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

View File

@ -1416,20 +1416,73 @@
}
],
"175002": [
{ "id": "A", "zones": ["saloon", "cab"] },
{ "id": "B", "zones": ["saloon", "cab"] }
{
"id": "A",
"zones": [
"saloon",
"cab"
]
},
{
"id": "B",
"zones": [
"saloon",
"cab"
]
}
],
"175007": [
{ "id": "A", "zones": ["saloon", "cab"] },
{ "id": "B", "zones": ["saloon", "cab"] }
{
"id": "A",
"zones":[
"saloon",
"cab"
]
},
{
"id": "B",
"zones": [
"saloon",
"cab"
]
}
],
"175009": [
{ "id": "A", "zones": ["saloon", "cab"] },
{ "id": "B", "zones": ["saloon", "cab"] }
{
"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"] }
{
"id": "A",
"zones": [
"saloon",
"cab"
]
},
{
"id": "B",
"zones": [
"saloon"
]
},
{
"id": "C",
"zones": [
"saloon",
"cab"
]
}
]
}