import { Button } from 'primereact/button' import { InputText } from 'primereact/inputtext' import React, { useEffect, useRef, useState } from 'react' import { DataTable } from 'primereact/datatable' import { Column } from 'primereact/column' import { Dropdown } from 'primereact/dropdown' import { Toast } from 'primereact/toast' import { Dialog } from 'primereact/dialog' import { Toolbar } from 'primereact/toolbar' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faUserAlt } from '@fortawesome/free-solid-svg-icons' import { faPhoneAlt } from '@fortawesome/free-solid-svg-icons' import { faAt } from '@fortawesome/free-solid-svg-icons' import { faIdCardAlt } from '@fortawesome/free-solid-svg-icons' import { faHashtag } from '@fortawesome/free-solid-svg-icons' import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons' import { useCookies } from 'react-cookie' import classNames from 'classnames'; const Inquilinos = () => { const emptyTenant = { _id: null, dni: '', name: '', last_name: '', email: '', phone: '', password: '', community_id: '', community_name: '', number_house: 'Sin número de vivienda', user_type: '4', date_entry: new Date(), status: '1', status_text: '', } const [tenants, setTenants] = useState([]) const [tenant, setTenant] = useState(emptyTenant) const [selectedTentants, setSelectedTenants] = useState(null) const [globalFilter, setGlobalFilter] = useState(null) const [deleteTenantDialog, setDeleteTenantDialog] = useState(false) const [deleteTenantsDialog, setDeleteTenantsDialog] = useState(false) const [community, setCommunity] = useState([]) const [saveButtonTitle, setSaveButtonTitle] = useState("Registrar") const [houseNumber, setHouseNumber] = useState([]) const [housesList, setHousesList] = useState([]) const [submitted, setSubmitted] = useState(false) const [infoDialogVisible, setShowInfoDialog] = useState(false) const toast = useRef(null) const dt = useRef(null) const [cookies] = useCookies() const [changeStatusTenantDialog, setChangeStatusTenantDialog] = useState(false) async function tenantsList() { await fetch( `http://localhost:4000/user/findTenants/${cookies.community_id}`, { method: 'GET' }, ) .then((response) => response.json()) .then((data) => data.message) .then((data) => { data = data.filter((val) => val.status !== -1) data.map((item) => { if (item.status === '1') { item.status_text = 'Activo' } else if (item.status === '0') { item.status_text = 'Inactivo' } if (item.number_house === '') { item.number_house = 'Sin vivienda asignada' } }) setTenants(data) }) } async function getCommunity() { let response = await fetch( `http://localhost:4000/community/findCommunityName/${cookies.community_id}`, { method: 'GET' }, ) const responseJson = await response.json() const result = await responseJson.message setCommunity(await result) const houses = await result.houses.filter((house) => house.state === "desocupada" ) setHousesList(houses.map((house) => ({ label: house.number_house, value: house.number_house })) ) } async function getHouses() { let response = await fetch( `http://localhost:4000/community/findHousesCommunity/${cookies.community_id}`, { method: 'GET' }, ) let resList = await response.json() setHousesList(await resList) } useEffect(() => { tenantsList() }, []) useEffect(() => { getCommunity() }, []) const saveTenant = () => { if (tenant._id === null) { if (tenant.email && tenant.number_house && tenant.dni && tenant.name && tenant.last_name && tenant.phone) { let _tenants = [...tenants] let _tenant = { ...tenant } _tenant.community_id = cookies.community_id; _tenant.number_house = houseNumber; _tenant.password = _tenant.email; console.log(`Registrando nuevo inquilino: ${_tenant}`) fetch(`http://localhost:4000/user/createTenant`, { cache: 'no-cache', method: 'POST', body: JSON.stringify(_tenant), headers: { 'Content-Type': 'application/json', }, }) .then((response) => { if (response.status !== 200) console.log(`Hubo un error en el servicio: ${response.status}`) else return response.json() }) .then((data) => { if (_tenant.status === '1') { _tenant.status_text = 'Activo' } else if (_tenant.status === '0') { _tenant.status_text = 'Inactivo' } _tenants.push(_tenant) toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Inquilino creado', life: 3000, }) setTenants(_tenants) setTenant(emptyTenant) setHouseNumber('') }) .catch((error) => console.log(`Ocurrió un error: ${error}`)) } else setSubmitted(true) } else { let _tenant = { ..._tenant, number_house: houseNumber }; console.log(`Actualizando inquilino: ${_tenant}`) fetch(`http://localhost:4000/user/updateUser/${tenant._id}`, { cache: 'no-cache', method: 'PUT', body: JSON.stringify(_tenant), headers: { 'Content-Type': 'application/json', }, }).then((response) => { if (response.status !== 200) console.log(`Hubo un error en el servicio: ${response.status}`) else return response.json() }).then(() => { toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Inquilino editado', life: 3000, }) tenantsList() setTenant(emptyTenant) setHouseNumber('') }) } } const deleteTenant = () => { let _tenant = { community_id: tenant.community_id, number_house: tenant.number_house }; fetch('http://localhost:4000/user/deleteTenant/' + tenant._id, { cache: 'no-cache', method: 'PUT', body: JSON.stringify(_tenant), headers: { 'Content-Type': 'application/json' } }) .then( function (response) { if (response.status != 201) console.log('Ocurrió un error con el servicio: ' + response.status); else return response.json(); } ) .then( function (response) { let _tenants = tenants.filter((val) => val._id !== tenant._id) setTenants(_tenants) setDeleteTenantDialog(false) setTenant(emptyTenant) toast.current.show({ severity: 'success', summary: 'Inquilino Eliminado', life: 3000, }) } ) .catch( err => { console.log('Ocurrió un error con el fetch', err) toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Inquilino no se pudo eliminar', life: 3000 }); } ); } const deleteSelectedTenants = () => { let _tenants = tenants.filter( (val) => !selectedTentants.includes(val) ); selectedTentants.map((item) => { let _tenant = { community_id: item.community_id, number_house: item.number_house }; fetch('http://localhost:4000/user/deleteTenant/' + item._id, { cache: 'no-cache', method: 'PUT', body: JSON.stringify(_tenant), headers: { 'Content-Type': 'application/json', }, }); }); setTenants(_tenants) setDeleteTenantsDialog(false) setSelectedTenants(null) toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Inquilinos Eliminados', life: 3000, }) } const cambiarStatusUser = () => { if (tenant.status === '1') { tenant.status = '0' tenant.status_text = 'Inactivo' } else if (tenant.status === '0') { tenant.status = '1' tenant.status_text = 'Activo' } var data = { id: tenant._id, status: tenant.status, } fetch('http://localhost:4000/user/changeStatus', { cache: 'no-cache', method: 'POST', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json', }, }) .then((response) => { if (response.status !== 201) { console.log('Ocurrió un error con el servicio: ' + response.status) } else { return response.json() } }) .then(() => { setChangeStatusTenantDialog(false) toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Inquilino Actualizado', life: 3000, }) }) .catch((err) => console.log('Ocurrió un error con el fetch', err)) } const hideDeleteTenantDialog = () => { setDeleteTenantDialog(false) } const hideDeleteTenantsDialog = () => { setDeleteTenantsDialog(false) } const confirmDeleteTenant = (tenant) => { setTenant(tenant) setDeleteTenantDialog(true) } const confirmDeleteSelected = () => { setDeleteTenantsDialog(true) } const hideChangeStatusTenantDialog = () => { setChangeStatusTenantDialog(false) } const confirmChangeStatusTenant = (tenant) => { setTenant(tenant) setChangeStatusTenantDialog(true) } const hideInfoDialog = () => { setSubmitted(false); setShowInfoDialog(false); setTenant(emptyTenant); } const infoTenant = (tenant) => { setTenant(tenant); setShowInfoDialog(true); } const editTenant = (tenant) => { setTenant(tenant); console.log(tenant); setSaveButtonTitle('Actualizar'); setHouseNumber(tenant.number_house); } const cancelEdit = () => { setTenant(emptyTenant); setSaveButtonTitle('Registrar'); setHouseNumber(''); } const actionsTenant = (rowData) => { let icono = '' let text = '' if (rowData.status === '0') { icono = 'pi pi-eye' text = 'Activar Inquilino' } else if (rowData.status === '1') { icono = 'pi pi-eye-slash' text = 'Desactivar Inquilino' } return (
{' '}