ResultadoEventoAccion.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import React, { useEffect, useState } from 'react';
  2. import { Row, Col, Table, Divider, Tooltip } from "antd";
  3. import {
  4. RedoOutlined,
  5. FileExcelOutlined,
  6. ArrowLeftOutlined,
  7. CheckCircleOutlined,
  8. } from "@ant-design/icons";
  9. import 'moment/locale/es-mx';
  10. import { useQuery } from "../../../hooks";
  11. import { firestore } from "../../../services/firebase";
  12. import { useHistory } from 'react-router-dom';
  13. import { ViewLoading } from '../../../components';
  14. import exportarExcelJS from '../../../services/exportarExcel';
  15. import { doc, getDoc } from "firebase/firestore";
  16. import { SimpleTableLayout } from '../../../components/layouts';
  17. const ResultadoEventoAccion = () => {
  18. const query = useQuery();
  19. const id = query.get("id");
  20. const url = "/administracion/eventos";
  21. const history = useHistory();
  22. const [ evento, setEvento ] = useState(null);
  23. const [ filteredResults, setFilteredResults ] = useState([])
  24. const [ searchInput, setSearchInput ] = useState('');
  25. const defaultText = prop => prop || <span style={{ color: '#c7c3c3' }}>---</span>;
  26. const RenderAccion = (item, val) => {
  27. const urlEvento = `https://laud-red.web.app/evento-laud?id=${id}&uid=${item?.uid}`;
  28. const tel = '+52'+item?.telefono;
  29. const waParams1 = `Hola, aún falta tu participación en el siguiente evento en ${evento?.redSocial}: ${urlEvento}`;
  30. const waParams2 = `Hola, gracias por haber participado en el evento de ${evento?.redSocial }: *"${evento?.nombre}"*`;
  31. const url1 = `https://wa.me/${tel}?text=${encodeURIComponent(waParams1)}`;
  32. const url2 = `https://wa.me/${tel}?text=${encodeURIComponent(waParams2)}`;
  33. if( evento?.resultado[item?.uid]?.includes(val) ) {
  34. return (
  35. <Tooltip
  36. title="Se verificó participación."
  37. onClick={() => window.open(url2, '_blank')}
  38. >
  39. <CheckCircleOutlined style={{ color: '#33ca3a', fontSize: 24 }} />
  40. </Tooltip>
  41. )
  42. } else {
  43. return (
  44. <Tooltip
  45. title="Reenviar enlace del evento."
  46. onClick={() => window.open(url1, '_blank')}
  47. >
  48. <RedoOutlined style={{ color: '#c7c3c3', fontSize: 24 }} />
  49. </Tooltip>
  50. )
  51. }
  52. };
  53. const columns = [
  54. {
  55. title: "Usuario",
  56. dataIndex: "nombre",
  57. key: "nombre",
  58. render: defaultText,
  59. },
  60. {
  61. title: "Recibió",
  62. dataIndex: "recibio",
  63. key: "recibio",
  64. align: 'center',
  65. render: (_, item )=> RenderAccion(item, "recibio")
  66. },
  67. {
  68. title: "Click",
  69. dataIndex: "click",
  70. key: "click",
  71. align: 'center',
  72. render: (_, item )=> RenderAccion(item, "click")
  73. },
  74. {
  75. title: "Me gusta",
  76. dataIndex: "likes",
  77. key: "likes",
  78. align: 'center',
  79. render: (_, item )=> RenderAccion(item, "likes")
  80. },
  81. {
  82. title: "Compartir",
  83. dataIndex: "shares",
  84. key: "shares",
  85. align: 'center',
  86. render: (_, item )=> RenderAccion(item, "shares")
  87. },
  88. {
  89. title: "Comentario",
  90. dataIndex: "comments",
  91. key: "comments",
  92. align: 'center',
  93. render: (_, item )=> RenderAccion(item, "comments")
  94. },
  95. {
  96. title: "Retweets",
  97. dataIndex: "retweets",
  98. key: "retweets",
  99. align: 'center',
  100. render: (_, item )=> RenderAccion(item, "retweets")
  101. },
  102. ];
  103. const multipleButtonData = [
  104. {
  105. text: "Volver",
  106. to: () => history.push(url),
  107. icon: <ArrowLeftOutlined />,
  108. props: { disabled: false, type: 'primary', }
  109. },
  110. {
  111. text: "Generar reporte",
  112. to: () => generarReporte(),
  113. icon: <FileExcelOutlined />,
  114. props: { disabled: false, style: { background: '#107038', color: '#fff', border: 'none' } }
  115. }
  116. ];
  117. const onSearch = (searchValue) => {
  118. setSearchInput(searchValue);
  119. if( searchValue !== '' ) {
  120. const filteredData = evento?.usuarios?.filter((item) => {
  121. return Object.values(item).join('').toLowerCase().includes(searchValue.toLowerCase());
  122. })
  123. setFilteredResults(filteredData)
  124. } else {
  125. setFilteredResults(evento?.usuarios)
  126. }
  127. };
  128. const generarReporte = async () => {
  129. try {
  130. const datos = [];
  131. const usuarios = evento?.usuarios;
  132. for( let i = 0; i < usuarios.length; i++ ) {
  133. datos.push({
  134. nombre: usuarios[i]?.nombre,
  135. recibio: evento?.resultado[usuarios[i].uid]?.includes('recibio') ? 1 : 0,
  136. click: evento?.resultado[usuarios[i].uid]?.includes('click') ? 1 : 0,
  137. likes: evento?.resultado[usuarios[i].uid]?.includes('likes') ? 1 : 0,
  138. shares: evento?.resultado[usuarios[i].uid]?.includes('shares') ? 1 : 0,
  139. comments: evento?.resultado[usuarios[i].uid]?.includes('comments') ? 1 : 0,
  140. retweets: evento?.resultado[usuarios[i].uid]?.includes('retweets') ? 1 : 0,
  141. })
  142. }
  143. await exportarExcelJS(
  144. columns,
  145. datos,
  146. 'reporte evento acción',
  147. evento?.nombre,
  148. `Descripción: ${evento?.descripcion} - Red social: ${evento?.redSocial} `,
  149. '/LAUD-Logotipo.png',
  150. ""
  151. )
  152. } catch (error) {
  153. console.log('error al generar reporte: ', error)
  154. }
  155. };
  156. // Obtener detalles del evento.
  157. useEffect(() => {
  158. let mounted = true;
  159. if( mounted ) {
  160. try {
  161. setTimeout( async () => {
  162. const docRef = doc(firestore, "eventos", id);
  163. const docSnap = await getDoc(docRef);
  164. setEvento(docSnap.data());
  165. }, 0);
  166. } catch (error) {
  167. console.log('error al obtener evento: ', error);
  168. }
  169. };
  170. return () => mounted = false;
  171. }, [id] );
  172. if( !evento ) return <ViewLoading />;
  173. return (
  174. <SimpleTableLayout
  175. title="Resultados por acciones"
  176. multipleButtonData={multipleButtonData}
  177. onSearchClicked={onSearch}
  178. >
  179. <>
  180. <Row gutter={[10, 10]}>
  181. <Col span={20}>
  182. <h2>{evento?.nombre}</h2>
  183. </Col>
  184. <Col span={4} style={{ textAlign: 'right' }}>
  185. <img src={ evento ? '/assets/'+evento?.redSocial+'.png' : '' } width='50' alt="Red social" />
  186. </Col>
  187. </Row>
  188. <Divider />
  189. <Row gutter={[10, 10]}>
  190. <Col span={24}>
  191. <Table
  192. rowKey={'id'}
  193. pagination={false}
  194. size="small"
  195. columns={columns}
  196. dataSource={ searchInput.length > 1 ? filteredResults : evento?.usuarios?.sort((a, b) => a?.nombre > b?.nombre ? 1 : -1) }
  197. />
  198. </Col>
  199. </Row>
  200. </>
  201. </SimpleTableLayout>
  202. )
  203. }
  204. export default ResultadoEventoAccion