-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.js
74 lines (61 loc) · 2.78 KB
/
a.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//establishing userinput into a separate variable
let station_1 = document.getElementById("station_1").value.trim();
//THE URL
let api_url = `https://rata.digitraffic.fi/api/v1/live-trains/station/${station_1}`
const specs = `?arrived_trains=5&arriving_trains=5&departed_trains=5&departing_trains=5&include_nonstopping=false`;
const url = api_url + specs;
//FUNCTIONS
async function getData() {
const station_1 = document.getElementById("station_1").value.trim();
//if userinput is null, app gives an error message
if (!station_1) return alert("Enter a station, please.");
//external API's url with the station being
//a variable that changes via userinput
let api_url = `https://rata.digitraffic.fi/api/v1/live-trains/station/${station_1}`;
const specs = `?arrived_trains=5&arriving_trains=5&departed_trains=5&departing_trains=5&include_nonstopping=false`;
const url = api_url + specs;
try {
//fetching the text-area's id from HTML, assigning value
document.getElementById("traindata").value = "Loading...";
//calling fetch, passing in the url
const response = await fetch(url);
//if the response is not acceptable...
if (!response.ok) throw new Error("Error");
const data = await response.json();
//displays the response to the userinput
document.getElementById("traindata").value = JSON.stringify(data, null, 4);
} catch (error) {
//error message
alert(error);
}
if (station_1) {
//if station_1's input is fetched correctly...
}
}
//"RAW DATA" (CONTAINER) FUNCTION
async function getTrainData() {
const station = document.getElementById("station").value.trim();
//if userinput is null, app gives an error message
if (!station) return alert("Enter a station, please.");
//external API's url with the station being
//a variable that changes via userinput
let api_url = `https://rata.digitraffic.fi/api/v1/live-trains/station/${station}`;
const specs = `?arrived_trains=5&arriving_trains=5&departed_trains=5&departing_trains=5&include_nonstopping=false`;
const url = api_url + specs;
//const second_url = `https://rata.digitraffic.fi/api/v1/metadata/stations`;
//||
try {
//fetching the text-area's id from HTML, assigning value
document.getElementById("rawdata").value = "Loading...";
//calling fetch, passing in the url
const response = await fetch(url);
//if the response is not acceptable...
if (!response.ok) throw new Error("Error");
const data = await response.json();
//displays the response to the userinput
document.getElementById("rawdata").value = JSON.stringify(data, null, 4);
} catch (error) {
//error message
alert(error);
}
}