|
|
@@ -1,19 +1,21 @@
|
|
|
-import React, { useState, useEffect } from "react";
|
|
|
+import React, { useState, useEffect, useMemo } from "react";
|
|
|
import { Form, Button, Row, Col, Input, Modal, Table, Typography, Select } from "antd";
|
|
|
import { SaveOutlined, PlusOutlined, CloseCircleOutlined } from "@ant-design/icons";
|
|
|
-import { useQuery, useModels } from "../../../hooks";
|
|
|
+import { useQuery, useModels, useModel } from "../../../hooks";
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
-import { firestore } from "../../../services/firebase";
|
|
|
+// import { firestore } from "../../../services/firebase";
|
|
|
import { ViewLoading } from "../../../components";
|
|
|
import { DefaultLayout } from "../../../components/layouts";
|
|
|
-import { addDoc, collection, doc, getDoc, updateDoc } from "firebase/firestore";
|
|
|
+// import { addDoc, collection, doc, getDoc, updateDoc } from "firebase/firestore";
|
|
|
import HttpService from "../../../services/httpService";
|
|
|
+import { emptyRequest } from "../../../constants/requests";
|
|
|
+import { respuestas } from "../../../utilities";
|
|
|
|
|
|
const { Title } = Typography;
|
|
|
|
|
|
const DependenciaDetalle = () => {
|
|
|
|
|
|
- const firebaseCollection = "dependencias";
|
|
|
+ // const firebaseCollection = "dependencias";
|
|
|
const query = useQuery();
|
|
|
const history = useHistory();
|
|
|
const { TextArea } = Input;
|
|
|
@@ -23,8 +25,8 @@ const DependenciaDetalle = () => {
|
|
|
const url = "/administracion/dependencias";
|
|
|
const [form] = Form.useForm();
|
|
|
|
|
|
- const [dependencia, setDependencia] = useState([]);
|
|
|
- const [cargandoDependencia, setCargandoDependencia] = useState(true);
|
|
|
+ // const [dependencia, setDependencia] = useState([]);
|
|
|
+ // const [cargandoDependencia, setCargandoDependencia] = useState(true);
|
|
|
const [guadandoCargando, setGuardandoCargando] = useState(false);
|
|
|
const [buscarValue, setBuscarValue] = useState('');
|
|
|
const [requestUsuarios, setRequestUsuarios] = useState({});
|
|
|
@@ -32,7 +34,15 @@ const DependenciaDetalle = () => {
|
|
|
const [miembros, setMiembros] = useState([]);
|
|
|
const [miembrosLoading, setMiembrosLoading] = useState(false);
|
|
|
const [usuario, setUsuario] = useState(null);
|
|
|
+ const [miembro, setMiembro] = useState(null);
|
|
|
const [lideres, setLideres] = useState([]);
|
|
|
+ const [request, setRequest] = useState(emptyRequest);
|
|
|
+
|
|
|
+ const requestParams = useMemo(() => ({
|
|
|
+ name: 'v1/dependencia',
|
|
|
+ id: id,
|
|
|
+ expand: 'lideres,usuarios'
|
|
|
+ }), [id]);
|
|
|
|
|
|
const usuarioExtraParams = React.useMemo(() => ({ q: buscarValue }), [buscarValue])
|
|
|
const usuariosParams = React.useMemo(() => ({
|
|
|
@@ -43,6 +53,11 @@ const DependenciaDetalle = () => {
|
|
|
extraParams: usuarioExtraParams
|
|
|
}), [usuarioExtraParams])
|
|
|
|
|
|
+ const {
|
|
|
+ model,
|
|
|
+ modelLoading
|
|
|
+ } = useModel(request);
|
|
|
+
|
|
|
const [
|
|
|
models,
|
|
|
modelsLoading, , ,
|
|
|
@@ -51,16 +66,24 @@ const DependenciaDetalle = () => {
|
|
|
const columnasMiembros = [
|
|
|
{ title: 'Nombre', dataIndex: 'nombre', key: 'nombre', },
|
|
|
{ title: 'Correo', dataIndex: 'email', key: 'email', },
|
|
|
+ {
|
|
|
+ title: '', dataIndex: 'id', key: 'id', render: (text) =>
|
|
|
+ <Button
|
|
|
+ danger
|
|
|
+ icon={<CloseCircleOutlined />}
|
|
|
+ onClick={() => setMiembros(miembros.filter(l => l.id !== text))}
|
|
|
+ />
|
|
|
+ },
|
|
|
]
|
|
|
|
|
|
const columnasLideres = [
|
|
|
{ title: 'Nombre', dataIndex: 'nombre', key: 'nombre', },
|
|
|
{
|
|
|
- title: '', dataIndex: 'uid', key: 'uid', render: (text) =>
|
|
|
+ title: '', dataIndex: 'id', key: 'id', render: (text) =>
|
|
|
<Button
|
|
|
danger
|
|
|
icon={<CloseCircleOutlined />}
|
|
|
- onClick={() => setLideres(lideres.filter(l => l.uid !== text))}
|
|
|
+ onClick={() => setLideres(lideres.filter(l => l.id !== text))}
|
|
|
/>
|
|
|
},
|
|
|
]
|
|
|
@@ -74,55 +97,47 @@ const DependenciaDetalle = () => {
|
|
|
setTimer(newTimer);
|
|
|
};
|
|
|
|
|
|
- React.useEffect(() => {
|
|
|
- setRequestUsuarios(usuariosParams);
|
|
|
- return () => setRequestUsuarios({});
|
|
|
- }, [usuariosParams]);
|
|
|
-
|
|
|
- React.useEffect(() => {
|
|
|
- if (editando && id) {
|
|
|
- const getMiembros = async () => {
|
|
|
- setMiembrosLoading(true);
|
|
|
- const response = await HttpService.get(`v1/usuario?ordenar=nombre-asc&idDependencia=${id}&limite=-1`);
|
|
|
- if (!response.isError) {
|
|
|
- setMiembros(response.resultado);
|
|
|
- setMiembrosLoading(false);
|
|
|
- }
|
|
|
- }
|
|
|
- getMiembros();
|
|
|
- }
|
|
|
- }, [editando, id]);
|
|
|
-
|
|
|
const onFinish = async (values) => {
|
|
|
try {
|
|
|
setGuardandoCargando(true);
|
|
|
|
|
|
let body = {
|
|
|
...values,
|
|
|
- timestamp: editando ? dependencia?.timestamp : new Date(),
|
|
|
- estatus: editando ? dependencia?.estatus : true,
|
|
|
+ // timestamp: editando ? dependencia?.timestamp : new Date(),
|
|
|
+ // estatus: editando ? dependencia?.estatus : true,
|
|
|
sincronizado: null,
|
|
|
- usuarios: lideres.map(l => ({
|
|
|
- uid: l.uid,
|
|
|
+ lideres: lideres.map(l => ({
|
|
|
+ id: l.id,
|
|
|
nombre: l.nombre,
|
|
|
})),
|
|
|
+ usuarios: miembros.map(l => ({
|
|
|
+ id: l.id,
|
|
|
+ nombre: l.nombre,
|
|
|
+ }))
|
|
|
};
|
|
|
- // comentario prueba
|
|
|
|
|
|
- // Agregamos o editamos el documento grupo
|
|
|
if (editando) {
|
|
|
- await updateDoc(doc(firestore, firebaseCollection, id), body);
|
|
|
- } else {
|
|
|
- await addDoc(collection(firestore, firebaseCollection), body);
|
|
|
+ body.id = model?.id;
|
|
|
}
|
|
|
|
|
|
- Modal.success({
|
|
|
- title: "Éxito",
|
|
|
- content: "Dependencia guardado correctamente",
|
|
|
- });
|
|
|
+ const res = await HttpService.post('v1/dependencia', body);
|
|
|
+ respuestas(res);
|
|
|
+ if (res?.status === 200) {
|
|
|
+ Modal.success({
|
|
|
+ title: "Éxito",
|
|
|
+ content: "Dependencia guardada correctamente",
|
|
|
+ });
|
|
|
|
|
|
- history.push(url);
|
|
|
+ history.push(url);
|
|
|
+ }
|
|
|
+ // comentario prueba
|
|
|
|
|
|
+ // Agregamos o editamos el documento grupo
|
|
|
+ /* if (editando) {
|
|
|
+ await updateDoc(doc(firestore, firebaseCollection, id), body);
|
|
|
+ } else {
|
|
|
+ await addDoc(collection(firestore, firebaseCollection), body);
|
|
|
+ } */
|
|
|
} catch (error) {
|
|
|
Modal.warning({
|
|
|
title: "Atención",
|
|
|
@@ -143,6 +158,30 @@ const DependenciaDetalle = () => {
|
|
|
};
|
|
|
|
|
|
useEffect(() => {
|
|
|
+ setRequest(requestParams);
|
|
|
+ return () => setRequest({});
|
|
|
+ }, [requestParams]);
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ setRequestUsuarios(usuariosParams);
|
|
|
+ return () => setRequestUsuarios({});
|
|
|
+ }, [usuariosParams]);
|
|
|
+
|
|
|
+ /* useEffect(() => {
|
|
|
+ if (editando && id) {
|
|
|
+ const getMiembros = async () => {
|
|
|
+ setMiembrosLoading(true);
|
|
|
+ const response = await HttpService.get(`v1/usuario?ordenar=nombre-asc&idDependencia=${id}&limite=-1`);
|
|
|
+ if (!response.isError) {
|
|
|
+ setMiembros(response.resultado);
|
|
|
+ setMiembrosLoading(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ getMiembros();
|
|
|
+ }
|
|
|
+ }, [editando, id]); */
|
|
|
+
|
|
|
+ /* useEffect(() => {
|
|
|
let mounted = true;
|
|
|
if (mounted && editando) {
|
|
|
try {
|
|
|
@@ -164,19 +203,25 @@ const DependenciaDetalle = () => {
|
|
|
}
|
|
|
}
|
|
|
return () => (mounted = false);
|
|
|
- }, [editando, id]);
|
|
|
+ }, [editando, id]); */
|
|
|
|
|
|
useEffect(() => {
|
|
|
- let mounted = true;
|
|
|
- if (mounted && editando) {
|
|
|
+ if (model && editando) {
|
|
|
form.setFieldsValue({
|
|
|
- ...dependencia,
|
|
|
+ ...model,
|
|
|
});
|
|
|
+
|
|
|
+ if (model?.lideres) {
|
|
|
+ setLideres(model?.lideres);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (model?.usuarios) {
|
|
|
+ setMiembros(model?.usuarios);
|
|
|
+ }
|
|
|
}
|
|
|
- return () => (mounted = false);
|
|
|
- }, [dependencia, editando, form]);
|
|
|
+ }, [editando, form, model]);
|
|
|
|
|
|
- if (cargandoDependencia && editando) return <ViewLoading />;
|
|
|
+ if (modelLoading && editando) return <ViewLoading />;
|
|
|
|
|
|
return (
|
|
|
<DefaultLayout title={titulo} multipleButtonData={[]}>
|
|
|
@@ -223,6 +268,61 @@ const DependenciaDetalle = () => {
|
|
|
<Row gutter={10}>
|
|
|
<Col className="gutter-row" span={12}>
|
|
|
<Title level={4}>Miembros</Title>
|
|
|
+ <Row>
|
|
|
+ <Col span={22}>
|
|
|
+ <Form.Item
|
|
|
+ label="Usuarios"
|
|
|
+ rules={[{
|
|
|
+ required: true,
|
|
|
+ message: 'Selecciona un usuario!',
|
|
|
+ }]}
|
|
|
+ hasFeedback
|
|
|
+ >
|
|
|
+ <Select
|
|
|
+ showSearch
|
|
|
+ onSearch={onSearchUsuarios}
|
|
|
+ allowClear
|
|
|
+ placeholder="Elige a un usuario"
|
|
|
+ optionFilterProp="children"
|
|
|
+ filterOption={(input, option) => (option?.label ?? '').toLowerCase().includes(input.toLowerCase())}
|
|
|
+ loading={modelsLoading}
|
|
|
+ onChange={(_, item) => {
|
|
|
+ setMiembro(item);
|
|
|
+ console.log("item", item);
|
|
|
+ }}
|
|
|
+ options={models?.length > 0 && models?.map(i => ({
|
|
|
+ ...i,
|
|
|
+ value: i.id,
|
|
|
+ label: i.nombre
|
|
|
+ }))}
|
|
|
+ value={miembro}
|
|
|
+ />
|
|
|
+ </Form.Item>
|
|
|
+ </Col>
|
|
|
+ <Col span={2}>
|
|
|
+ <Form.Item label=" ">
|
|
|
+ <Button
|
|
|
+ type="default"
|
|
|
+ icon={<PlusOutlined />}
|
|
|
+ shape="circle"
|
|
|
+ size="middle"
|
|
|
+ style={{
|
|
|
+ // marginTop: 70,
|
|
|
+ marginLeft: 10
|
|
|
+ }}
|
|
|
+ onClick={() => {
|
|
|
+ if (miembro) {
|
|
|
+ //agregar sólo si no existe en el array, con esto evitamos duplicados
|
|
|
+ if (!miembros.find(i => i.id === miembro.id)) {
|
|
|
+ setMiembros([...miembros, miembro]);
|
|
|
+ setMiembro(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ </Form.Item>
|
|
|
+ </Col>
|
|
|
+ </Row>
|
|
|
<Table
|
|
|
columns={columnasMiembros}
|
|
|
dataSource={miembros}
|
|
|
@@ -267,7 +367,7 @@ const DependenciaDetalle = () => {
|
|
|
}}
|
|
|
options={models?.length > 0 && models?.map(i => ({
|
|
|
...i,
|
|
|
- value: i.uid,
|
|
|
+ value: i.id,
|
|
|
label: i.nombre
|
|
|
}))}
|
|
|
value={usuario}
|
|
|
@@ -287,7 +387,7 @@ const DependenciaDetalle = () => {
|
|
|
onClick={() => {
|
|
|
if (usuario) {
|
|
|
//agregar sólo si no existe en el array, con esto evitamos duplicados
|
|
|
- if (!lideres.find(i => i.uid === usuario.uid)) {
|
|
|
+ if (!lideres.find(i => i.id === usuario.id)) {
|
|
|
setLideres([...lideres, usuario]);
|
|
|
setUsuario(null);
|
|
|
}
|
|
|
@@ -300,7 +400,7 @@ const DependenciaDetalle = () => {
|
|
|
columns={columnasLideres}
|
|
|
dataSource={lideres}
|
|
|
size="small"
|
|
|
- rowKey="uid"
|
|
|
+ rowKey="id"
|
|
|
pagination={
|
|
|
lideres.length > 10
|
|
|
? {
|