-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
254 lines (238 loc) · 6.33 KB
/
App.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import React, { useEffect, useState } from 'react';
import { Alert,
Image,
Modal,
Pressable,
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
View
} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import ControlPresupuesto from './src/components/ControlPresupuesto';
import Filtro from './src/components/Filtro';
import FormularioGasto from './src/components/FormularioGasto';
import Header from './src/components/Header';
import ListadoGastos from './src/components/ListadoGastos';
import NuevoPresupuesto from './src/components/NuevoPresupuesto';
import { generarId } from './src/helpers';
const App = () => {
const [ isValidPresupuesto, setIsValidPresupuesto ] = useState(false)
const [ presupuesto, setPresupuesto ] = useState(0)
const [ gastos, setGastos ] = useState([])
const [ modal, setModal ] = useState(false)
const [ gasto, setGasto ] = useState({})
const [ filtro, setFiltro ] = useState('')
const [ gastosFiltrados, setGastosFiltrados ] = useState([])
//Get Presupuesto
useEffect(() => {
const obtenerPresupuestoStorage = async () => {
try {
const presupuestoStorage = await AsyncStorage.getItem('planificador_presupuesto') ?? 0
console.log(presupuestoStorage)
if(presupuestoStorage>0){
setPresupuesto(presupuestoStorage)
setIsValidPresupuesto(true)
}
} catch (error) {
console.log(error)
}
}
obtenerPresupuestoStorage()
},[])
//get gastos , obtener
useEffect(()=> {
const obtenerGastosStorage = async () =>{
try {
const gastosStorage = await AsyncStorage.getItem('planificador_gastos') ?? []
setGastos( gastosStorage ? JSON.parse(gastosStorage) : [])
} catch (error) {
console.log(error)
}
}
obtenerGastosStorage()
},[])
//set gastos, setear
useEffect(() => {
const guardarGastosStorage = async () =>{
try {
await AsyncStorage.setItem('planificador_gastos', JSON.stringify(gastos))
} catch (error) {
console.log(error)
}
}
guardarGastosStorage()
}, [ gastos ])
//guardarPresupuesto
useEffect(() => {
if(isValidPresupuesto){
const guardarPresupuestoStorage = async () => {
try {
await AsyncStorage.setItem('planificador_presupuesto', presupuesto)
} catch (error) {
console.log(error)
}
}
guardarPresupuestoStorage()
}
},[ isValidPresupuesto ])
const handleNuevoPresupuesto = (presupuesto) =>{
if(Number(presupuesto) > 0 ){
setIsValidPresupuesto(true)
}else{
Alert.alert(
"Error",
"Presupuesto no puede ser 0 o menor",
[
{ text: "OK" }
]
);
}
}
const handleGasto = (gasto) => {
if([gasto.nombre, gasto.categoria, gasto.cantidad].includes('')){
Alert.alert(
"Error",
"Todos los campos son obligatorios"
);
return
}
if(gasto.id){
const gastosActualizados = gastos.map( gastoState => gastoState.id === gasto.id ? gasto : gastoState)
setGastos(gastosActualizados)
}else{
gasto.id = generarId()
gasto.fecha = Date.now()
setGastos([...gastos, gasto])
}
setModal(!modal)
}
const eliminarGasto = (id) =>{
Alert.alert(
"¿Deseas eliminar este registro?",
"Un gasto eliminado no puede restaurarse",
[
{ text: 'No', style: 'cancel'},
{ text: 'Si, Eliminar', onPress: () => {
const gastosActualizados = gastos.filter( gastoState => gastoState.id !== id )
setGastos(gastosActualizados)
setModal(!modal)
setGasto({})
}}
]
);
}
const resetearApp = () =>{
Alert.alert(
'Deseas eliminar todos los datos?',
'Esto eliminara presupuesto y gastos',
[
{ text: 'No', style: 'cancel'},
{ text: 'Si, Eliminar', onPress: async () => {
try {
await AsyncStorage.clear()
setIsValidPresupuesto(false)
setPresupuesto(0)
setGastos([])
} catch (error) {
console.log(error)
}
}
}
]
)
}
return (
<View>
<ScrollView>
<View style={styles.header}>
<Header/>
{/**pantalla ingreso presupuesto o mostrar principal */}
{isValidPresupuesto ? (
<ControlPresupuesto
gastos={gastos}
presupuesto={presupuesto}
resetearApp={resetearApp}
/>
) : (
<NuevoPresupuesto
presupuesto={presupuesto}
setPresupuesto={setPresupuesto}
handleNuevoPresupuesto = { handleNuevoPresupuesto }
/>
)}
</View>
{isValidPresupuesto &&
<>
<Filtro
setFiltro={setFiltro}
filtro={filtro}
gastos={gastos}
setGastosFiltrados={setGastosFiltrados}
/>
<ListadoGastos
gastos={gastos}
setModal={setModal}
setGasto={setGasto}
filtro={filtro}
gastosFiltrados={gastosFiltrados}
/>
</>
}
</ScrollView>
{modal && (
<Modal
animationType='slide'
visible={modal}
onRequestClose={()=>{
setModal(!modal)
}}
>
<FormularioGasto
setModal={setModal}
handleGasto={handleGasto}
setGasto={setGasto}
gasto={gasto}
eliminarGasto={eliminarGasto}
/>
</Modal>
)}
{/**Boton con imagen para agregar gasto */}
{isValidPresupuesto && (
<Pressable
style={styles.pressable}
onPress={() => setModal(!modal)}
>
<Image
style={styles.imagen}
source={ require('./src/img/nuevo-gasto.png')}
/>
</Pressable>
)}
</View>
);
};
const styles = StyleSheet.create({
contenedor:{
backgroundColor: '#F5F5F5',
flex: 1
},
header:{
backgroundColor: '#3B82F6',
minHeight: 400
},
pressable: {
width: 60,
height: 60,
position: 'absolute',
bottom: 40,
right: 30
},
imagen: {
width: 60,
height: 60
}
});
export default App;