This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
web/js/pis.js

60 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-04-22 21:46:34 +01:00
hideLoading()
async function findByOrigDest() {
showLoading()
const formData = await fetchOrigDest()
log(`findByOrigDest: Searching for PIS Code for ${JSON.stringify(formData)}`)
2023-04-24 12:14:45 +01:00
const endpoint = `pis/origdest/${formData.origin}/${formData.destination}`
2023-04-22 21:46:34 +01:00
const json = await getApi(endpoint)
if (json == false) {
await noData()
} else {
2023-04-22 22:23:36 +01:00
await insertData(json)
2023-04-22 21:46:34 +01:00
}
document.getElementById('crs-box').style = 'display:none'
document.getElementById('result-box').style = 'display:block'
hideLoading()
}
async function fetchOrigDest() {
var orig = document.getElementById("origin").value
var dest = document.getElementById("destination").value
return {origin: orig, destination: dest}
}
2023-04-22 22:23:36 +01:00
async function insertData(json) {
2023-04-22 21:48:55 +01:00
// Receives the JSON Respose ([{},{}]) containing one or more possible
// PIS codes. Display the code and the stops with a method of scrolling between them.
// Maybe as a table or a carousel?
const div = document.getElementById('result-box')
2023-04-22 22:44:32 +01:00
let tableData = `<table id="result-table">
2023-04-22 22:23:36 +01:00
<tr>
<th class="code">Code</th>
<th class="stops">Stations</th>
2023-04-22 22:44:32 +01:00
</tr>`
div.insertAdjacentHTML("beforeend", tableHtml)
const data = JSON.parse(json)
2023-04-22 22:44:32 +01:00
let results = 0
2023-04-22 22:23:36 +01:00
for(var i = 0; i < data.length; i++) { // Break into separate function. Need to
2023-04-22 22:45:24 +01:00
// iterate over stops and print them in the
// format: `AVN, PRI, SHH... etc.`
2023-04-22 22:44:32 +01:00
tableData += `<tr><td class="code">${data[i][code]}</td>
2023-04-22 22:23:36 +01:00
<td class="stops">${data[i][stops]}</td></tr>`
2023-04-22 22:44:32 +01:00
results++
}
2023-04-22 22:44:32 +01:00
tableData += "</table>"
2023-04-22 22:46:46 +01:00
document.getElementById('result-table').insertAdjacentHTML("beforeend", tableData)
2023-04-22 22:44:32 +01:00
document.getElementById('result-count').textContent = results
2023-04-22 21:46:34 +01:00
}
async function noData() {
2023-04-22 22:47:20 +01:00
const msg = '<p>No results found</p>'
2023-04-22 21:46:34 +01:00
document.getElementById('result-box').insertAdjacentHTML("beforeend", msg)
}
async function reset() {
2023-04-22 21:52:42 +01:00
document.getElementById('origin').value = ""
document.getElementById('destination').value = ""
2023-04-22 21:46:34 +01:00
document.getElementById('result-box').style = 'display:none'
document.getElementById('crs-box').style = 'display:block'
}