clean up saveTenant function

This commit is contained in:
Eduardo Quiros 2022-08-20 02:42:33 -06:00
parent f6ce86909f
commit dc65cdda4f
No known key found for this signature in database
GPG Key ID: B77F36C3F12720B4
1 changed files with 40 additions and 32 deletions

View File

@ -98,38 +98,39 @@ const Inquilinos = () => {
value: item._id, value: item._id,
})) }))
function registrarInquilino() { const saveTenant = () => {
let newTenant = { if (tenant.email && tenant.number_house) {
_id: null, let _tenants = [...tenants]
dni: '', let _tenant = { ...tenant }
name: '', _tenant.community_id = communityId
last_name: '', console.log(_tenant)
email: document.getElementById('correo_electronico').value,
phone: '',
password: '',
community_id: document.getElementById('numero_vivienda').value,
community_name: '',
number_house: 'Sin número de vivienda',
date_entry: new Date(),
user_type: '3',
status: '1',
status_text: '',
}
fetch('http://localhost:4000/api/createUser', { fetch(`http://localhost:4000/user/createUser`, {
method: 'POST',
cache: 'no-cache', cache: 'no-cache',
body: JSON.stringify(newTenant), method: 'POST',
body: JSON.stringify(tenant),
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
}).then((response) => {
if (response.ok) {
alert('Inquilino registrado correctamente')
} else {
alert('Error al registrar inquilino')
}
}) })
.then((response) => {
if (response.status !== 201)
console.log(`Hubo un error en el servicio: ${response.status}`)
else return response.json()
})
.then(() => {
_tenants.push(_tenant)
toast.current.show({
severity: 'success',
summary: 'Éxito',
detail: 'Inquilino creado',
life: 3000,
})
setTenants(_tenants)
setTenant(emptyTenant)
})
.catch((error) => console.log(`Ocurrió un error: ${error}`))
} else setSubmitted(true)
} }
const deleteTenant = () => { const deleteTenant = () => {
@ -418,6 +419,13 @@ const Inquilinos = () => {
) )
} }
const onInputChange = (e) => {
const value = (e.target && e.target.value) || ''
let _tenant = { ...tenant }
_tenant[`${name}`] = value
setTenant(_tenant)
}
return ( return (
<div className='grid'> <div className='grid'>
<div className='col-12'> <div className='col-12'>
@ -618,7 +626,7 @@ const Inquilinos = () => {
onChange={(e) => setCommunityId(e.value)} onChange={(e) => setCommunityId(e.value)}
/> />
</div> </div>
<Button label='Registrar' onClick={registrarInquilino} /> <Button label='Registrar' onClick={saveTenant} />
</div> </div>
</div> </div>
</div> </div>