katoikia-app/web-ui/web-react/src/components/AdministradoresComunidad.js

497 lines
22 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useState, useRef } from 'react';
import { InputText } from 'primereact/inputtext';
import { Button } from 'primereact/button';
import { DataTable } from 'primereact/datatable';
import { Column } from 'primereact/column';
import { Toast } from 'primereact/toast';
import { Dialog } from 'primereact/dialog';
import { Toolbar } from 'primereact/toolbar';
2022-07-25 04:38:48 +00:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faHome, 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 { faHomeAlt } from '@fortawesome/free-solid-svg-icons';
2022-07-25 06:25:39 +00:00
import { Dropdown } from 'primereact/dropdown';
2022-07-25 22:08:00 +00:00
import classNames from 'classnames';
const AdministradoresComunidad = () => {
let emptyAdminCommunity = {
_id: null,
dni: '',
name: '',
last_name: '',
email: '',
phone: '',
password: '',
2022-07-25 22:08:00 +00:00
confirmPassword: '',
community_id: '',
community_name: '',
user_type: '2',
date_entry: new Date(),
status: '1'
};
2022-07-25 22:08:00 +00:00
const [listaAdmins, setListaAdmins] = useState([]);
const [listaAdminComunidad, setListaAdminComunidad] = useState([]);
const [adminCommunity, setAdminCommunity] = useState(emptyAdminCommunity);
const [selectedAdminsCommunities, setSelectedAdminsCommunities] = useState(null);
const [globalFilter, setGlobalFilter] = useState(null);
const [deleteAdminCommunityDialog, setDeleteAdminCommunityDialog] = useState(false);
const [deleteAdminsCommunitiesDialog, setDeleteAdminsCommunitiesDialog] = useState(false);
const [communitiesList, setCommunitiesList] = useState([]);
const [communityId, setCommunityId] = useState(null);
const [submitted, setSubmitted] = useState(false);
const toast = useRef(null);
const dt = useRef(null);
2022-07-25 06:25:39 +00:00
async function listaAdmin() {
let nombres = await fetch('http://localhost:4000/user/findAdminComunidad/', { method: 'GET' })
.then((response) => response.json())
.then((data) => {
return Promise.all(data.message.map(item => {
//item.full_name returns the repositorie name
return fetch(`http://localhost:4000/community/findCommunityName/${item.community_id}`, { method: 'GET' })
.then((response2) => response2.json())
.then(data => data.message)
2022-07-27 04:03:08 +00:00
.then(data => {
item.community_name = data['name']
return item
})
}));
})
.then(data => setListaAdmins(data));
}
2022-07-25 22:08:00 +00:00
async function getCommunites() {
let response = await fetch('http://localhost:4000/community/allCommunities', { method: 'GET' });
let resList = await response.json();
let list = await resList.message;
console.log(list);
setCommunitiesList(await list);
}
useEffect(() => {
2022-07-27 04:03:08 +00:00
listaAdmin();
2022-07-25 22:08:00 +00:00
}, [])
2022-07-27 04:03:08 +00:00
useEffect(() => {
getCommunites();
},[])
2022-07-25 06:25:39 +00:00
const cList = communitiesList.map((item) => ({
label: item.name,
value: item._id,
2022-07-25 06:25:39 +00:00
}))
2022-07-25 22:08:00 +00:00
const deleteAdminCommunity = () => {
/* 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 });
}
);
*/
2022-07-27 04:03:08 +00:00
let _administrators = listaAdmins.filter(
(val) => val._id !== adminCommunity._id,
);
setListaAdmins(_administrators);
setDeleteAdminCommunityDialog(false);
setAdminCommunity(emptyAdminCommunity);
toast.current.show({
severity: 'success',
summary: 'Administrador de Comunidad Eliminada',
life: 3000,
});
};
const deleteSelectedAdminsCommunity = () => {
let _admins = listaAdmins.filter(
(val) => !selectedAdminsCommunities.includes(val),
);
/* selectedCommunities.map((item) => {
fetch('http://localhost:4000/user/deleteCommunity/' + item._id, {
cache: 'no-cache',
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
})
})*/
setListaAdmins(_admins);
setDeleteAdminsCommunitiesDialog(false);
setSelectedAdminsCommunities(null);
toast.current.show({
severity: 'success',
summary: 'Éxito',
detail: 'Administradores de Comunidad de Viviendas Eliminados',
life: 3000,
});
};
2022-07-25 22:08:00 +00:00
const saveAdminCommunity = () => {
if (adminCommunity.name && adminCommunity.dni && adminCommunity.last_name && adminCommunity.email && adminCommunity.phone) {
2022-07-27 04:03:08 +00:00
let _administrators = [...listaAdmins];
let _adminCommunity = { ...adminCommunity };
_adminCommunity.community_id = communityId;
console.log(_adminCommunity)
console.log(communityId)
fetch('http://localhost:4000/user/createAdminCommunity', {
cache: 'no-cache',
method: 'POST',
body: JSON.stringify(_adminCommunity),
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(() => {
2022-07-27 04:03:08 +00:00
// _adminCommunity.community_id = communitiesList.find(c => c._id === _adminCommunity.community_id).name
_administrators.push(_adminCommunity);
2022-07-26 23:07:12 +00:00
toast.current.show({ severity: 'success', summary: 'Registro exitoso', detail: 'Administrador de Comunidad de vivienda Creada', life: 3000 });
setListaAdmins(_administrators);
setAdminCommunity(emptyAdminCommunity);
})
.catch(
err => console.log('Ocurrió un error con el fetch', err)
);
2022-07-25 22:08:00 +00:00
} else {
setSubmitted(true);
2022-07-25 06:25:39 +00:00
2022-07-25 22:08:00 +00:00
}
2022-07-25 06:25:39 +00:00
}
const hideDeleteAdminCommunityDialog = () => {
setDeleteAdminCommunityDialog(false);
}
2022-07-27 04:03:08 +00:00
const hideDeleteAdminsCommunitysDialog = () => {
setDeleteAdminsCommunitiesDialog(false);
}
2022-07-27 04:03:08 +00:00
const confirmDeleteAdminCommunity = (adminCommunity) => {
setAdminCommunity(adminCommunity);
setDeleteAdminCommunityDialog(true);
}
2022-07-25 06:17:26 +00:00
const confirmDeleteSelected = () => {
setDeleteAdminsCommunitiesDialog(true);
2022-07-27 04:03:08 +00:00
};
2022-07-25 06:17:26 +00:00
const actionsAdminCommunity = (rowData) => {
return (
<div className="actions">
2022-07-27 04:03:08 +00:00
<Button
icon="pi pi-trash"
className="p-button-rounded p-button-danger mt-2"
onClick={() => confirmDeleteAdminCommunity(rowData)}
/>
</div>
);
2022-07-27 04:03:08 +00:00
};
const deleteAdminCommunityDialogFooter = (
<>
<Button label="No" icon="pi pi-times" className="p-button-text" onClick={hideDeleteAdminCommunityDialog} />
<Button label="Yes" icon="pi pi-check" className="p-button-text" onClick={deleteAdminCommunity} />
</>
);
const deleteAdminsCommunityDialogFooter = (
<>
<Button label="No" icon="pi pi-times" className="p-button-text" onClick={hideDeleteAdminsCommunitysDialog} />
<Button label="Yes" icon="pi pi-check" className="p-button-text" onClick={deleteSelectedAdminsCommunity} />
</>
);
2022-07-25 06:17:26 +00:00
const leftToolbarTemplate = () => {
return (
<React.Fragment>
<div className="my-2">
<Button label="Eliminar" icon="pi pi-trash" className="p-button-danger" onClick={confirmDeleteSelected} disabled={!selectedAdminsCommunities || !selectedAdminsCommunities.length} />
</div>
</React.Fragment>
)
}
const rightToolbarTemplate = () => {
return (
<React.Fragment>
2022-07-25 06:25:39 +00:00
<Button label="Exportar" icon="pi pi-upload" className="p-button-help" />
2022-07-25 06:17:26 +00:00
</React.Fragment>
)
}
2022-07-25 06:25:39 +00:00
2022-07-25 06:17:26 +00:00
const header = (
2022-07-27 04:03:08 +00:00
<React.Fragment>
<div className="flex flex-column md:flex-row md:justify-content-between md:align-items-center">
<h5 className="m-0">Administradores de Comunidades</h5>
<span className="block mt-2 md:mt-0 p-input-icon-left">
<i className="pi pi-search" />
<InputText type="search" onInput={(e) => setGlobalFilter(e.target.value)} placeholder="Buscar..." />
</span>
</div>
</React.Fragment>
2022-07-25 06:17:26 +00:00
);
const headerName = (
<>
2022-07-27 04:03:08 +00:00
<p>{' '}
<FontAwesomeIcon icon={faUserAlt} style={{ color: "#C08135" }} /> {' '}
Nombre
</p>
</>
)
2022-07-27 04:03:08 +00:00
const headerLastName = (
<>
<p>
{' '}
<FontAwesomeIcon icon={faUserAlt} style={{ color: "#D7A86E" }} />{' '}
Apellidos
</p>
</>
)
2022-07-27 04:03:08 +00:00
const headerDNI = (
<>
<p> {' '}
<FontAwesomeIcon icon={faIdCardAlt} style={{ color: "#C08135" }} />{' '}
Identificación
</p>
</>
)
2022-07-27 04:03:08 +00:00
const headerEmail = (
<>
<p> {' '}
<FontAwesomeIcon icon={faAt} style={{ color: "#D7A86E" }} />{' '}
Correo Electrónic
</p>
</>
)
2022-07-27 04:03:08 +00:00
const headerPhone = (
<>
<p> <FontAwesomeIcon icon={faPhoneAlt} style={{ color: "#C08135" }} />{' '}
Teléfono
</p>
</>
)
2022-07-27 04:03:08 +00:00
const headerCommuntiy = (
<>
2022-07-27 04:03:08 +00:00
<p>
{' '}
<FontAwesomeIcon icon={faHomeAlt} style={{ color: "#D7A86E" }} />{' '}
Comunidad</p>
</>
2022-07-27 04:03:08 +00:00
)
2022-07-27 04:03:08 +00:00
const headerOptions = (
<>
2022-07-27 04:03:08 +00:00
<p>
{' '}
<FontAwesomeIcon icon={faEllipsis} size="2x" style={{ color: "#C08135" }} />{' '}
</p>
</>
2022-07-27 04:03:08 +00:00
)
2022-07-27 04:03:08 +00:00
const onInputChange = (e, name) => {
const val = (e.target && e.target.value) || '';
let _adminCommunity = { ...adminCommunity };
_adminCommunity[`${name}`] = val;
2022-07-27 04:03:08 +00:00
setAdminCommunity(_adminCommunity);
}
2022-07-25 22:08:00 +00:00
const handleCommunities = (event) => {
const getCommunityValue = event.target.value;
setCommunityId(getCommunityValue);
console.log(getCommunityValue)
2022-07-25 22:08:00 +00:00
}
2022-07-27 04:03:08 +00:00
return (
2022-07-27 04:03:08 +00:00
<div className="grid">
<div className="col-12">
<Toast ref={toast} />
<div className="card">
<Toolbar className="mb-4" left={leftToolbarTemplate} right={rightToolbarTemplate}></Toolbar>
<DataTable ref={dt} value={listaAdmins} dataKey="_id" paginator rows={5} selection={selectedAdminsCommunities} onSelectionChange={(e) => setSelectedAdminsCommunities(e.value)}
scrollable scrollHeight="400px" scrollDirection="both" header={header}
rowsPerPageOptions={[5, 10, 25]} className="datatable-responsive mt-3"
paginatorTemplate="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport RowsPerPageDropdown"
currentPageReportTemplate="Mostrando {first} a {last} de {totalRecords} administradores de comunidades de viviendas"
globalFilter={globalFilter} emptyMessage="No hay administradores de comunidades registrados.">
<Column selectionMode="multiple" headerStyle={{ width: '3rem' }}></Column>
<Column field="name" sortable header={headerName} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column field="last_name" sortable header={headerLastName} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }} alignFrozen="left"></Column>
<Column field="dni" sortable header={headerDNI} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}>
</Column>
<Column field="email" sortable header={headerEmail} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column field="phone" sortable header={headerPhone} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column field="community_name" header={headerCommuntiy} style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}></Column>
<Column header={headerOptions} style={{ flexGrow: 1, flexBasis: '130px', minWidth: '130px' }} body={actionsAdminCommunity}></Column>
</DataTable>
<Dialog visible={deleteAdminCommunityDialog} style={{ width: '450px' }} header="Confirmar" modal footer={deleteAdminCommunityDialogFooter} onHide={hideDeleteAdminCommunityDialog}>
<div className="flex align-items-center justify-content-center">
<i className="pi pi-exclamation-triangle mr-3" style={{ fontSize: '2rem' }} />
{adminCommunity && <span>¿Estás seguro que desea eliminar a <b>{adminCommunity.name}</b>?</span>}
</div>
</Dialog>
<Dialog visible={deleteAdminsCommunitiesDialog} style={{ width: '450px' }} header="Confirmar" modal footer={deleteAdminsCommunityDialogFooter} onHide={hideDeleteAdminsCommunitysDialog}>
<div className="flex align-items-center justify-content-center">
<i className="pi pi-exclamation-triangle mr-3" style={{ fontSize: '2rem' }} />
{selectedAdminsCommunities && <span>¿Está seguro eliminar los administradores de las comunidades de viviendas seleccionados?</span>}
</div>
</Dialog>
</div>
</div>
2022-07-25 06:25:39 +00:00
<div className="col-12">
<div className="card">
<h5>Registro de un administrador de una comunidad de viviendas</h5>
<div className="p-fluid formgrid grid">
<div className="field col-12 md:col-6">
2022-07-25 22:08:00 +00:00
<label htmlFor="name">Nombre</label>
<div className="p-0 col-12 md:col-12">
<div className="p-inputgroup">
<span className="p-inputgroup-addon p-button p-icon-input-khaki">
<i className="pi pi-home"></i>
</span>
<InputText id="name" value={adminCommunity.name} onChange={(e) => onInputChange(e, 'name')} required autoFocus className={classNames({ 'p-invalid': submitted && adminCommunity.name === '' })} />
</div>
{submitted && adminCommunity.name === '' && <small className="p-invalid">Nombre es requirido.</small>}
</div>
2022-07-25 06:25:39 +00:00
</div>
<div className="field col-12 md:col-6">
2022-07-25 22:08:00 +00:00
<label htmlFor="name">Apellido(s)</label>
<div className="p-0 col-12 md:col-12">
<div className="p-inputgroup">
<span className="p-inputgroup-addon p-button p-icon-input-khaki">
<i className="pi pi-home"></i>
</span>
<InputText id="last_name" value={adminCommunity.last_name} onChange={(e) => onInputChange(e, 'last_name')} required autoFocus className={classNames({ 'p-invalid': submitted && adminCommunity.last_name === '' })} />
</div>
{submitted && adminCommunity.last_name === '' && <small className="p-invalid">Apellidos es requirido.</small>}
</div>
2022-07-25 06:25:39 +00:00
</div>
<div className="field col-12 md:col-6">
2022-07-25 22:08:00 +00:00
<label htmlFor="name">Correo Electrónico</label>
<div className="p-0 col-12 md:col-12">
<div className="p-inputgroup">
<span className="p-inputgroup-addon p-button p-icon-input-khaki">
<i className="pi pi-home"></i>
</span>
<InputText id="email" value={adminCommunity.email} onChange={(e) => onInputChange(e, 'email')} required autoFocus className={classNames({ 'p-invalid': submitted && adminCommunity.email === '' })} />
</div>
{submitted && adminCommunity.email === '' && <small className="p-invalid">Correo electrónico
es requirido.</small>}
</div>
2022-07-25 06:25:39 +00:00
</div>
<div className="field col-12 md:col-6">
2022-07-25 22:08:00 +00:00
<label htmlFor="name">Identificación</label>
<div className="p-0 col-12 md:col-12">
<div className="p-inputgroup">
<span className="p-inputgroup-addon p-button p-icon-input-khaki">
<i className="pi pi-home"></i>
</span>
<InputText id="dni" value={adminCommunity.dni} onChange={(e) => onInputChange(e, 'dni')} required autoFocus className={classNames({ 'p-invalid': submitted && adminCommunity.dni === '' })} />
</div>
{submitted && adminCommunity.email === '' && <small className="p-invalid">Identificación es requirida.</small>}
</div>
2022-07-25 06:25:39 +00:00
</div>
2022-07-25 22:08:00 +00:00
<div className="field col-12 md:col-6">
<label htmlFor="name">Número de teléfono</label>
<div className="p-0 col-12 md:col-12">
<div className="p-inputgroup">
<span className="p-inputgroup-addon p-button p-icon-input-khaki">
<i className="pi pi-phone"></i>
</span>
<InputText id="phone" value={adminCommunity.phone} onChange={(e) => onInputChange(e, 'phone')} required autoFocus className={classNames({ 'p-invalid': submitted && adminCommunity.phone === '' })} />
</div>
{submitted && adminCommunity.phone === '' && <small className="p-invalid">Número de teléfono es requirida.</small>}
</div>
2022-07-25 06:25:39 +00:00
</div>
2022-07-25 22:08:00 +00:00
<div className="field col-12 md:col-6">
2022-07-25 06:25:39 +00:00
<label htmlFor="administrator">Comunidad a asignar: </label>
2022-07-25 22:08:00 +00:00
<div className="p-0 col-12 md:col-12">
<div className="p-inputgroup">
<span className="p-inputgroup-addon p-button p-icon-input-khaki">
<i className="pi pi-home"></i>
</span>
<Dropdown placeholder="--Seleccione la Comunidad a Asignar--" id="administrator" value={communityId} options={cList}
onChange={handleCommunities} required autoFocus className={classNames({ 'p-invalid': submitted && !communityId })} />
2022-07-25 22:08:00 +00:00
</div>
{submitted && !communityId && <small className="p-invalid">Comunidad es requirida.</small>}
</div>
2022-07-25 06:25:39 +00:00
</div>
2022-07-25 22:08:00 +00:00
<Button label="Registrar" onClick={saveAdminCommunity} />
2022-07-25 06:25:39 +00:00
</div>
</div>
</div>
</div>
2022-07-27 04:03:08 +00:00
);
2022-07-25 04:38:48 +00:00
};
export default React.memo(AdministradoresComunidad);