import { Button } from 'primereact/button'; import { InputText } from 'primereact/inputtext' import React, { useEffect, useState, useRef } 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 { faHome } from '@fortawesome/free-solid-svg-icons'; 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 { faEllipsis } from '@fortawesome/free-solid-svg-icons'; import { faHashtag } from '@fortawesome/free-solid-svg-icons'; import { useCookies } from "react-cookie"; const Inquilinos = () => { let 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' }; 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 [communitiesList, setCommunitiesList] = useState([]); const [communityId, setCommunityId] = useState(null); const [submitted, setSubmitted] = useState(false); const toast = useRef(null); const dt = useRef(null); const [cookies, setCookie] = useCookies(); 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.map((item) => { if(item.number_house ==""){ item.number_house = "Sin vivienda asignada"; } }) setTenants(data) }); } async function getCommunites() { let response = await fetch('http://localhost:4000/community/allCommunities', { method: 'GET' }); let resList = await response.json(); let list = await resList.message; setCommunitiesList(await list); } useEffect(() => { tenantsList(); }, []) useEffect(() => { getCommunites(); }, []) const cList = communitiesList.map((item) => ({ label: item.name, value: item._id, })) function registrarInquilino() { let data = { dni: document.getElementById('identificacion').value, name: document.getElementById('nombre').value, last_name: document.getElementById('apellidos').value, phone: document.getElementById('telefono').value, email: document.getElementById('correo_electronico').value, community_id: document.getElementById('numero_vivienda').value, password: document.getElementById('password').value, user_type: '3', status: '1', }; fetch('http://localhost:3000/api/createUser', { method: 'POST', cache: 'no-cache', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json', }, }).then((response) => { if (response.ok) { alert('Inquilino registrado correctamente'); } else { alert('Error al registrar inquilino'); } }); } const deleteTenant = () => { /* fetch('http://localhost:4000/community/deleteCommunity/' + community._id, { cache: 'no-cache', method: 'DELETE', 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 _community = communities.filter(val => val._id !== community._id); setCommunities(_community); setDeleteCommunityDialog(false); setCommunity(emptyCommunity); toast.current.show({ severity: 'success', summary: 'Exito', detail: 'Comunidad de Viviendas Eliminada', life: 3000 }); } ) .catch( err => { console.log('Ocurrió un error con el fetch', err) toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Comunidad de Viviendas no se pudo eliminar', life: 3000 }); } ); */ 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, }); }; const deleteSelectedTenants = () => { let _tenants = tenants.filter( (val) => !selectedTentants.includes(val), ); /* selectedCommunities.map((item) => { fetch('http://localhost:4000/user/deleteCommunity/' + item._id, { cache: 'no-cache', method: 'DELETE', headers: { 'Content-Type': 'application/json' } }) })*/ setTenants(_tenants); setDeleteTenantsDialog(false); setSelectedTenants(null); toast.current.show({ severity: 'success', summary: 'Éxito', detail: 'Inquilinos Eliminados', life: 3000, }); }; const hideDeleteTenantDialog = () => { setDeleteTenantDialog(false); } const hideDeleteTenantsDialog = () => { setDeleteTenantsDialog(false); } const confirmDeleteTenant = (tenant) => { setTenant(tenant); setDeleteTenantDialog(true); } const confirmDeleteSelected = () => { setDeleteTenantsDialog(true); }; const actionsTenant = (rowData) => { return (
Opciones