Added button for getting user position

This commit is contained in:
dbroqua 2020-03-03 10:10:00 +01:00
parent 658f9aebb7
commit 0faf2359de
5 changed files with 74 additions and 5 deletions

BIN
public/gps.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 649 B

View File

@ -16,7 +16,8 @@ class Application extends React.Component {
this.state = { this.state = {
showModal: false, showModal: false,
selectedGasStation: {}, selectedGasStation: {},
selectedGasType: 'E85' selectedGasType: 'E85',
needUpdateUserLocation: false
}; };
} }
@ -68,6 +69,15 @@ class Application extends React.Component {
})); }));
} }
/**
* Méthode permettant de demander à l'app de géoloc ou non l'utilisateur
*/
setNeedUpdateUserLocation = (needUpdateUserLocation) => {
this.setState({
needUpdateUserLocation
})
}
/** /**
* Méthode gérant le rendu de la vue * Méthode gérant le rendu de la vue
*/ */
@ -76,6 +86,8 @@ class Application extends React.Component {
selectedGasType, selectedGasType,
showModal, showModal,
selectedGasStation, selectedGasStation,
userLocation,
needUpdateUserLocation,
} = this.state; } = this.state;
return ( return (
@ -87,8 +99,8 @@ class Application extends React.Component {
selectedGasStation={selectedGasStation} selectedGasStation={selectedGasStation}
selectedGasType={selectedGasType} selectedGasType={selectedGasType}
/> />
<Map selectedGasType={selectedGasType} showGasStation={this.showGasStation} /> <Map selectedGasType={selectedGasType} userLocation={userLocation} needUpdateUserLocation={needUpdateUserLocation} setNeedUpdateUserLocation={this.setNeedUpdateUserLocation} showGasStation={this.showGasStation} />
<Footer selectedGasType={selectedGasType} selectGasType={this.selectGasType} /> <Footer selectedGasType={selectedGasType} selectGasType={this.selectGasType} setNeedUpdateUserLocation={this.setNeedUpdateUserLocation} />
</ToastProvider> </ToastProvider>
); );
} }

View File

@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { Navbar } from "react-bootstrap"; import { Navbar, Button } from "react-bootstrap";
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import GasTypes from "./GasTypes"; import GasTypes from "./GasTypes";
@ -8,11 +8,20 @@ const Footer = (props) => {
const { const {
selectGasType, selectGasType,
selectedGasType, selectedGasType,
setNeedUpdateUserLocation,
} = props; } = props;
return ( return (
<Navbar bg="light" variant="light" fixed="bottom"> <Navbar bg="light" variant="light" fixed="bottom">
<GasTypes selectedGasType={selectedGasType} selectGasType={selectGasType} /> <GasTypes selectedGasType={selectedGasType} selectGasType={selectGasType} />
<Button
variant="link"
onClick={() => setNeedUpdateUserLocation(true)}
onFocus={() => { }}
onBlur={() => { }}
>
<img src="/gps.png" alt="Géolocalisez moi" />
</Button>
</Navbar> </Navbar>
); );
}; };
@ -20,6 +29,7 @@ const Footer = (props) => {
Footer.propTypes = { Footer.propTypes = {
selectedGasType: PropTypes.string.isRequired, selectedGasType: PropTypes.string.isRequired,
selectGasType: PropTypes.func.isRequired, selectGasType: PropTypes.func.isRequired,
setNeedUpdateUserLocation: PropTypes.func.isRequired,
}; };
export default Footer; export default Footer;

View File

@ -6,6 +6,11 @@ import { gasTypes } from "../config";
import { capitalizeFirstLetter} from "../helpers"; import { capitalizeFirstLetter} from "../helpers";
class GasStation extends React.Component { class GasStation extends React.Component {
/**
* Méthode permettant de convertir un type de carburant en un nom lisible
* @param {string} gasType
* @return {mixed}
*/
renderGasType = (gasType) => { renderGasType = (gasType) => {
for( let i = 0 ; i < gasTypes.length ; i +=1 ) { for( let i = 0 ; i < gasTypes.length ; i +=1 ) {
if ( gasTypes[i].type === gasType) { if ( gasTypes[i].type === gasType) {
@ -16,6 +21,10 @@ class GasStation extends React.Component {
return (null); return (null);
} }
/**
* Méthode permettant de générer la liste des prix
* return {string}
*/
renderPrices = () => { renderPrices = () => {
const { const {
selectedGasType, selectedGasType,

View File

@ -35,10 +35,31 @@ class Map extends React.Component {
this.loadGasStations(); this.loadGasStations();
} }
/**
* Méthode appelée au premier chargement du module
*/
componentDidMount() { componentDidMount() {
this.setUserLocation(); this.setUserLocation();
} }
/**
* Méthode appelée à chaque modificiation de l'état
* @param {Object} prevProps
*/
componentDidUpdate( prevProps ) {
const {
setNeedUpdateUserLocation,
} = this.props;
// L'utilisateur souhaite que l'app le géoloc
if ( prevProps.needUpdateUserLocation ) {
setNeedUpdateUserLocation(false);
this.setUserLocation(() => {
this.loadGasStations();
});
}
}
/** /**
* Méthode permettant de mettre à jour la position de la map * Méthode permettant de mettre à jour la position de la map
* @param {Object} viewport * @param {Object} viewport
@ -51,8 +72,9 @@ class Map extends React.Component {
/** /**
* Méthode permettant de sauvegarder la position de l'utilisateur * Méthode permettant de sauvegarder la position de l'utilisateur
* @param {Function} [cb]
*/ */
setUserLocation() { setUserLocation(cb) {
navigator.geolocation.getCurrentPosition((position) => { navigator.geolocation.getCurrentPosition((position) => {
const setUserLocation = { const setUserLocation = {
latitude: position.coords.latitude, latitude: position.coords.latitude,
@ -68,10 +90,20 @@ class Map extends React.Component {
this.setState({ this.setState({
viewport: newViewport, viewport: newViewport,
userLocation: setUserLocation, userLocation: setUserLocation,
}, () => {
if ( cb ) {
cb();
}
}); });
}); });
} }
/**
* Méthode récursive permettant de charger tous les stations d'une zone
* @param {Number} start
* @param {Number} newLoadId
* @param {Function} callback
*/
loadAllGasStations(start, newLoadId, callback) { loadAllGasStations(start, newLoadId, callback) {
const limit = 20; const limit = 20;
@ -214,9 +246,15 @@ class Map extends React.Component {
} }
} }
Map.defaultProps = {
needUpdateUserLocation: false
};
Map.propTypes = { Map.propTypes = {
needUpdateUserLocation: PropTypes.bool,
selectedGasType: PropTypes.string.isRequired, selectedGasType: PropTypes.string.isRequired,
showGasStation: PropTypes.func.isRequired, showGasStation: PropTypes.func.isRequired,
setNeedUpdateUserLocation: PropTypes.func.isRequired,
toastManager: PropTypes.shape({ toastManager: PropTypes.shape({
add: PropTypes.func, add: PropTypes.func,
removeAll: PropTypes.func, removeAll: PropTypes.func,