import React, { Component } from 'react'; import logo from './logo.svg'; import enter from './images/enter.png'; import './App.css'; import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react'; import axios from 'axios'; import Geocode from "react-geocode"; Geocode.setApiKey("AIzaSyBLYp4CH1DV_xZHvxtQ5GjaQU4A6gaUzSE"); // centering in the US const initialState = { name: '', lat: 39.8283, lng: -98.57, error: false } class App extends Component { constructor(props) { super(props); this.state = initialState; } // Sets the state as user is typing in the name of the physician updateName = (e) => { var newState = this.state; newState['name'] = e.target.value; this.setState(newState); } // Connection to the separated backend server => DB // endpoint: https://physiciansearch.herokuapp.com/ // local: http://localhost:3000 search = (e) => { // get request to backend REST API axios.get(`https://physiciansearch.herokuapp.com/physicianloc`, { params: { name: this.state.name } }) .then(res => { // received data const physician = res.data; console.log(physician); const address = physician.address + ', ' + physician.city + ' ' + physician.state + ' ' + physician.zip_code.toString(); // Convert address to lat and lng for our frontend map API // Uses Google's API Geocode to convert Geocode.fromAddress(address).then( response => { const { lat, lng } = response.results[0].geometry.location; console.log(lat, lng); this.setState({ lat: lat, lng: lng, error: false }); }, error => { console.error(error); } ); }, err => { console.log('Could not find physician'); // make the error box appear this.setState({ error: true }); }); } render() { const {name, lat, lng} = this.state; return (
{name.length === 0 ? null : }
{lat === initialState.lat ? null : }
{this.state.error ?
Could not find physician!
: null }
); } } export default GoogleApiWrapper({ apiKey: ('AIzaSyBLYp4CH1DV_xZHvxtQ5GjaQU4A6gaUzSE') })(App);