-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
07 : maj geolocation : passage dans searchForm + action creator (Prom…
…ise) et suppression CreateForm plus utile
- Loading branch information
Showing
5 changed files
with
54 additions
and
90 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {change} from 'redux-form'; | ||
export const GEOLOCATION_LOADING = 'GEOLOCATION_LOADING'; | ||
export const GEOLOCATION_COMPLETE = 'GEOLOCATION_COMPLETE'; | ||
|
||
export function geolocate(){ | ||
return function( dispatch, getState ){ | ||
const p = new Promise( resolve => resolve(dispatch( { type: GEOLOCATION_LOADING } ) ) ) | ||
// on met à jour le champ de saisie pour indiquer que la geoloc est en cours | ||
.then(() => dispatch( change( 'search', 'city', 'Recherche position GPS...' ) ) ) | ||
// on lance la geolocalisation | ||
.then(()=> new Promise((resolve, reject) => navigator.geolocation.getCurrentPosition( resolve, reject, {timeout: 20000, enableHighAccuracy:true}) )) | ||
// on met à jour le champ de recherche pour indiquer qu'on a trouvé les coordonnées GPS | ||
.then( position => { | ||
dispatch( change( 'search', 'city', `Recherche ville à ${position.coords.latitude},${position.coords.longitude}...` ) ) | ||
return position; | ||
}) | ||
// on appelle l'API de reverse geocoding | ||
.then( position => fetch( `http://geocode.xyz/${position.coords.latitude},${position.coords.longitude}?json=1`, { credentials: 'include' } ) ) | ||
.then( response => response.json() ) | ||
// une fois les donnnées reçues et parsées | ||
.then( data => { | ||
// on vérifie si l'API a retourné une erreur | ||
if ( data.error ) { | ||
throw new Error(`Erreur de géolocalisation : ${data.error.message}`) | ||
} | ||
// et on met à jour le champ de saisie avec la ville retournée par l'API de reverse geocoding | ||
dispatch( change( 'search', 'city', data.city ) ); | ||
} ) | ||
// en cas d'erreur on affiche le message d'erreur directement dans le champ | ||
// dans un vrai projet il faudrait un mécanisme d'affichage des erreurs plus perfectionné ! | ||
.catch( error => dispatch( change( 'search', 'city', error.message ) ) ) | ||
// quelque soit le résultat on désactive le loading | ||
.finally( () => dispatch( { type: GEOLOCATION_COMPLETE } ) ); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {GEOLOCATION_LOADING, GEOLOCATION_COMPLETE} from '../actions/geolocate'; | ||
|
||
const defaultState = { | ||
geolocationIsLoading: false | ||
} | ||
|
||
export default function( state=defaultState, action ) { | ||
switch ( action.type ){ | ||
case GEOLOCATION_LOADING : | ||
return { geolocationIsLoading: true }; | ||
break; | ||
case GEOLOCATION_COMPLETE : | ||
return { geolocationIsLoading: false }; | ||
break; | ||
} | ||
return state; | ||
} |