import React, { useContext, useState } from "react"; import Cookies from 'universal-cookie'; import { Text, Link, Center, Heading, VStack, Box, FormControl, Button, Image, ErrorMessage } from "native-base"; import logo from "../assets/logo-katoikia.png"; import { Entypo } from '@expo/vector-icons'; import { MaterialCommunityIcons } from '@expo/vector-icons'; import { View, TextInput, StyleSheet } from "react-native"; import { UserContext } from "../context/UserContext"; import { API } from "../environment/api"; const baseURL = `${API.BASE_URL}/user/loginUser`; export default function LogIn({ navigation }) { const { addUser } = useContext(UserContext); const [errors, setErrors] = useState({}); const [credentials, setCredentials] = useState({ email: "lalo@lalo.com", password: "12345" }); const onHandleChange = (name) => (value) => setCredentials(prev => ({ ...prev, [name]: value })) const validate = async() => { if( credentials.email === "" && credentials.password === ""){ setErrors({ ...errors, email: 'Debe ingresar un correo electrónico', password: 'Debe ingresar una contraseña' }); return false; }else if (credentials.password === "") { setErrors({ ...errors, password: 'Debe ingresar una contraseña' }); return false; } else if(credentials.email === ""){ setErrors({ ...errors, email: 'Debe ingresar un correo electrónico' }); return false; } return true; } const iniciarSesion = async () => { const error = await validate(); console.log(error); if (error) { try { await fetch(baseURL, { cache: 'no-cache', method: 'POST', body: JSON.stringify(credentials), headers: { 'Content-Type': 'application/json' } }) .then(response => { if (response.status != 201){ console.log('ocurrio un error '); }else{ return response.json(); } }) .then( response => { // inqulino 4 y guarda 3 const user = response.message if(user !== null){ if(user.user_type == '4'){ addUser(user); navigation.navigate('Comunicados', {user}) }else if(user.user_type == '3'){ addUser(user); // cambiar por ComunicadosGuarda luego navigation.navigate('Comunicados', {user}) } }else{ setErrors({ ...errors, user: 'Debe ingresar credenciales válidos' }); } }) } catch (error) { console.log("ERROR: " +error); } } console.log(errors); } return (
Katoikia logo
Bienvenido a Katoikia Su app de comunidad de confianza Correo Electrónico {'email' in errors && Debe ingresar un correo electrónico } Contraseña {'password' in errors && Debe ingresar una contraseña } {/* {'user' in errors && Debe ingresar credenciales válidos } */}
); } const styles = StyleSheet.create({ input: { height: 40, margin: 10, borderWidth: 0.5, padding: 5, flex: 1, paddingTop: 10, paddingRight: 10, paddingLeft: 0, marginTop: 50, borderRadius: 4 }, errorMessage: { height: 40, margin: 10, borderWidth: 0.5, padding: 5, flex: 1, paddingTop: 10, paddingRight: 10, paddingLeft: 0, marginTop: 50, borderRadius: 4, borderColor: '#be123c' }, iconStyle: { paddingBottom: 20, marginTop: 3, paddingTop: 35 }, viewSection: { flex: 1, flexDirection: 'row', justifyContent: 'center', alignItems: 'center', marginBottom: 50 }, container: { marginBottom: 6 } })