|
|
@@ -1,13 +1,50 @@
|
|
|
-import React, { FC } from "react";
|
|
|
+import React, { FC, useState, useEffect, useContext } from "react";
|
|
|
+import { monthlyDegrees } from "../../../api";
|
|
|
+import { UserStateContext } from "../../../context/auth/AuthProvider";
|
|
|
+import { StateContext } from "../../../context/dashboard/Provider";
|
|
|
+import { DegreeDays } from "../../../types";
|
|
|
import * as classes from "../tables.module.css";
|
|
|
|
|
|
-interface DegreeDayProps {
|
|
|
+interface DegreesProps {
|
|
|
title: String;
|
|
|
periodString: String;
|
|
|
}
|
|
|
|
|
|
-const DegreeDay: FC<DegreeDayProps> = ({ title, periodString }) => {
|
|
|
- const suffix = "Precipitaciones mensuales";
|
|
|
+// TODO: Computar el string de temporada
|
|
|
+// Consigue un string para que el usuario identifique la temporada que esta viendo
|
|
|
+const campString = (ISOFrom: string, ISOTo: string) => {
|
|
|
+ const from = new Date(Date.parse(ISOFrom));
|
|
|
+ const to = new Date(Date.parse(ISOTo));
|
|
|
+
|
|
|
+ return `${ISOFrom.slice(0, 10)} / ${ISOTo.slice(0, 10)}`;
|
|
|
+};
|
|
|
+
|
|
|
+const DegreeDay: FC<DegreesProps> = ({ title, periodString }) => {
|
|
|
+ const [data, setData] = useState<DegreeDays[] | null>(null);
|
|
|
+ const { userToken } = useContext(UserStateContext);
|
|
|
+ const { from, to, sector } = useContext(StateContext);
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (!userToken || !sector) return;
|
|
|
+ monthlyDegrees(from, to, sector, userToken).then(setData);
|
|
|
+ }, [from, to, userToken, sector]);
|
|
|
+
|
|
|
+ const suffix = "Grados días promedio mensuales";
|
|
|
+
|
|
|
+ const rows = data?.map((x) => (
|
|
|
+ <tr key={x.initial_date}>
|
|
|
+ <th className={classes.cell}>
|
|
|
+ {campString(x.initial_date, x.final_date)}
|
|
|
+ </th>
|
|
|
+ <td className={classes.cell}>{x.months[10] ?? "-"}</td>
|
|
|
+ <td className={classes.cell}>{x.months[11] ?? "-"}</td>
|
|
|
+ <td className={classes.cell}>{x.months[12] ?? "-"}</td>
|
|
|
+ <td className={classes.cell}>{x.months[1] ?? "-"}</td>
|
|
|
+ <td className={classes.cell}>{x.months[2] ?? "-"}</td>
|
|
|
+ <td className={classes.cell}>{x.months[3] ?? "-"}</td>
|
|
|
+ </tr>
|
|
|
+ ));
|
|
|
+
|
|
|
return (
|
|
|
<section>
|
|
|
<h3>{`${title} - ${suffix}`}</h3>
|
|
|
@@ -22,32 +59,22 @@ const DegreeDay: FC<DegreeDayProps> = ({ title, periodString }) => {
|
|
|
<td className={classes.cell}>Enero</td>
|
|
|
<td className={classes.cell}>Febrero</td>
|
|
|
<td className={classes.cell}>Marzo</td>
|
|
|
- <td className={classes.cell}>Precipitaciones acumuladas [mm]</td>
|
|
|
</tr>
|
|
|
</thead>
|
|
|
- <tbody>
|
|
|
- <tr>
|
|
|
- <th className={classes.cell}>Temporada 2020-2021-Vendimia 2021</th>
|
|
|
- <td className={classes.cell}>4,44</td>
|
|
|
- <td className={classes.cell}>5,02</td>
|
|
|
- <td className={classes.cell}>11,02</td>
|
|
|
- <td className={classes.cell}>14,02</td>
|
|
|
- <td className={classes.cell}>12,02</td>
|
|
|
- <td className={classes.cell}>8,02</td>
|
|
|
- <td className={classes.cell}>205</td>
|
|
|
- </tr>
|
|
|
- <tr>
|
|
|
- <th className={classes.cell}>Temporada 2020-2021-Vendimia 2021</th>
|
|
|
- <td className={classes.cell}>4,44</td>
|
|
|
- <td className={classes.cell}>5,02</td>
|
|
|
- <td className={classes.cell}>11,02</td>
|
|
|
- <td className={classes.cell}>14,02</td>
|
|
|
- <td className={classes.cell}>12,02</td>
|
|
|
- <td className={classes.cell}>8,02</td>
|
|
|
- <td className={classes.cell}>440</td>
|
|
|
- </tr>
|
|
|
- </tbody>
|
|
|
+ <tbody>{data && rows}</tbody>
|
|
|
</table>
|
|
|
+ {!data && (
|
|
|
+ <div className="d-flex py-3 justify-content-center">
|
|
|
+ <div className="spinner-border" role="status">
|
|
|
+ <span className="visually-hidden">Loading...</span>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ )}
|
|
|
+ {data && data.length === 0 && (
|
|
|
+ <div className="d-flex py-3 justify-content-center">
|
|
|
+ <p>Sin datos</p>
|
|
|
+ </div>
|
|
|
+ )}
|
|
|
</section>
|
|
|
);
|
|
|
};
|