index.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React, { FC, useEffect, useContext, useState } from "react";
  2. import { seasonsSummariesTable } from "../../../api/tables";
  3. import { Summary } from "../../../types/summary";
  4. import { TableHeader as Header, TdGroup } from "../shared";
  5. import * as classes from "../tables.module.css";
  6. import { StateContext as DashboardContext } from "../../../context/dashboard/Provider";
  7. import { UserStateContext } from "../../../context/auth/AuthProvider";
  8. import { campString, grayScale, greenScale, yellowScale } from "../../../utils";
  9. import { redScale, orangeScale, blueScale } from "../../../utils";
  10. const TemperaturePerSeason: FC = () => {
  11. const [data, setData] = useState<Summary[] | null>(null);
  12. const { sector, from, to } = useContext(DashboardContext);
  13. const { userToken } = useContext(UserStateContext);
  14. useEffect(() => {
  15. if (!sector || !userToken) return;
  16. seasonsSummariesTable(from, to, sector, userToken).then(setData);
  17. }, [from, to, sector, userToken]);
  18. const rows = data?.map((x) => (
  19. <tr key={x.initial_date}>
  20. <th className={`${classes.cell} ${classes.nowrap}`}>
  21. {campString(x.initial_date, x.final_date)}
  22. </th>
  23. <TdGroup>
  24. <td
  25. style={{ backgroundColor: blueScale([0, 50])(x.lt10).css() as any }}
  26. >
  27. {x.lt10 ?? "-"}%
  28. </td>
  29. <td
  30. style={{ backgroundColor: orangeScale([0, 50])(x.gt33).css() as any }}
  31. >
  32. {x.gt30 ?? "-"}%
  33. </td>
  34. <td style={{ backgroundColor: redScale([0, 50])(x.gt33).css() as any }}>
  35. {x.gt33 ?? "-"}%
  36. </td>
  37. </TdGroup>
  38. <td
  39. className={classes.cell}
  40. style={{
  41. backgroundColor: greenScale([0, 3000])(x.grados_acumulados).css(),
  42. }}
  43. >
  44. {x.grados_acumulados ?? "-"}
  45. </td>
  46. <td className={classes.cell}>{x.dias_igualar_temporada}</td>
  47. <td
  48. className={classes.cell}
  49. style={{
  50. backgroundColor: yellowScale([0, 50])(x.amplitud_termica).css(),
  51. }}
  52. >
  53. {x.amplitud_termica ?? "-"}
  54. </td>
  55. <td
  56. className={classes.cell}
  57. style={{
  58. backgroundColor: grayScale([0, 300])(x.precip_acumulada).css(),
  59. }}
  60. >
  61. {x.precip_acumulada ?? "-"}
  62. </td>
  63. </tr>
  64. ));
  65. return (
  66. <>
  67. <h5>General - {data ? data[0].station.title : null}</h5>
  68. <table className={`${classes.table} ${classes.widthSix}`}>
  69. <Header daysToMatchCurrentTemperature={true} />
  70. <tbody>{data && rows}</tbody>
  71. </table>
  72. {!data && (
  73. <div className="d-flex py-3 justify-content-center">
  74. <div className="spinner-border" role="status">
  75. <span className="visually-hidden">Loading...</span>
  76. </div>
  77. </div>
  78. )}
  79. {data && data.length === 0 && (
  80. <div className="d-flex py-3 justify-content-center">
  81. <p>Sin datos</p>
  82. </div>
  83. )}
  84. </>
  85. );
  86. };
  87. export default TemperaturePerSeason;