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
|
import {
getCamionAPI,
insertarCamionAPI,
editarCamionAPI,
eliminarCamionAPI
} from 'ba-api/camion';
import {
getTipoCamionXNombreAPI
} from 'ba-api/tipoCamion';
import * as types from './actionTypes'
export const setCamiones = (payload) => ({
type: types.LISTA_PEDIDO,
payload
});
export const insertCamiones = (payload) => ({
type: types.NUEVO_CAMION,
payload
});
export const updateCamiones = (payload) => ({
type: types.ACTUALIZAR_CAMION,
payload
});
export const deleteCamiones = (payload) => ({
type: types.ELIMINAR_CAMION,
payload
});
/***************************************/
export function b2fCamiones(x){
const {...other} = x
return {
id: x.id,
codigo: 'A001',
placa: x.placa,
origen: 'Lima',
tipo: x.tipoCamion.nombre,
estado: x.estado,
capacidad: x.tipoCamion.capacidad,
...other
}
}
export function f2bCamion(x, tipo){
console.log("Antes", tipo)
console.log("data new", x.get('id'))
const data = {
id: x.get('id')? x.get('id'): undefined,
placa: x.get('placa'),
codigo: "A0005",
estado: x.get('estado'),
kilometraje: 50.0,
tipoCamion: {
id: tipo.id,
nombre: tipo.nombre,
capacidad: tipo.capacidad
}
}
return data
}
/***************************************/
export const getCamiones = () => async dispatch => {
try{
const res = await getCamionAPI();
res.data.forEach((x, i) => {
res.data[i] = b2fCamiones(x)
});
dispatch(setCamiones({res}))
return res;
}catch(e){
console.log(e)
}
}
export const insertarCamion = (data) => async dispatch => {
try{
const resTipo = await getTipoCamionXNombreAPI(data.get('tipo'));
const res = await insertarCamionAPI(f2bCamion(data, resTipo.data));
dispatch(insertCamiones({res}))
return res;
}catch(e){
console.log(e)
}
}
export const editarCamion = (data) => async dispatch => {
try{
const resTipo = await getTipoCamionXNombreAPI(data.get('tipo'));
const res = await editarCamionAPI(f2bCamion(data, resTipo.data));
dispatch(updateCamiones({res}))
return res;
}catch(e){
console.log(e)
}
}
export const eliminarCamion = (data) => async dispatch => {
try{
//Envia id
const res = await eliminarCamionAPI(data);
dispatch(deleteCamiones({res}))
return res;
}catch(e){
console.log(e)
}
}
|