From 1c8839ca1794db966a3118923c5e7eae896e9651 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 22 Apr 2023 21:46:34 +0100 Subject: [PATCH] Add PIS Page and some logic --- js/lib.main.js | 46 ++++++++++++++++++++++++++++++++-- js/pis.js | 37 +++++++++++++++++++++++++++ pis.html | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ styles/pis.css | 7 ++++++ 4 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 js/pis.js create mode 100644 pis.html create mode 100644 styles/pis.css diff --git a/js/lib.main.js b/js/lib.main.js index 9cc9747..e69940f 100644 --- a/js/lib.main.js +++ b/js/lib.main.js @@ -45,7 +45,7 @@ const delay = ms => new Promise(res => setTimeout(res, ms)); /* Maintains backwards compatibility for previous implementation of log helper */ async function log(msg, type) { - const mode = "dev" + const mode = "tst" if (mode === "prod" && type != "ERR") { return; } @@ -126,7 +126,49 @@ async function getQuery(param) { } } -async function vibe(type) { // Offers three standard vibration patterns for consistency +async function getApi(path,auth = false) { + let apiVer = 'v1' + let url = `https://${window.location.origin}/api/${apiVer}/${path}` + log(`getApi: Fetching from endpoint: ${url}, Auth=${auth}`) + try { + var resp = await fetch(url) + if (resp != 200) { + return false + } + if (!resp.ok) { + return false + } + if (resp.json() === "[]") { + return 'empty' + } + return resp.json(); + } catch(err) { + return false + } +} + +async function getApi(path,auth = false) { + let apiVer = 'v1' + let url = `https://${window.location.origin}/api/${apiVer}/${path}` + log(`getApi: Fetching from endpoint: ${url}, Auth=${auth}`) + try { + var resp = await fetch(url) + if (resp != 200) { + return false + } + if (!resp.ok) { + return false + } + if (resp.json() === "[]") { + return 'empty' + } + return resp.json(); + } catch(err) { + return false + } +} + +async function vibe(type) { let canVibrate = "vibrate" in navigator || "mozVibrate" in navigator if (canVibrate && !("vibrate" in navigator)){ navigator.vibrate = navigator.mozVibrate diff --git a/js/pis.js b/js/pis.js new file mode 100644 index 0000000..259aabb --- /dev/null +++ b/js/pis.js @@ -0,0 +1,37 @@ +hideLoading() + +async function findByOrigDest() { + showLoading() + const formData = await fetchOrigDest() + log(`findByOrigDest: Searching for PIS Code for ${JSON.stringify(formData)}`) + const endpoint = `pis/${formData.origin}/${formData.destination}` + const json = await getApi(endpoint) + if (json == false) { + await noData() + } else { + await displayData(json) + } + 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} +} + +async function displayData(json) { + log(`displayData: ${JSON.stringify(json)}`) +} + +async function noData() { + msg = '

No results found

' + document.getElementById('result-box').insertAdjacentHTML("beforeend", msg) +} + +async function reset() { + document.getElementById('result-box').style = 'display:none' + document.getElementById('crs-box').style = 'display:block' +} \ No newline at end of file diff --git a/pis.html b/pis.html new file mode 100644 index 0000000..bddeee8 --- /dev/null +++ b/pis.html @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + OwlBoard - PIS + + + + + + +
+ + + + Home + + +
+ + + + + + OwlBoard Logo + +

PIS Codes

+ +
+
+
+

Searching

+
+ +
+

Enter a services start and end CRS stations to see code options and + stopping patterns.

+

Currently supported (Beta): GWR Bristol Metro region only.

+
+

+
+

+ +
+ +
+

Results

+ +
+ + + + + + \ No newline at end of file diff --git a/styles/pis.css b/styles/pis.css new file mode 100644 index 0000000..8c42fb8 --- /dev/null +++ b/styles/pis.css @@ -0,0 +1,7 @@ +#crs-boxes { + display: block; +} + +#result-box { + display: none; +} \ No newline at end of file