-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
128 lines (107 loc) · 3.33 KB
/
App.js
File metadata and controls
128 lines (107 loc) · 3.33 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
import React, { useState, useEffect } from 'react';
import { Text, StyleSheet, View, FlatList, TouchableHighlight, TouchableWithoutFeedback, Keyboard, Platform } from 'react-native';
import Cita from './componentes/Cita';
import Formulario from './componentes/Formulario';
import AsyncStorage from '@react-native-community/async-storage';
const App = () => {
const [citas, setCitas] = useState([]);
const [mostrarform, guardarMostrarForm] = useState(false);
useEffect(() => {
const obtenerCitasStorage = async () => {
try {
const citasStorage = await AsyncStorage.getItem('citas');
if(citasStorage) {
setCitas(JSON.parse(citasStorage))
}
} catch (error) {
console.log(error);
}
}
obtenerCitasStorage();
}, []);
const eliminarPaciente = id => {
const citasFiltradas = citas.filter( cita => cita.id !== id );
setCitas( citasFiltradas );
guardarCitasStorage(JSON.stringify(citasFiltradas));
}
const mostrarFormulario = () => {
guardarMostrarForm(!mostrarform);
}
const cerrarTeclado = () => {
Keyboard.dismiss();
}
const guardarCitasStorage = async (citasJSON) => {
try {
await AsyncStorage.setItem('citas', citasJSON);
} catch (error) {
console.log(error);
}
}
return (
<TouchableWithoutFeedback onPress={() => cerrarTeclado() }>
<View style={styles.contenedor}>
<Text style={styles.titulo}>Administrador de Citas</Text>
<View>
<TouchableHighlight onPress={ () => mostrarFormulario() } style={styles.btnMostrarForm}>
<Text style={styles.textoMostrarForm}> {mostrarform ? 'Cancelar Crear Cita' : 'Crear Nueva Cita'} </Text>
</TouchableHighlight>
</View>
<View style={styles.contenido}>
{ mostrarform ? (
<>
<Text style={styles.titulo}>Crear Nueva Cita</Text>
<Formulario
citas={citas}
setCitas={setCitas}
guardarMostrarForm={guardarMostrarForm}
guardarCitasStorage={guardarCitasStorage}
/>
</>
) : (
<>
<Text style={styles.titulo}> {citas.length > 0 ? 'Administra tus citas' : 'No hay citas, agrega una'} </Text>
<FlatList
style={styles.listado}
data={citas}
renderItem={ ({item}) => <Cita item={item} eliminarPaciente={eliminarPaciente} /> }
keyExtractor={ cita => cita.id}
/>
</>
) }
</View>
</View>
</TouchableWithoutFeedback>
);
};
const styles = StyleSheet.create({
contenedor: {
backgroundColor: '#AA076B',
flex: 1
},
titulo: {
color: '#FFF',
marginTop: Platform.OS === 'ios' ? 40 : 20 ,
marginBottom: 20,
fontSize: 24,
fontWeight: 'bold',
textAlign: 'center'
},
contenido: {
flex: 1,
marginHorizontal: '2.5%',
},
listado: {
flex: 1,
},
btnMostrarForm: {
padding: 10,
backgroundColor:'#7d024e',
marginVertical: 10
},
textoMostrarForm: {
color: '#FFF',
fontWeight: 'bold',
textAlign: 'center'
}
});
export default App;