location.js 1.72 KB
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)))
}