From 51e91981e699c14b400564f2b0d71bee861aec17 Mon Sep 17 00:00:00 2001 From: williameriksson126 Date: Tue, 24 Nov 2020 11:54:59 +0100 Subject: [PATCH] Add NearbyStation class is a form that given latitude and longitude coordinates gives nearby locations --- src/App.js | 1 + src/components/AccessToken.js | 10 +++- src/components/NearbyStation.js | 101 ++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/components/NearbyStation.js diff --git a/src/App.js b/src/App.js index 575611b..c603e2d 100644 --- a/src/App.js +++ b/src/App.js @@ -5,6 +5,7 @@ import Header from './components/Header.js' import PageArea from './components/PageArea.js' import BottomMenu from './components/BottomMenu.js' import AccessToken from './components/AccessToken.js' +import NearbyStation from './components/NearbyStation'; class App extends Component { render() { diff --git a/src/components/AccessToken.js b/src/components/AccessToken.js index 0086423..b0014f1 100644 --- a/src/components/AccessToken.js +++ b/src/components/AccessToken.js @@ -28,6 +28,12 @@ class AccessToken extends React.Component { responseFromVT: response.data.access_token, token: 'token sent' }) + .error(res => { + this.setState({ + responseFromVT: "some error", + token: "some error occured" + }) + }) }) } @@ -59,5 +65,5 @@ export default AccessToken //grant_type=client_credentials&scope= -//BPvMjPidHckBtETZxr3dHP1rptQa -//z5MFCS_wwmqprc0s4iLZWBAUJdga \ No newline at end of file +//5ty7gxmAfQlUHDHdm7kgaqXwK5Ia +//wpIOURnJJcTtO6rORYmYYPq4wXka \ No newline at end of file diff --git a/src/components/NearbyStation.js b/src/components/NearbyStation.js new file mode 100644 index 0000000..2e50077 --- /dev/null +++ b/src/components/NearbyStation.js @@ -0,0 +1,101 @@ +import React from 'react'; +import axios from 'axios'; + +/* +const StopLocation = ({ name, id, lat, long, track }) => ( +
+
+

{name}

+

{id}

+

{lat}

+

{lon}

+

{track}

+
+
+); +*/ +class NearbyStation extends React.Component { + state = { + lat: '57.5987', + long: '11.9454', + token: '2d596c20-a6e7-3272-8df6-51ed2468da63', + stops: [], + } + + + handleChangeLat = event => { + this.setState({ lat: event.target.value}); + } + + handleChangeLong = event => { + this.setState({ long: event.target.value}); + } + + handleSubmit = event => { + event.preventDefault(); + + const headers = { + 'Authorization': 'Bearer ' + this.state.token + }; + + console.log('Attempted connection') + + axios.get('https://api.vasttrafik.se/bin/rest.exe/v2/location.nearbystops?originCoordLat='+this.state.lat+'&originCoordLong='+this.state.long+'&maxNo=20&format=json', { headers }) + .then(response => { + console.log(response.data.LocationList.StopLocation) + this.setState({ + stops: response.data.LocationList.StopLocation, + }) + }) + } + + render() { + return ( +
+
+ + + +
+

+ {this.state.lat} +

+

+ {this.state.long} +

+ {this.state.stops.map((item) => +
+

+ {item.name}, + {item.id}, + {item.lat}, + {item.lon}, + {item.track} +

+
+ )} +
+ ) + } +} +export default NearbyStation + +//https://api.vasttrafik.se/bin/rest.exe/v2/location.nearbystops?originCoordLat=57.5987&originCoordLong=11.9454&maxNo=20&format=json + +//https://api.vasttrafik.se/bin/rest.exe/v2/location.nearbystops + + +//'https://reqres.in/api/articles' + +//'https://api.vasttrafik.se/token' + +//grant_type=client_credentials&scope= + +//BPvMjPidHckBtETZxr3dHP1rptQa +//z5MFCS_wwmqprc0s4iLZWBAUJdga \ No newline at end of file