Skip to content

Commit

Permalink
adding update
Browse files Browse the repository at this point in the history
  • Loading branch information
ar5had committed Mar 22, 2017
1 parent 94158c2 commit 732be0a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -17,7 +17,8 @@
"openlayers": "^3.19.1",
"react": "^15.4.1",
"react-dom": "^15.4.1",
"react-tap-event-plugin": "^2.0.1"
"react-tap-event-plugin": "^2.0.1",
"unfetch": "^2.1.2"
},
"scripts": {
"start": "react-scripts start",
Expand Down
13 changes: 12 additions & 1 deletion src/GreenNav.js
Expand Up @@ -7,6 +7,7 @@ import Dialog from 'material-ui/Dialog';
import Toggle from 'material-ui/Toggle';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
import fetch from 'unfetch';

import Menu from './components/Menu';
import GreenNavMap from './components/GreenNavMap';
Expand Down Expand Up @@ -38,9 +39,19 @@ export default class GreenNav extends Component {
const destinationOsmId = waypoints[waypoints.length - 1];
const url = `${GreenNavServerAddress}from/${startOsmId}/to/${destinationOsmId}`;
fetch(url)
.then(response => response.json())
.then((response) => {
if (response.status > 400) {
throw new Error('Failed to load route data!');
}
else {
return response.json();
}
})
.then((route) => {
this.map.setRoute(route);
})
.catch((err) => {
console.error(`Error: ${err}`);
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/components/Menu.js
Expand Up @@ -78,7 +78,6 @@ export default class Menu extends Component {
vehicle: 0,
open: this.props.open,
dataSource: [],
route: [],
autoCompletes: [{ id: 1, label: 'From', route: '' }, { id: 2, label: 'To', route: '' }]
};
this.autoCompleteId = 3;
Expand All @@ -102,7 +101,8 @@ export default class Menu extends Component {
getRoute = () => {
const autoCompletes = this.state.autoCompletes;
if (autoCompletes.every(elem => elem.route !== '')) {
this.props.getRoute(this.state.route);
const routes = autoCompletes.map(elem => elem.route);
this.props.getRoute(routes);
}
else {
// TODO: implement notifications (Thomas GSoC Project - see polymer reference branch)
Expand All @@ -126,14 +126,14 @@ export default class Menu extends Component {
floatingLabelText={autoComplete.label}
onNewRequest={(req, index) => {
const route = index === -1 ? '' : this.state.dataSource[index].data.osm_id;
const updatedStopovers =
const updatedAutoCompletes =
this.state.autoCompletes.map((autoCompleteElem) => {
if (autoCompleteElem.id === autoComplete.id) {
return Object.assign({}, autoCompleteElem, { route });
}
return autoCompleteElem;
});
this.setState({ stopovers: updatedStopovers });
this.setState({ autoCompletes: updatedAutoCompletes });
}}
dataSource={this.state.dataSource}
onUpdateInput={this.handleUpdate}
Expand Down

0 comments on commit 732be0a

Please sign in to comment.