2022-08-23 01:31:42 +00:00
import React , { useContext , useState } from "react" ;
import { API } from "../environment/api" ;
2022-08-30 02:45:34 +00:00
import { MaterialCommunityIcons } from '@expo/vector-icons' ;
2022-08-19 04:30:47 +00:00
import {
2022-08-23 01:31:42 +00:00
Box , Button ,
2022-08-30 02:45:34 +00:00
Center , FormControl , Heading , ScrollView , VStack , Select
2022-08-19 04:30:47 +00:00
} from "native-base" ;
2022-08-23 01:31:42 +00:00
import { StyleSheet , TextInput } from "react-native" ;
import { UserContext } from "../context/UserContext" ;
2022-08-19 04:30:47 +00:00
2022-08-23 01:31:42 +00:00
export default function AgregarInvitados ( { navigation } ) {
2022-08-23 02:53:07 +00:00
const baseURL = ` ${ API . BASE _URL } /guest/createGuest/ ` ;
2022-08-23 01:31:42 +00:00
const [ name , setName ] = useState ( ) ;
const [ apellido , setApellido ] = useState ( ) ;
2022-08-23 02:53:07 +00:00
const [ dni , setDNI ] = useState ( ) ;
const [ phone , setPhone ] = useState ( ) ;
const [ number _plate , setNumber _plate ] = useState ( ) ;
const [ tenant _id , setTenant _id ] = useState ( ) ;
const [ community _id , setCommunity _id ] = useState ( ) ;
const { user } = useContext ( UserContext ) ;
2022-08-26 21:01:29 +00:00
const [ errors , setErrors ] = useState ( { } ) ;
2022-08-30 02:45:34 +00:00
const [ categoria , setCategoria ] = React . useState ( "" ) ;
2022-08-26 21:01:29 +00:00
const [ info , setInfo ] = useState ( {
name : "" ,
last _name : "" ,
dni : "" ,
phone : "" ,
number _plate : "" ,
2022-08-30 02:45:34 +00:00
status : "1" ,
2022-08-26 21:01:29 +00:00
tenant _id : user . _id ,
2022-08-30 02:45:34 +00:00
//tenant_id: "6301df20dac7dcf76dcecade",
community _id : user . community _id ,
//community_id: "62be68215692582bbfd77134",
type _guest : ""
2022-08-26 21:01:29 +00:00
} ) ;
const onHandleChange = ( name ) => ( value ) => setInfo ( prev => ( { ... prev , [ name ] : value } ) )
const validate = async ( ) => {
if ( info . name === "" && info . last _name === "" && info . dni === "" && info . phone === "" ) {
setErrors ( { ... errors ,
name : 'Debe ingresar un nombre' ,
last _name : 'Debe ingresar un apellido' ,
dni : 'Debe ingresar un número de identificación' ,
phone : 'Debe ingresar un número de teléfono'
} ) ;
return false ;
} else if ( info . name === "" ) {
setErrors ( { ... errors ,
name : 'Debe ingresar un nombre'
} ) ;
return false ;
} else if ( info . last _name === "" ) {
setErrors ( { ... errors ,
last _name : 'Debe ingresar un apellido'
} ) ;
return false ;
} else if ( info . dni === "" ) {
setErrors ( { ... errors ,
dni : 'Debe ingresar un número de identificación'
} ) ;
return false ;
} else if ( info . phone === "" ) {
setErrors ( { ... errors ,
phone : 'Debe ingresar un número de teléfono'
} ) ;
return false ;
2022-08-23 01:31:42 +00:00
}
2022-08-26 21:01:29 +00:00
return true ;
}
2022-08-23 01:31:42 +00:00
2022-08-26 21:01:29 +00:00
const saveInvitado = async ( ) => {
const error = await validate ( ) ;
if ( error ) {
try {
await fetch ( baseURL , {
cache : 'no-cache' ,
method : 'POST' ,
body : JSON . stringify ( info ) ,
headers : {
'Content-Type' : 'application/json'
}
} )
. then ( response => {
if ( response . status != 201 ) {
console . log ( 'ocurrio un error ' ) ;
} else {
2022-08-30 02:45:34 +00:00
navigation . navigate ( 'Inicio' ) ;
2022-08-26 21:01:29 +00:00
return response . json ( ) ;
}
} )
} catch ( error ) {
console . log ( "ERROR: " + error ) ;
}
2022-08-23 01:31:42 +00:00
}
2022-08-26 21:01:29 +00:00
2022-08-23 01:31:42 +00:00
}
return (
< Center >
2022-08-26 21:01:29 +00:00
< ScrollView width = '100%' h = '570' ml = '36' _contentContainerStyle = { {
2022-08-23 01:31:42 +00:00
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" >
Registrar invitado
< / H e a d i n g >
< Heading mt = "1" color = "coolGray.600" _dark = { {
color : "warmGray.200"
} } fontWeight = "medium" size = "xs" >
Registre el invitado que desee
< / H e a d i n g >
2022-08-26 21:01:29 +00:00
< VStack space = { 5 } mt = "5" >
2022-08-23 04:20:07 +00:00
< FormControl isRequired >
2022-08-23 01:31:42 +00:00
< FormControl . Label > Nombre < / F o r m C o n t r o l . L a b e l >
2022-08-26 21:01:29 +00:00
< TextInput style = { 'name' in errors ? styles . errorMessage : styles . input } type = "text" onChangeText = { onHandleChange ( "name" ) } / >
{ 'name' in errors && < FormControl . ErrorMessage _text = { {
fontSize : 'xs'
} } > Debe ingresar un correo electrónico < / F o r m C o n t r o l . E r r o r M e s s a g e > }
2022-08-23 01:31:42 +00:00
< / F o r m C o n t r o l >
2022-08-23 04:20:07 +00:00
< FormControl isRequired >
2022-08-23 01:31:42 +00:00
< FormControl . Label > Apellido < / F o r m C o n t r o l . L a b e l >
2022-08-26 21:01:29 +00:00
< TextInput style = { 'last_name' in errors ? styles . errorMessage : styles . input } type = "text" onChangeText = { onHandleChange ( "last_name" ) } / >
2022-08-23 01:31:42 +00:00
< / F o r m C o n t r o l >
2022-08-23 04:20:07 +00:00
< FormControl isRequired >
2022-08-23 02:53:07 +00:00
< FormControl . Label > Identificación < / F o r m C o n t r o l . L a b e l >
2022-08-26 21:01:29 +00:00
< TextInput style = { 'dni' in errors ? styles . errorMessage : styles . input } type = "text" onChangeText = { onHandleChange ( "dni" ) } / >
2022-08-23 01:31:42 +00:00
< / F o r m C o n t r o l >
2022-08-23 04:20:07 +00:00
< FormControl isRequired >
2022-08-23 02:53:07 +00:00
< FormControl . Label > Teléfono < / F o r m C o n t r o l . L a b e l >
2022-08-26 21:01:29 +00:00
< TextInput style = { 'phone' in errors ? styles . errorMessage : styles . input } type = "text" onChangeText = { onHandleChange ( "phone" ) } / >
2022-08-23 01:31:42 +00:00
< / F o r m C o n t r o l >
2022-08-23 20:18:49 +00:00
< FormControl >
< FormControl . Label > Placa < / F o r m C o n t r o l . L a b e l >
2022-08-26 21:01:29 +00:00
< TextInput style = { styles . input } type = "text" onChangeText = { onHandleChange ( "number_plate" ) } / >
2022-08-30 02:45:34 +00:00
< / F o r m C o n t r o l >
< FormControl >
< FormControl . Label > Tipo de invitado < / F o r m C o n t r o l . L a b e l >
2022-09-01 04:58:53 +00:00
< Select onChangeText = { onHandleChange ( "type_guest" ) } selectedValue = { categoria } minWidth = "200" accessibilityLabel = "Choose Service" placeholder = "Elija el tipo de invitado" _selectedItem = { {
2022-08-30 02:45:34 +00:00
bg : "teal.600"
} } mt = { 1 } onValueChange = { onHandleChange ( "type_guest" ) } >
< Select . Item label = "Invitado Frecuente" value = "Frecuente" / >
< Select . Item label = "Invitado de acceso rápido" value = "Rapido" / >
< / S e l e c t >
2022-08-23 20:18:49 +00:00
< / F o r m C o n t r o l >
2022-08-23 04:20:07 +00:00
< Button mt = "2" backgroundColor = 'tertiary.600' onPress = { ( ) => saveInvitado ( ) } >
2022-08-23 01:31:42 +00:00
Guardar
< / B u t t o n >
< / V S t a c k >
< / B o x >
< / S c r o l l V i e w >
< / C e n t e r >
)
}
2022-08-19 04:30:47 +00:00
2022-08-23 01:31:42 +00:00
const styles = StyleSheet . create ( {
input : {
2022-08-26 21:01:29 +00:00
height : 35 ,
2022-08-23 01:31:42 +00:00
margin : 3 ,
borderWidth : 0.5 ,
padding : 5 ,
flex : 1 ,
paddingTop : 9 ,
paddingRight : 19 ,
paddingBottom : 20 ,
paddingLeft : 0 ,
marginTop : 6 ,
marginBottom : 6 ,
borderRadius : 4
2022-08-26 21:01:29 +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-23 01:31:42 +00:00
}
} )
2022-08-19 04:30:47 +00:00