solicitudes.js
4.41 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import qs from 'query-string'
import { merge, reject, isNil, tap } from 'ramda'
import { SENATICS_URL } from '../../constants'
import { notify } from '../../lib/notify'
export const SOLICITUDES_REQUESTED = 'SOLICITUDES_REQUESTED'
export const solicitudesRequested = () => ({
type: SOLICITUDES_REQUESTED
})
export const SOLICITUDES_RECEIVED = 'SOLICITUDES_RECEIVED'
export const solicitudesReceived = (solicitudes) => ({
type: SOLICITUDES_RECEIVED,
solicitudes: solicitudes.results,
pages: solicitudes.meta
})
export const SOLICITUDES_FAILED = 'SOLICITUDES_FAILED'
export const solicitudesFailed = (error) => ({
type: SOLICITUDES_FAILED,
error
})
export const fetchSolicitudes = (token, data) => (dispatch) => {
dispatch(solicitudesRequested())
const options = {
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
},
}
const params = merge({
page: 1,
}, reject(isNil, data))
const url = `${SENATICS_URL}/solicitudes?${qs.stringify(params)}`
const internal = () => fetch(url, options)
.then(response => response.text())
.then(tap(text => dispatch({ type: 'TEXT_RECEIVED', text })))
.then(JSON.parse)
.then(json => dispatch(solicitudesReceived(json)))
.catch(error => dispatch(solicitudesFailed(error)))
return internal()
}
export const VER_DETALLE_SOLICITUD = 'VER_DETALLE_SOLICITUD'
export const verDetalleSolicitud = (solicitud) => ({
type: VER_DETALLE_SOLICITUD,
solicitud,
})
export const FLUJOS_REQUESTED = 'FLUJOS_REQUESTED'
export const flujosRequested = () => ({
type: FLUJOS_REQUESTED,
})
export const FLUJOS_RECEIVED = 'FLUJOS_RECEIVED'
export const flujosReceived = (flujos) => ({
type: FLUJOS_RECEIVED,
flujos
})
export const FLUJOS_FAILED = 'FLUJOS_FAILED'
export const flujosFailed = (error) => ({
type: FLUJOS_FAILED,
error
})
export const fetchFlujo = (token, solicitudId) => (
(dispatch) => {
dispatch(flujosRequested())
const options = {
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
},
}
const url = `${SENATICS_URL}/flujos-solicitud/${solicitudId}`
const internal = () => fetch(url, options)
.then(response => response.text())
.then(tap(text => dispatch({ type: 'TEXT_RECEIVED', text })))
.then(JSON.parse)
.then(json => dispatch(flujosReceived(json)))
.catch(error => dispatch(flujosFailed(error)))
return internal()
}
)
export const SOLICITUD_POSTED = 'SOLICITUD_POSTED'
export const solicitudPosted = (solicitud) => ({
type: SOLICITUD_POSTED,
solicitud
})
export const SOLICITUD_POST_FAILED = 'SOLICITUD_POST_FAILED'
export const solicitudPostFailed = () => ({
type: SOLICITUD_POST_FAILED
})
export const SOLICITUD_POST_SUCCESS = 'SOLICITUD_POST_SUCCESS'
export const solicitudPostSuccess = (result) => ({
type: SOLICITUD_POST_SUCCESS,
result
})
// esto es un thunk para mantener consistencia
// como integre redux-observables muy tarde, prefiero mantener
// convencion y tener todos los efectos secundarios en los thunks
export const notifySolicitud = ({ titulo, institucion }) => () =>
notify({
title: `Solicitud ${titulo} creada.`,
body: `Tu solicitud ${titulo} a ${institucion.nombre}, fue creada exitosamente.`
})
export const postSolicitud = (token, solicitud) => (dispatch) => {
dispatch(solicitudPosted(solicitud))
const options = {
method: 'POST',
body: JSON.stringify(solicitud),
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
}
const url = `${SENATICS_URL}/solicitudes`
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(solicitudPostFailed(json.error))
return dispatch(solicitudPostSuccess(json))
})
.catch(error => dispatch(solicitudPostFailed(error)))
return internal()
}
export const CAMBIAR_BUSQUEDA_SOLICITUD = 'CAMBIAR_BUSQUEDA_SOLICITUD'
export const cambiarBusquedaSolicitud = (busqueda) => ({
type: CAMBIAR_BUSQUEDA_SOLICITUD,
busqueda,
})
export const CAMBIAR_PAGINA_SOLICITUD = 'CAMBIAR_PAGINA_SOLICITUD'
export const cambiarPaginaSolicitud = (busqueda) => ({
type: CAMBIAR_PAGINA_SOLICITUD,
busqueda,
})