location.js 3.31 KB
import EC from 'elliptic'
import { Permissions, Location } from 'expo'
import { tap } from 'ramda'
import { Buffer } from 'buffer/'
import { ownPrivate } from '../../constants/keys'
import { BACKEND_URL } from '../../constants'

const ec = new EC.ec('secp256k1')
const clientPriv = ec.keyFromPrivate(ownPrivate)

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 = (solicitud) => ({
  type: LOCATION_PERMISSION_GRANTED,
  solicitud
})

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 = (solicitud) => (dispatch) => {
  dispatch(locationPermissionRequested())
  return Permissions.askAsync(Permissions.LOCATION)
    .then(({ status }) => {
      if (status === 'granted') {
        return dispatch(locationPermissionGranted(solicitud))
      }
      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, solicitud }) => ({
  type: LOCATION_RECEIVED,
  location,
  solicitud
})

export const LOCATION_FAILED = 'LOCATION_FAILED'
export const locationFailed = (error) => ({
  type: LOCATION_FAILED,
  error
})

export const requestLocation = (solicitud) => (dispatch) => {
  dispatch(locationRequested())
  Location.getCurrentPositionAsync({})
    .then(location => dispatch(locationReceived({ location, solicitud })))
    .catch(error => dispatch(locationFailed(error)))
}


export const LOCATION_POSTED = 'LOCATION_POSTED'
export const locationPosted = () => ({
  type: LOCATION_POSTED
})
export const LOCATION_POST_FAILED = 'LOCATION_POST_FAILED'
export const locationPostFailed = (error) => ({
  type: LOCATION_POST_FAILED,
  error
})
export const LOCATION_POST_SUCCESS = 'LOCATION_POST_SUCCESS'
export const locationPostSuccess = (result) => ({
  type: LOCATION_POST_SUCCESS,
  result
})


export const postLocation = (location, solicitud) => (dispatch) => {
  dispatch(locationPosted())
  const message = JSON.stringify({ location, solicitud })
  const firma = new Buffer(clientPriv.sign(message).toDER()).toString('hex')
  const options = {
    method: 'POST',
    body: message,
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
  }

  const url = `${BACKEND_URL}/solicitudes/ubicacion/${firma}`

  const internal = () => fetch(url, options)
    .then(response => response.text())
    .then(tap(text => dispatch({ type: 'TEXT_RECEIVED', text })))
    .then(JSON.parse)
    .then(json => {
      if (json.error) return dispatch(locationPostFailed(json.error))
      return dispatch(locationPostSuccess(json))
    })
    .catch(error => dispatch(locationPostFailed(error)))
  return internal()
}