Fixes for data requests failing

This commit is contained in:
Lucas
2021-09-27 17:41:15 -04:00
parent f5fab69877
commit 2bb0af5c6b
3 changed files with 52 additions and 62 deletions

View File

@@ -1,38 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- <link rel="stylesheet" href="/static/css/style.css">-->
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="/static/js/DataRequests.js"></script>
<meta charset="UTF-8">
<title>SUBWAY DISPLAY!</title>
</head>
<body>
<div id="main_div">
<div id="container">
<span>PI MTA TRACKER</span>
</div>
<div class="station_div">
<div class="station_header">
<span>Times Square Station</span>
</div>
<div class="direction_div">
<div class="direction_header"> North</div>
<div>Train 1 Info</div>
<div>Train 2 Info</div>
<div>Train 3 Info</div>
</div>
<div class="direction_div">
<div class="direction_header"> South</div>
<div>Train 1 Info</div>
<div>Train 2 Info</div>
<div>Train 3 Info</div>
</div>
</div>
<button id="test_button">Test Button</button>
<span id="result"></span>
</div>

View File

@@ -2,22 +2,21 @@ $(document).ready(function () {
const interval = setInterval(function () {
updateData($('#station_1'))
updateData($('#station_2'))
}, 5000);
const interval2 = setInterval(function () {
updateData($('#station_2'))
}, 6000);
function updateData(station) {
$.ajax({
type: "POST",
//the url where you want to sent the userName and password to
url: '/mta_data',
contentType: "application/json",
dataType: "json",
async: true,
//json object to sent to the authentication url
data: JSON.stringify({"station": station.find('.station-name:first').get(0).innerText}, null, '\t'),
success: function (data, text) {
// console.log(data)
updateStation(station, data)
},
error: function (request, status, error) {
@@ -27,32 +26,58 @@ $(document).ready(function () {
}
function updateStation(station, data) {
//get first item
updateDirections(station, data, "North");
updateDirections(station, data, "South");
}
function updateDirections(station, data, direction) {
// console.log(direction)
// console.log(".card:".concat(direction === "North" ? "first" : "last"))
n = data[direction]
// console.log(data[direction])
list_items = station.find(".card:".concat(direction === "North" ? "first" : "last")).find(".station-info")
var i = 0;
for (var train in n) {
// console.log(train)
// console.log(list_items)
updateLineItem(list_items.get(i), n[train], train)
i = i + 1
}
if (i < 3) {
console.log("Only 2 items updated")
for (let remainingIndex = i; remainingIndex < 3; remainingIndex++) {
updateLineItem(list_items.get(i), "No Trains Available", "N/A")
}
}
}
function updateLineItem(listItem, times, train) {
// console.log(times)
$(listItem).find("img").attr("src", "/static/images/lines/" + train + ".svg")
$(listItem).find("h1").text(times.sort(function (a, b) {
return a - b;
}).join(", "));
var timeString;
if (typeof times === 'string' || times instanceof String) {
timeString = times;
} else {
timeString = times.sort(function (a, b) {
return a - b;
}).join(", ");
}
$(listItem).find("h1").text(timeString);
if (imageExists("/static/images/lines/" + train + ".svg")) {
$(listItem).show()
$(listItem).find("img").attr("src", "/static/images/lines/" + train + ".svg")
} else if (train === "N/A"){
console.log("Route Is NA - Disabling Route")
console.log($(listItem))
$(listItem).hide()
}
}
function imageExists(image_url) {
var http = new XMLHttpRequest();
http.open('HEAD', image_url, false);
http.send();
return http.status != 404;
}
});