import React, { useContext, useState } from "react"; import { API } from "../environment/api"; import { Box, Button, FormControl, Heading, ScrollView, VStack } from "native-base"; import { StyleSheet, TextInput} from "react-native"; import { UserContext } from "../context/UserContext"; import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs'; import { stringMd5 } from 'react-native-quick-md5'; import { TouchableOpacity } from "react-native-gesture-handler"; const { Navigator, Screen } = createMaterialTopTabNavigator(); export default function Profile({ navigation }) { const baseURL = `${API.BASE_URL}/user/updateUser/` const userData = useContext(UserContext) const [name, setName] = useState(userData.user.name); const [apellido, setApellido] =useState(userData.user.last_name); const [email, setEmail] = useState(userData.user.email); const [password, setPassword] = useState(); const [confirmPassword, setConfirmPassword] = useState() const id = userData.user._id; const decode = userData.Password; const [error, setError] = useState({}) const [disable, setDisable] = useState(false) console.log(userData.user); const [info, setInfo] = useState({ _id: userData.user._id, dni: userData.user.dni, name: "", last_name: "", email: "", community_id: userData.user.community_id, }); const [infoPassword, setInfoPassword] = useState({ _id: userData.user._id, dni: userData.user.dni, password: "" }); const onHandleChange = (name) => (value) => setInfo(prev => ({...prev, [name]: value})) const onHandleChangePassword = (value) => { //console.log(value); const dpassword = stringMd5(value) console.log(dpassword); console.log(userData.user.password); if (userData.user.password == dpassword) { console.log(true); setError({}); }else{ console.log(false); setError({ ...error, password: 'La contraseña no coincide con la actual' }); } } const ProfileView = () => ( Bienvenido(a) {userData.user.name} Modifique sus datos DNI {/* Teléfono */} Nombre Apellido Correo electrónico ) const PasswordView = () => ( Bienvenido(a) {userData.user.name} Modifique sus contraseña Contraseña actual onHandleChangePassword(value) } placeholder='Actual contraseña'/> Nueva Contraseña setPassword(value) } placeholder='Nueva contraseña' /> Confirmar nueva contraseña setConfirmPassword(value) } placeholder='Confirmar nueva contraseña'/> {/* {'password' in error && La contraseña no coincide con la actual } */} ) const updatePassword = async() =>{ const dataPassword = { "_id": userData.user._id, "dni": userData.user.dni, "name": userData.user.name, "last_name": userData.user.last_name, "email": userData.user.email, "phone": userData.user.phone, "password": password, "user_type": userData.user.user_type, "status": userData.user.status, "date_entry": userData.user.date_entry, "community_id": userData.user.community_id, } try { await fetch(baseURL+`${id}`, { cache: 'no-cache', method: 'PUT', body: JSON.stringify(dataPassword), headers: { 'Content-Type': 'application/json' } }) .then(response => { // console.log(baseURL+`${id}`); if (response.status != 201 && response.status != 200){ console.log('ocurrio un error ' + response); }else{ return response.json(); } }) } catch (error) { console.log("ERROR: " + error); } } const updateInfo = async() => { const data = { "_id": userData.user._id, "dni": userData.user.dni, "name": name, "last_name": apellido, "email": email, "community_id": userData.user.community_id } if (info.email == "") { info.email = email }else if (info.name == "") { info.name = name }else if (info.last_name ==" ") { info.name = apellido } try { await fetch(baseURL+`${id}`, { cache: 'no-cache', method: 'PUT', body: JSON.stringify(info), headers: { 'Content-Type': 'application/json' } }) .then(response => { console.log(response); //console.log(baseURL+`${id}`); if (response.status != 201){ console.log('ocurrio un error '); }else{ return response.json(); } }) } catch (error) { console.log("ERROR: " + error); } } return ( ) } const styles = StyleSheet.create({ input: { height: 35, margin: 3, borderWidth: 0.5, flex: 1, marginTop: 6, borderRadius: 4 }, errorMessage: { height: 35, margin: 3, borderWidth: 0.5, padding: 5, flex: 1, paddingTop: 9, paddingRight: 19, paddingLeft: 0, marginTop: 6, borderRadius: 4, borderColor: '#be123c' } })