location.js
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Permissions, Location } from 'expo'
export const LOCATION_PERMISSION_REQUESTED = 'LOCATION_PERMISSION_REQUESTED'
export const locationPermissionRequested = () => ({
type: LOCATION_PERMISSION_REQUESTED
})
export const LOCATION_PERMISSION_GRANTED = 'LOCATION_PERMISSION_GRANTED'
export const locationPermissionGranted = () => ({
type: LOCATION_PERMISSION_GRANTED
})
export const LOCATION_PERMISSION_DENIED = 'LOCATION_PERMISSION_DENIED'
export const locationPermissionDenied = () => ({
type: LOCATION_PERMISSION_DENIED
})
export const LOCATION_PERMISSION_FAILED = 'LOCATION_PERMISSION_FAILED'
export const locationPermissionFailed = (error) => ({
type: LOCATION_PERMISSION_FAILED,
error
})
export const requestLocationPermission = () => (dispatch) => {
dispatch(locationPermissionRequested())
return Permissions.askAsync(Permissions.LOCATION)
.then(({ status }) => {
if (status === 'granted') {
return dispatch(locationPermissionGranted())
}
return dispatch(locationPermissionDenied())
})
.catch(error => dispatch(locationPermissionFailed(error)))
}
export const LOCATION_REQUESTED = 'LOCATION_REQUESTED'
export const locationRequested = () => ({
type: LOCATION_REQUESTED
})
export const LOCATION_RECEIVED = 'LOCATION_RECEIVED'
export const locationReceived = (location) => ({
type: LOCATION_RECEIVED,
location
})
export const LOCATION_FAILED = 'LOCATION_FAILED'
export const locationFailed = (error) => ({
type: LOCATION_FAILED,
error
})
export const requestLocation = () => (dispatch) => {
dispatch(locationRequested())
Location.getCurrentPositionAsync({})
.then(location => dispatch(locationReceived(location)))
.catch(error => dispatch(locationFailed(error)))
}