location.js
3.31 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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()
}