Guest User

Untitled

a guest
Sep 30th, 2019
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import MapGL, {Marker} from "react-map-gl";
  3. import 'mapbox-gl/dist/mapbox-gl.css'
  4. import './App.scss'
  5. import Dialog from '@material-ui/core/Dialog';
  6. import Button from '@material-ui/core/Button';
  7. import DialogActions from '@material-ui/core/DialogActions';
  8. import DialogContent from '@material-ui/core/DialogContent';
  9. import DialogContentText from '@material-ui/core/DialogContentText';
  10. import { makeStyles } from '@material-ui/core/styles';
  11. import TextField from '@material-ui/core/TextField';
  12. import DialogTitle from '@material-ui/core/DialogTitle';
  13.  
  14. class Map2 extends React.Component {
  15.    
  16.     state = {
  17.         viewport: {
  18.             width: "100vw",
  19.             height: "100vh",
  20.             latitude: 0,
  21.             longitude: 0,
  22.             zoom: 15
  23.         },
  24.         locations: [],
  25.  
  26.         // new review stuff
  27.         newReviewOpen: false,
  28.         locationName: '',
  29.         newReviewLocationId: 0,
  30.         reviews: [],
  31.  
  32.         // new location stuff
  33.         newLocationOpen: false,
  34.         reviewLat: 0,
  35.         reviewLng: 0,
  36.         newName: '',
  37.         newLocationId: 0,
  38.         newReviewBody: ''
  39.     }
  40.  
  41.     componentDidMount(){
  42.         this.getInitialPosition();
  43.         this.fetchLocations();
  44.         this.getMarkers();
  45.     }
  46.  
  47.     getInitialPosition = () => {
  48.         navigator.geolocation.getCurrentPosition(position => {
  49.             let newViewport = {
  50.                 height: "100vh",
  51.                 width: "100vw",
  52.                 latitude: position.coords.latitude,
  53.                 longitude: position.coords.longitude,
  54.                 zoom: 15
  55.             }
  56.             this.setState({
  57.                 viewport: newViewport
  58.             });
  59.         });
  60.     }
  61.  
  62.     handleLocationClick = id => {
  63.         this.setState({newReviewLocationId: id});
  64.         fetch(`http://localhost:3000/reviews/${id}`)
  65.         .then(response => response.json())
  66.         .then(reviews => this.setState({reviews}))
  67.         this.setState({newReviewOpen: true});
  68.     }
  69.    
  70.     fetchLocations = () => {
  71.         fetch(`http://localhost:3000/locations/${this.state.viewport.latitude}/${this.state.viewport.longitude}`)
  72.         .then(resp => resp.json())
  73.         .then(locations => this.setState({locations}))
  74.     }
  75.    
  76.     onViewportChange = viewport => {
  77.         this.setState({viewport});
  78.         this.fetchLocations();
  79.         this.getMarkers();
  80.     }
  81.    
  82.     mapClick = event => {
  83.         this.setState({reviewLat: event.lngLat[1], reviewLng: event.lngLat[0]})
  84.         this.setState({newLocationOpen: true});
  85.     }
  86.    
  87.     handleClose = () => {
  88.         this.setState({newLocationOpen: false});
  89.     }
  90.  
  91.     handleReviewClose = () => {
  92.         this.setState({newReviewOpen: false})
  93.     }
  94.  
  95.     getReviews = () => {
  96.         return this.state.reviews.map(review => {
  97.             return (
  98.                 <li>{review.body}</li>
  99.             );
  100.         })
  101.     }
  102.    
  103.     getMarkers = () => {
  104.         return this.state.locations.map(location => {
  105.             return (
  106.                 <Marker key={location.id} offsetLeft={-25} offsetTop={-70} latitude={parseFloat(location.lat)} longitude={parseFloat(location.lng)}>
  107.                     <div className="marker-styles" onClick={() => {this.handleLocationClick(location.id)}} >
  108.                         <p>{location.reviews}</p>
  109.                     </div>
  110.                 </Marker>
  111.             );
  112.         });
  113.     }
  114.  
  115.     newLocationFormHandler = event => {
  116.         this.setState({[event.target.name]: event.target.value});
  117.     }
  118.  
  119.     newLocationSubmitHandler = event => {
  120.         event.preventDefault();
  121.         fetch("http://localhost:3000/locations", {
  122.             method: "POST",
  123.             headers: {
  124.                 'Content-Type': 'application/json',
  125.                 'Accept': 'application/json'
  126.             },
  127.             body: JSON.stringify({
  128.                 'location_name': this.state.newName,
  129.                 'lat': this.state.reviewLat,
  130.                 'lng': this.state.reviewLng
  131.             })
  132.         })
  133.         .then(res => res.json())
  134.         .then((finalResponse) =>
  135.         fetch("http://localhost:3000/reviews", {
  136.             method: "POST",
  137.             headers: {
  138.                 'Content-Type': 'application/json',
  139.                 'Accept': 'application/json'
  140.             },
  141.             body: JSON.stringify({
  142.                 'body': this.state.newReviewBody,
  143.                 'user_id': 1,
  144.                 'location_id': finalResponse.id
  145.             })
  146.         })
  147.         )
  148.         this.setState({newLocationOpen: false});
  149.         this.fetchLocations();
  150.         this.getMarkers();
  151.     }
  152.    
  153.     render(){
  154.         const TOKEN='pk.eyJ1Ijoiam9obmZhamFyZG8iLCJhIjoiY2swcjVnZmFpMDBwMTNnbWd6ZWRwMG1nMiJ9.VwxkYPyVdPzoLc83t4mlqQ'
  155.         return (
  156.             <div>
  157.                 <MapGL
  158.                     {...this.state.viewport}
  159.                    
  160.                     onClick={this.mapClick}
  161.                     attributionControl={false}
  162.                     onViewportChange={this.onViewportChange}
  163.                     mapboxApiAccessToken={TOKEN}
  164.                     mapStyle="mapbox://styles/mapbox/streets-v10">
  165.                         {this.getMarkers()}                        
  166.                     </MapGL>
  167.  
  168.                         <Dialog
  169.                             open={this.state.newLocationOpen}
  170.                             // keepMounted
  171.                             onClose={this.handleClose}
  172.                             aria-labelledby="alert-dialog-slide-title"
  173.                             aria-describedby="alert-dialog-slide-description">
  174.                             <DialogTitle id="alert-dialog-slide-title">{"Adding a new review!"}</DialogTitle>
  175.                             <DialogContent>
  176.                             <DialogContentText id="alert-dialog-slide-description">
  177.                                 <form onSubmit={this.newLocationSubmitHandler}>
  178.                                 <p><TextField
  179.                                     id="standard-name"
  180.                                     label="Location name"
  181.                                     name="newName"
  182.                                     // className={classes.textField}
  183.                                     value={this.state.newName}
  184.                                     onChange={this.newLocationFormHandler}
  185.                                     margin="normal"
  186.                                 /></p>
  187.                                 <p><TextField
  188.                                     id="standard-review"
  189.                                     label="Your review"
  190.                                     name="newReviewBody"
  191.                                     // className={classes.textField}
  192.                                     value={this.state.newReviewBody}
  193.                                     onChange={this.newLocationFormHandler}
  194.                                     margin="normal"
  195.                                 /></p>
  196.                                 <Button color="primary" type="submit">Ok!</Button>
  197.                                 </form>
  198.                             </DialogContentText>
  199.                             </DialogContent>
  200.                         </Dialog>
  201.  
  202.  
  203.                         <Dialog
  204.                             open={this.state.newReviewOpen}
  205.                             // keepMounted
  206.                             onClose={this.handleReviewClose}
  207.                             aria-labelledby="alert-dialog-slide-title"
  208.                             aria-describedby="alert-dialog-slide-description">
  209.                             <DialogTitle id="alert-dialog-slide-title">{`Adding a new review for location`}</DialogTitle>
  210.                             <DialogContent>
  211.                             <DialogContentText id="alert-dialog-slide-description">
  212.                                 {this.getReviews()}
  213.                                 <form onSubmit={this.newLocationSubmitHandler}>
  214.                                 <p><TextField
  215.                                     id="standard-review"
  216.                                     label="Your review"
  217.                                     name="newReviewBody"
  218.                                     // className={classes.textField}
  219.                                     value={this.state.newReviewBody}
  220.                                     onChange={this.newLocationFormHandler}
  221.                                     margin="normal"
  222.                                 /></p>
  223.                                 <Button color="primary" type="submit">Ok!</Button>
  224.                                 </form>
  225.                             </DialogContentText>
  226.                             </DialogContent>
  227.                         </Dialog>
  228.             </div>
  229.         )
  230.     }
  231. }
  232.  
  233. export default Map2;
Advertisement
Add Comment
Please, Sign In to add comment