askerovlab

Untitled

May 19th, 2016
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. INDEX JS FILE
  3. */
  4.  
  5. import React, { Component, PropTypes } from 'react';
  6. import ReactDOM from 'react-dom';
  7.  
  8. import SearchBar from './search-bar';
  9. import GoogleMapComponent from './google-map';
  10.  
  11. class App extends Component {
  12.   render() {
  13.   return (
  14.       <div>
  15.         <SearchBar />
  16.         <GoogleMapComponent />
  17.       </div>
  18.     );
  19.   }
  20. }
  21.  
  22. ReactDOM.render(<App />, document.getElementById('app'));
  23.  
  24. /*
  25. SEARCHBAR JS
  26. */
  27.  
  28. import React, { Component } from 'react';
  29.  
  30. import fetchData from './fetch-data';
  31.  
  32. import EventEmitter from './EventEmitter';
  33.  
  34. class SearchBar extends Component {
  35.   constructor() {
  36.     super();
  37.     this.state = {search: ''};
  38.     this.changeHandle = this.changeHandle.bind(this);
  39.     this.submitHandle = this.submitHandle.bind(this);
  40.   }
  41.  
  42.   changeHandle(event) {
  43.     this.setState({search: event.target.value});
  44.   }
  45.  
  46.   submitHandle(event) {
  47.     event.preventDefault();
  48.  
  49.     fetchData(this.state.search).then((response) => {
  50.       const locationData = {
  51.         lat: response.data.results[0].geometry.location.lat,
  52.         lng: response.data.results[0].geometry.location.lng
  53.       };
  54.       EventEmitter.dispatch('locationChanged', locationData);
  55.     });
  56.   }
  57.  
  58.   render() {
  59.     return (
  60.       <form onSubmit={this.submitHandle} className="input-group">
  61.         <input
  62.           placeholder="Type the name of a city or a country"
  63.           className="form-control"
  64.           value={this.state.search}
  65.           onChange={this.changeHandle}
  66.           />
  67.         <span className="input-group-btn">
  68.           <button type="submit" className="btn btn-secondary">Submit</button>
  69.         </span>
  70.       </form>
  71.     );
  72.   }
  73.  
  74. }
  75.  
  76. export default SearchBar;
  77.  
  78. /*
  79. GOOGLE-MAP JS
  80. */
  81. import React, {Component} from 'react';
  82. import {GoogleMapLoader, GoogleMap} from "react-google-maps";
  83. import EventEmitter from './EventEmitter';
  84.  
  85.  
  86. class GoogleMapComponent extends Component {
  87.   constructor() {
  88.     super();
  89.     this.state = {
  90.       loc: {lat: 50, lng: 40}
  91.     }
  92.   }
  93.  
  94.   render() {
  95.    
  96.     EventEmitter.subscribe('locationChanged', (data) => {
  97.       //this.setState({loc: data});
  98.       console.log(this.state.loc);
  99.     });
  100.  
  101.     return (
  102.       <GoogleMapLoader
  103.         containerElement = { <div style={{ height: '60%' }} /> }
  104.         googleMapElement = {
  105.           <GoogleMap defaultZoom = {12} defaultCenter = {this.state.loc} />
  106.         }
  107.       />
  108.   );
  109.  
  110.   };
  111. };
  112.  
  113.  
  114. export default GoogleMapComponent;
  115.  
  116. /*
  117. EVENT EMITTER
  118. */
  119.  
  120. const EventEmitter = {
  121.     _events: {},
  122.     dispatch: function (event, data) {
  123.         if (!this._events[event]) return; // no one is listening to this event
  124.         for (var i = 0; i < this._events[event].length; i++)
  125.             this._events[event][i](data);
  126.     },
  127.     subscribe: function (event, callback) {
  128.       if (!this._events[event]) this._events[event] = []; // new event
  129.       this._events[event].push(callback);
  130.     }
  131. }
  132.  
  133. export default EventEmitter;
Advertisement
Add Comment
Please, Sign In to add comment