Instantly share code, notes, and snippets.
Created
January 21, 2018 18:08
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save rodolfopeixoto/9baa308cd101e418d6aa397bbe13691d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import * as routes from '../constants/routes'; | |
import { geolocated } from 'react-geolocated'; | |
import { Link } from 'react-router-dom'; | |
import { db } from './../firebase'; | |
const INITIAL_STATE = { | |
email: '', | |
city: 'camposDosGoytacazes', | |
latitude: null, | |
longitude: null, | |
numberLine: '', | |
itinerary: '', | |
error: null | |
} | |
const byPropKey = (propertyName, value) => () => ({ | |
[propertyName]: value, | |
}); | |
class ShareLocation extends Component { | |
constructor(props){ | |
super(props); | |
this.state = {...INITIAL_STATE} | |
console.log('Constructor()'); | |
} | |
componentDidMount(){ | |
const { | |
email, | |
city, | |
latitude, | |
longitude, | |
numberLine, | |
itinerary | |
} = this.state; | |
const { match: { params } } = this.props; | |
console.log(this.state.latitude); | |
if(this.state.latitude != null && this.state.longitude != null){ | |
this.interval = setInterval(() => { | |
console.log(`Número da Linha: ${this.props.params.numeroDaLinha}`) | |
this.setState({ latitude: this.props.coords.latitude, longitude: this.props.coords.longitude, numberLine: params.numeroDaLinha ,itinerary: params.itinerario }); | |
db.doCreateShareLocation(email, city, latitude, longitude, numberLine, itinerary) | |
.then(() => { | |
this.setState(() => ({ ...INITIAL_STATE })); | |
console.log('Cadastrado ShareLocation'); | |
}) | |
.catch(error => { | |
this.setState(byPropKey('error', error)); | |
}); | |
}, 73000) | |
} | |
} | |
componentWillMount(){ | |
console.log('componentWillMount()'); | |
} | |
componentWillUnmount() { | |
console.log('componentWillUnmount()'); | |
clearInterval(this.interval); | |
} | |
render() { | |
const { | |
email, | |
city, | |
latitude, | |
longitude, | |
numberLine, | |
itinerary, | |
error | |
} = this.state; | |
return !this.props.isGeolocationAvailable | |
? <div className="container"> | |
O seu browser infelizmente não suporta Geolocalização. :( | |
</div> | |
: !this.props.isGeolocationEnabled | |
? <div className="container">Por favor, ative a Geolocalização, pois no momento ela não está habilitada.</div> | |
: this.props.coords | |
? | |
<div className="container"> | |
<p> | |
Olá, muito obrigado por ajudar a aumentar essa corrente do bem! | |
Você está ajudando 0 pessoas. Mas não desligue, pois a qualquer momento as pessoas podem | |
acessar. Nós pegamos sua localização de 1 em 1 minuto para que possamos a cada 1 minuto mostrar | |
a localização. | |
<br/> | |
{ | |
setInterval( () => { | |
this.setState({ latitude: this.props.coords.latitude, longitude: this.props.coords.longitude }); | |
console.log('Passou 1 minuto') | |
}, 63000) | |
} | |
<br /> | |
<b>Latitude:</b> { this.props.coords.latitude } | |
<br /> | |
<b>Longitude:</b>{ this.props.coords.longitude } | |
<br/> | |
<Link to={routes.SEARCH_BUS}><button className="form-control btn btn-primary">Voltar</button></Link> | |
<Link to={routes.SEARCH_BUS}><button className="form-control btn btn-primary" | |
onClick={ () => this.clearIntervalMethod } | |
>PAUSAR</button></Link> | |
</p> | |
</div> | |
: <div>Obtenha os dados da localização.</div>; | |
} | |
} | |
export default geolocated({ | |
positionOptions: { | |
enableHighAccuracy: false, | |
}, | |
userDecisionTimeout: 5000, | |
})(ShareLocation); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment