‘Travel Plan’ is a simple web app I implemented by following the tutorial on React official website. It uses basic React functions and Google Map Javascript APIs.
The app has a large input field for users to type in their next travel stops with autocomplete support from Google AutocompleteService API. And it has a list of stops with distances and durations between them. The distance and duration data come from Google DistanceMatrixService API.
PlaceForm
PlaceForm component takes one onPlaceSelect property and has three state attributes (keywords, predictions, error). ‘keywords’ stores current user input. ‘predictions’ stores all places returned from Google AutocompleteService. ‘error’ keeps error from the Google API. We use bind(this) for both callback functions so that we can set state within the callback. And we use bind(this, prediction.place_id, prediction.description) to pass place info to handlePlaceSelect. The callback function is defined as function(place_id, description, e). Notice that place info parameters should be added before the default event parameter.
PlaceBox
handlePlaceSelect receives place info from PlaceForm. It uses find function to check if the place has been added. If not, find will return undefined and we add the place info to our place list. If there is a previous stop, we pass both places to calculateDistanceMatrix to retrieve distance and duration info.
PlaceList
Instead of adding each property separately for Place node, we use spread attribute feature {…place}.
Place
We use some fancy icons from FontAwesome.
Render
We add ‘Powered by Google’ at the end of the page to acknowledge Google’s contribution to this app:P