Frontend: Improve code tidyness

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-01-18 21:37:01 +00:00
parent 393c59c1d7
commit c221639c89
2 changed files with 79 additions and 41 deletions

View File

@ -6,7 +6,6 @@
* Frontend - Implement paginated NRCC Notices as they take up lots of space. * Frontend - Implement paginated NRCC Notices as they take up lots of space.
- Carousel: https://www.section.io/engineering-education/how-to-make-an-image-carousel-for-your-website/ - Carousel: https://www.section.io/engineering-education/how-to-make-an-image-carousel-for-your-website/
* Frontend - If Ferry Services Present - Load at bottom of screen. * Frontend - If Ferry Services Present - Load at bottom of screen.
* Frontend - Load train services to screen.
* Frontend - Decide what to do with Bus Services. * Frontend - Decide what to do with Bus Services.
* Frontend - Implement error pages - will need to look at including these on the proxy container too? * Frontend - Implement error pages - will need to look at including these on the proxy container too?
- https://expressjs.com/en/guide/error-handling.html - https://expressjs.com/en/guide/error-handling.html

View File

@ -104,56 +104,95 @@ async function displayNoTrains() {
async function displayTrains(data) { async function displayTrains(data) {
log(`Inserting data in DOM`) log(`Inserting data in DOM`)
var table = document.getElementById("output");
for(var i = 0; i < data.GetStationBoardResult.trainServices.service.length; i++) { for(var i = 0; i < data.GetStationBoardResult.trainServices.service.length; i++) {
// Reset Vars // Reset Vars
var svc = data.GetStationBoardResult.trainServices.service[i]; var svc = data.GetStationBoardResult.trainServices.service[i];
var eaChg = ""; displayService(svc);
var edChg = ""; }
clearLoading();
log(`Insertion complete`)
}
async function displayService(svc) {
var table = document.getElementById("output");
// Determine Time Message // Determine Time Message
if (svc.sta){var sta = svc.sta}else{var sta = '-'}; var sta = await parseTime(svc.sta);
if (svc.eta){var eta = svc.eta}else{var eta = '-'}; var eta = await parseTime(svc.eta);
if (eta=="On time"){var eta='RT'}else if(eta=="Delayed"){var eta="LATE";var eaChg="changed";}else if(eta!='-'){var eaChg="changed";}; var std = await parseTime(svc.std);
if (svc.std){var std = svc.std}else{var std = '-'}; var etd = await parseTime(svc.etd);
if (svc.etd){var etd = svc.etd}else{var etd = '-'};
if (etd=="On time"){var etd='RT';}else if(etd=="Delayed"){var etd="LATE";var edChg="changed";}else if(etd!='-'){var edChg="changed";};
// Determine Platform Message // Determine Platform Message
if (svc.platform){var plt = svc.platform}else{var plt = "-"}; //if (svc.platform != undefined){var plt = svc.platform} else {var plt = "-"};
var plt = await parsePlatform(svc);
// Define Table Row // Define Table Row
var row = ` var row = `
<table> <table>
<tr> <tr>
<td class="name">${svc.origin.location.locationName}</td> <td class="name">${svc.origin.location.locationName}</td>
<td class="name">${svc.destination.location.locationName}</td> <td class="name">${svc.destination.location.locationName}</td>
<td class="plat">${plt}</td> <td class="plat">${plt.num}</td>
<td class="time">${sta}</td> <td class="time">${sta.data}</td>
<td class="time ${eaChg}">${eta}</td> <td class="time ${eta.changed}">${eta.data}</td>
<td class="time">${std}</td> <td class="time">${std.data}</td>
<td class="time ${edChg}">${etd}</td> <td class="time ${etd.changed}">${etd.data}</td>
</tr> </tr>
</table>` </table>`
// Put Table Row // Put Table Row
table.insertAdjacentHTML("beforeend", row) table.insertAdjacentHTML("beforeend", row)
// Parse cancelReason & delayReason // Parse cancelReason & delayReason
if (svc.cancelReason) { if (svc.cancelReason) {
console.log(`Cancel reason: ${svc.cancelReason}`)
var cancelRow = `<p class="msg">${svc.cancelReason}</p>` var cancelRow = `<p class="msg">${svc.cancelReason}</p>`
table.insertAdjacentHTML("beforeend", cancelRow); table.insertAdjacentHTML("beforeend", cancelRow);
} }
if (svc.delayReason) { if (svc.delayReason) {
console.log(`Delay reason: ${svc.delayReason}`)
var delayRow = `<p class="msg">${svc.delayReason}</p>` var delayRow = `<p class="msg">${svc.delayReason}</p>`
table.insertAdjacentHTML("beforeend", delayRow); table.insertAdjacentHTML("beforeend", delayRow);
} }
}
async function parsePlatform(svc){
if (svc.platform != undefined) {
var platform = svc.platform;
} else {
var platform = "-";
} }
clearLoading(); if (svc.platformChanged) {
log(`Insertion complete`) var changed = "changed";
} else {
var changed = "";
}
return {num: platform, change: changed}
} }
async function parsePlatform(){ async function parseTime(string){
} switch (string) {
case "Delayed":
async function parseExpTime(){ var output = "LATE";
var change = "changed";
break;
case "Cancelled":
var output = "CANC";
var change = "changed";
break;
case "On time":
var output = "RT";
var change = "";
break;
case "":
var output = "-";
var change = "";
break;
case undefined:
var output = "";
var change = "";
break;
default:
var output = string;
var change = "changed";
}
return {data: output, changed: change};
} }
async function parseName(){ async function parseName(){