2022-08-22 23:51:55 +00:00
import React , { useContext , useState } from "react" ;
2022-08-23 01:31:42 +00:00
import { API } from "../environment/api" ;
2022-08-17 16:36:33 +00:00
import {
2022-09-01 18:36:28 +00:00
Box , Button , FormControl , Heading , ScrollView , VStack
2022-08-22 20:31:47 +00:00
} from "native-base" ;
2022-09-01 18:36:28 +00:00
import { StyleSheet , TextInput } from "react-native" ;
2022-08-22 20:31:47 +00:00
import { UserContext } from "../context/UserContext" ;
2022-08-25 02:38:31 +00:00
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs' ;
2022-08-29 23:15:46 +00:00
import { stringMd5 } from 'react-native-quick-md5' ;
2022-09-01 20:58:16 +00:00
import { TouchableOpacity } from "react-native-gesture-handler" ;
2022-08-25 02:38:31 +00:00
const { Navigator , Screen } = createMaterialTopTabNavigator ( ) ;
2022-08-22 20:09:33 +00:00
2022-08-22 20:31:47 +00:00
export default function Profile ( { navigation } ) {
2022-08-22 20:09:33 +00:00
2022-09-01 04:14:08 +00:00
const baseURL = ` ${ API . BASE _URL } /user/updateUser/ `
2022-09-01 18:36:28 +00:00
2022-08-23 01:31:42 +00:00
const userData = useContext ( UserContext )
2022-08-31 04:50:25 +00:00
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 ( ) ;
2022-09-01 04:14:08 +00:00
const [ confirmPassword , setConfirmPassword ] = useState ( )
2022-08-22 20:38:18 +00:00
const id = userData . user . _id ;
2022-08-29 23:15:46 +00:00
const decode = userData . Password ;
2022-08-31 03:22:41 +00:00
const [ error , setError ] = useState ( { } )
2022-09-01 20:58:16 +00:00
const [ disable , setDisable ] = useState ( false )
2022-08-22 06:09:51 +00:00
2022-08-22 06:51:05 +00:00
console . log ( userData . user ) ;
2022-08-22 20:09:33 +00:00
2022-09-01 18:36:28 +00:00
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 } ) )
2022-08-29 23:15:46 +00:00
const onHandleChangePassword = ( value ) => {
2022-08-30 03:08:02 +00:00
//console.log(value);
2022-08-29 23:15:46 +00:00
const dpassword = stringMd5 ( value )
console . log ( dpassword ) ;
2022-08-30 03:08:02 +00:00
2022-08-31 03:22:41 +00:00
console . log ( userData . user . password ) ;
if ( userData . user . password == dpassword ) {
2022-08-30 03:08:02 +00:00
console . log ( true ) ;
2022-08-31 04:50:25 +00:00
setError ( { } ) ;
2022-08-30 03:08:02 +00:00
} else {
console . log ( false ) ;
2022-08-31 03:22:41 +00:00
setError ( { ... error ,
password : 'La contraseña no coincide con la actual'
} ) ;
2022-08-30 03:08:02 +00:00
}
2022-08-29 23:15:46 +00:00
}
2022-08-25 02:38:31 +00:00
const ProfileView = ( ) => (
2022-08-22 20:09:33 +00:00
2022-08-25 01:58:49 +00:00
< ScrollView width = '100%' h = '550' ml = '36' _contentContainerStyle = { {
2022-08-22 06:51:05 +00:00
px : "20px" ,
mb : "4" ,
minW : "72"
} } >
2022-08-17 21:08:38 +00:00
< Box safeArea p = "2" w = "90%" maxW = "290" py = "8" >
< Heading size = "lg" color = "coolGray.800" _dark = { {
color : "warmGray.50"
} } fontWeight = "semibold" >
2022-09-01 20:40:04 +00:00
Bienvenido ( a ) { userData . user . name }
2022-08-17 21:08:38 +00:00
< / H e a d i n g >
< Heading mt = "1" color = "coolGray.600" _dark = { {
color : "warmGray.200"
} } fontWeight = "medium" size = "xs" >
2022-08-17 22:47:48 +00:00
Modifique sus datos
2022-08-17 21:08:38 +00:00
< / H e a d i n g >
< VStack space = { 3 } mt = "5" >
2022-08-22 06:51:05 +00:00
< FormControl >
< FormControl . Label > DNI < / F o r m C o n t r o l . L a b e l >
2022-08-22 20:09:33 +00:00
< TextInput type = "text" defaultValue = { userData . user . dni } editable = { false } / >
< / F o r m C o n t r o l >
2022-08-23 20:18:49 +00:00
{ / * < F o r m C o n t r o l >
2022-08-22 20:09:33 +00:00
< FormControl . Label > Teléfono < / F o r m C o n t r o l . L a b e l >
< TextInput type = "text" defaultValue = { userData . user . phone } editable = { false } / >
2022-08-23 20:18:49 +00:00
< /FormControl> */ }
2022-08-17 21:08:38 +00:00
< FormControl >
< FormControl . Label > Nombre < / F o r m C o n t r o l . L a b e l >
2022-09-01 20:58:16 +00:00
< TextInput style = { styles . input } type = "text" defaultValue = { info . name } onChangeText = { onHandleChange ( "name" ) } placeholder = { name } / >
2022-08-17 21:08:38 +00:00
< / F o r m C o n t r o l >
< FormControl >
2022-08-22 06:51:05 +00:00
< FormControl . Label > Apellido < / F o r m C o n t r o l . L a b e l >
2022-09-01 20:58:16 +00:00
< TextInput style = { styles . input } type = "text" defaultValue = { info . last _name } onChangeText = { onHandleChange ( "last_name" ) } placeholder = { apellido } / >
2022-08-17 21:08:38 +00:00
< / F o r m C o n t r o l >
< FormControl >
2022-08-22 06:51:05 +00:00
< FormControl . Label > Correo electrónico < / F o r m C o n t r o l . L a b e l >
2022-09-01 20:58:16 +00:00
< TextInput style = { styles . input } type = "text" defaultValue = { info . email } onChangeText = { onHandleChange ( "email" ) } placeholder = { email } / >
2022-08-17 21:08:38 +00:00
< / F o r m C o n t r o l >
2022-08-22 20:09:33 +00:00
< Button mt = "2" backgroundColor = "orange.300" onPress = { ( ) => updateInfo ( ) } >
2022-08-17 21:08:38 +00:00
Actualizar
< / B u t t o n >
2022-09-01 20:58:16 +00:00
< TouchableOpacity disabled = { disable } >
< Button disabled = { disable } mt = "6" colorScheme = "error" onPress = { ( ) => navigation . navigate ( 'Iniciar Sesión' ) } >
2022-08-17 21:08:38 +00:00
Cerrar sesión
< / B u t t o n >
2022-09-01 20:58:16 +00:00
< / T o u c h a b l e O p a c i t y >
2022-08-17 21:08:38 +00:00
< / V S t a c k >
< / B o x >
2022-08-22 06:51:05 +00:00
< / S c r o l l V i e w >
2022-08-25 01:58:49 +00:00
2022-08-25 02:38:31 +00:00
)
2022-08-25 01:58:49 +00:00
2022-08-25 02:38:31 +00:00
const PasswordView = ( ) => (
2022-08-25 01:58:49 +00:00
< ScrollView width = '100%' h = '550' ml = '36' _contentContainerStyle = { {
px : "20px" ,
mb : "4" ,
minW : "72"
} } >
< Box safeArea p = "2" w = "90%" maxW = "290" py = "8" >
< Heading size = "lg" color = "coolGray.800" _dark = { {
color : "warmGray.50"
} } fontWeight = "semibold" >
2022-09-01 20:40:04 +00:00
Bienvenido ( a ) { userData . user . name }
2022-08-25 01:58:49 +00:00
< / H e a d i n g >
< Heading mt = "1" color = "coolGray.600" _dark = { {
color : "warmGray.200"
} } fontWeight = "medium" size = "xs" >
Modifique sus contraseña
< / H e a d i n g >
< VStack space = { 3 } mt = "5" >
< FormControl >
< FormControl . Label > Contraseña actual < / F o r m C o n t r o l . L a b e l >
2022-09-01 20:40:04 +00:00
< TextInput secureTextEntry = { true } style = { 'password' in error ? styles . errorMessage : styles . input } type = "password" onChangeText = { ( value ) => onHandleChangePassword ( value ) } placeholder = 'Actual contraseña' / >
2022-08-25 01:58:49 +00:00
< / F o r m C o n t r o l >
2022-08-31 03:22:41 +00:00
2022-08-25 01:58:49 +00:00
< FormControl >
< FormControl . Label > Nueva Contraseña < / F o r m C o n t r o l . L a b e l >
2022-09-01 20:40:04 +00:00
< TextInput secureTextEntry = { true } editable = { ! error } style = { styles . input } type = "password" onChangeText = { ( value ) => setPassword ( value ) } placeholder = 'Nueva contraseña' / >
2022-08-25 01:58:49 +00:00
< / F o r m C o n t r o l >
< FormControl >
< FormControl . Label > Confirmar nueva contraseña < / F o r m C o n t r o l . L a b e l >
2022-09-01 20:40:04 +00:00
< TextInput secureTextEntry = { true } editable = { ! error } style = { styles . input } type = "password" onChangeText = { ( value ) => setConfirmPassword ( value ) } placeholder = 'Confirmar nueva contraseña' / >
2022-08-25 01:58:49 +00:00
< / F o r m C o n t r o l >
2022-08-31 03:51:17 +00:00
< Button mt = "2" backgroundColor = "orange.300" onPress = { ( ) => updatePassword ( ) } disabled = { error } >
2022-08-25 01:58:49 +00:00
Actualizar contraseña
< / B u t t o n >
2022-08-31 03:22:41 +00:00
{ / * { ' p a s s w o r d ' i n e r r o r & & < F o r m C o n t r o l . E r r o r M e s s a g e _ t e x t = { {
fontSize : 'xs'
} } > La contraseña no coincide con la actual < /FormControl.ErrorMessage> } */ }
2022-08-25 01:58:49 +00:00
< / V S t a c k >
< / B o x >
< / S c r o l l V i e w >
2022-08-25 02:38:31 +00:00
)
2022-08-25 01:58:49 +00:00
2022-08-29 23:15:46 +00:00
const updatePassword = async ( ) => {
2022-08-25 01:58:49 +00:00
2022-08-31 03:51:17 +00:00
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 => {
2022-08-31 04:50:25 +00:00
// console.log(baseURL+`${id}`);
2022-08-31 15:25:16 +00:00
if ( response . status != 201 && response . status != 200 ) {
console . log ( 'ocurrio un error ' + response ) ;
2022-08-31 04:50:25 +00:00
2022-08-31 03:51:17 +00:00
} else {
return response . json ( ) ;
}
} )
} catch ( error ) {
console . log ( "ERROR: " + error ) ;
}
2022-08-29 23:15:46 +00:00
}
2022-08-25 01:58:49 +00:00
const updateInfo = async ( ) => {
const data = {
2022-08-31 03:51:17 +00:00
"_id" : userData . user . _id ,
"dni" : userData . user . dni ,
2022-08-25 01:58:49 +00:00
"name" : name ,
"last_name" : apellido ,
"email" : email ,
2022-08-31 18:27:36 +00:00
"community_id" : userData . user . community _id
2022-08-25 01:58:49 +00:00
}
2022-09-01 20:58:16 +00:00
if ( info . email == "" ) {
info . email = email
} else if ( info . name == "" ) {
info . name = name
} else if ( info . last _name == " " ) {
info . name = apellido
2022-09-01 20:40:04 +00:00
}
2022-08-25 01:58:49 +00:00
2022-09-01 20:58:16 +00:00
try {
2022-08-31 04:50:25 +00:00
2022-09-01 20:58:16 +00:00
await fetch ( baseURL + ` ${ id } ` , {
2022-08-25 01:58:49 +00:00
2022-09-01 20:58:16 +00:00
cache : 'no-cache' ,
method : 'PUT' ,
body : JSON . stringify ( info ) ,
headers : {
'Content-Type' : 'application/json'
}
} )
. then ( response => {
2022-08-25 01:58:49 +00:00
2022-09-01 20:58:16 +00:00
console . log ( response ) ;
2022-09-01 04:14:08 +00:00
2022-09-01 20:58:16 +00:00
//console.log(baseURL+`${id}`);
if ( response . status != 201 ) {
console . log ( 'ocurrio un error ' ) ;
2022-09-01 04:14:08 +00:00
2022-09-01 20:58:16 +00:00
} else {
return response . json ( ) ;
}
} )
2022-08-25 01:58:49 +00:00
2022-09-01 20:58:16 +00:00
} catch ( error ) {
console . log ( "ERROR: " + error ) ;
}
2022-08-25 01:58:49 +00:00
}
return (
2022-08-25 02:38:31 +00:00
< Navigator >
2022-09-01 04:58:53 +00:00
< Screen name = "Datos Personales" component = { ProfileView } / >
2022-08-25 02:38:31 +00:00
< Screen name = "Contraseña" component = { PasswordView } / >
< / N a v i g a t o r >
2022-08-22 20:31:47 +00:00
)
2022-08-22 06:51:05 +00:00
}
const styles = StyleSheet . create ( {
input : {
2022-08-31 03:22:41 +00:00
height : 35 ,
2022-08-22 20:31:47 +00:00
margin : 3 ,
2022-08-22 06:51:05 +00:00
borderWidth : 0.5 ,
flex : 1 ,
2022-08-22 20:31:47 +00:00
marginTop : 6 ,
2022-08-22 06:51:05 +00:00
borderRadius : 4
2022-08-31 03:22:41 +00:00
} ,
errorMessage : {
height : 35 ,
margin : 3 ,
borderWidth : 0.5 ,
padding : 5 ,
flex : 1 ,
paddingTop : 9 ,
paddingRight : 19 ,
paddingLeft : 0 ,
marginTop : 6 ,
borderRadius : 4 ,
borderColor : '#be123c'
2022-08-22 06:51:05 +00:00
}
2022-08-22 20:31:47 +00:00
} )
2022-08-22 06:51:05 +00:00