diff --git a/mobile-ui/components/AgregarInvitados.js b/mobile-ui/components/AgregarInvitados.js index 9a8c354e..288cd7c9 100644 --- a/mobile-ui/components/AgregarInvitados.js +++ b/mobile-ui/components/AgregarInvitados.js @@ -20,47 +20,91 @@ export default function AgregarInvitados({ navigation }) { const [tenant_id, setTenant_id] = useState(); const [community_id, setCommunity_id] = useState(); const { user } = useContext(UserContext); + const [errors, setErrors] = useState({}); + + const [info, setInfo] = useState({ + name: "", + last_name: "", + dni: "", + phone: "", + number_plate:"", + status: "-0", + tenant_id: user._id, + community_id: user.community_id + }); + + 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; + } + + return true; + } const saveInvitado = async() => { + const error = await validate(); - const data = { - "name": name, - "last_name": apellido, - "dni": dni, - "phone": phone, - "number_plate": number_plate, - "status":"-0", - "tenant_id": user.id, - "community_id": user.community_id - } - - try { - - await fetch(baseURL, { - - cache: 'no-cache', - method: 'POST', - body: JSON.stringify(data), - headers: { - 'Content-Type': 'application/json' - } - }) - .then(response => { - if (response.status != 201){ - console.log('ocurrio un error '); - }else{ - return response.json(); - } - }) - - } catch (error) { - console.log("ERROR: " + error); + 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{ + return response.json(); + } + }) + + } catch (error) { + console.log("ERROR: " + error); + } } + + } return (
- Registre el invitado que desee - + Nombre - setName(value)}/> + + {'name' in errors && Debe ingresar un correo electrónico } Apellido - setApellido(value)}/> + Identificación - setDNI(value)}/> + Teléfono - setPhone(value)} /> + Placa - setPhone(value)} /> +