|
@@ -1,4 +1,11 @@
|
|
|
-import React, { useContext, useState, useEffect, ChangeEvent, FC } from "react";
|
|
|
|
|
|
|
+import React, {
|
|
|
|
|
+ useContext,
|
|
|
|
|
+ useState,
|
|
|
|
|
+ useEffect,
|
|
|
|
|
+ ChangeEvent,
|
|
|
|
|
+ FC,
|
|
|
|
|
+ useMemo,
|
|
|
|
|
+} from "react";
|
|
|
import Select from "../../forms/select";
|
|
import Select from "../../forms/select";
|
|
|
import CalendarInput from "../../forms/calendarInput";
|
|
import CalendarInput from "../../forms/calendarInput";
|
|
|
import {
|
|
import {
|
|
@@ -8,91 +15,161 @@ import {
|
|
|
import * as actions from "../../../../context/dashboard/actions";
|
|
import * as actions from "../../../../context/dashboard/actions";
|
|
|
import { sectors } from "../../../../api";
|
|
import { sectors } from "../../../../api";
|
|
|
import { UserStateContext } from "../../../../context/auth/AuthProvider";
|
|
import { UserStateContext } from "../../../../context/auth/AuthProvider";
|
|
|
-
|
|
|
|
|
-const fincaList: string[] | number[] = ["a", "b", "c", "d"];
|
|
|
|
|
-const campaignList: any[] = ["2018", "2019", "2020", "2021"].map((c) => ({
|
|
|
|
|
- value: c,
|
|
|
|
|
- title: c,
|
|
|
|
|
-}));
|
|
|
|
|
|
|
+import { Station } from "../../../../types";
|
|
|
|
|
+import { mustCheckDateOrder } from "../../../../utils";
|
|
|
|
|
+import Modal from "../../../portals/modal";
|
|
|
|
|
|
|
|
const Cockpit: FC = () => {
|
|
const Cockpit: FC = () => {
|
|
|
|
|
+ const [error, setError] = useState<string | null>(null);
|
|
|
|
|
+
|
|
|
const dashboardState = useContext(StateContext);
|
|
const dashboardState = useContext(StateContext);
|
|
|
const dashboardDispatch = useContext(DispatchContext);
|
|
const dashboardDispatch = useContext(DispatchContext);
|
|
|
const userState = useContext(UserStateContext);
|
|
const userState = useContext(UserStateContext);
|
|
|
- const [sectorList, setSectorList] = useState<any[]>([]);
|
|
|
|
|
|
|
+ const [sectorList, setSectorList] = useState<Station[]>([]);
|
|
|
|
|
+
|
|
|
|
|
+ // Opciones para selector de campania
|
|
|
|
|
+ const selectedCampaignYear = dashboardState.maxDate.slice(0, 4);
|
|
|
|
|
+ const lastCampaignYear = useMemo(
|
|
|
|
|
+ () => dashboardState.maxDate.slice(0, 4),
|
|
|
|
|
+ []
|
|
|
|
|
+ );
|
|
|
|
|
+ const campaignListLength = 4;
|
|
|
|
|
+ const campaignList = useMemo(
|
|
|
|
|
+ () => campList(lastCampaignYear, campaignListLength),
|
|
|
|
|
+ []
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
// Inicializacion del selector de fincas
|
|
// Inicializacion del selector de fincas
|
|
|
- // useEffect(() => {
|
|
|
|
|
- // const token = userState.userToken;
|
|
|
|
|
- // if (!token) return;
|
|
|
|
|
- // sectors(token)
|
|
|
|
|
- // .then((res: Response) => res.json())
|
|
|
|
|
- // .then((body: any[]) => {
|
|
|
|
|
- // debugger;
|
|
|
|
|
- // const options = body.map((s: any) => ({
|
|
|
|
|
- // title: s.title,
|
|
|
|
|
- // value: s.station_code,
|
|
|
|
|
- // }));
|
|
|
|
|
-
|
|
|
|
|
- // if (options.length) {
|
|
|
|
|
- // dashboardDispatch(actions.setSector(options[0].value));
|
|
|
|
|
- // }
|
|
|
|
|
- // setSectorList(options);
|
|
|
|
|
- // });
|
|
|
|
|
- // }, []);
|
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ const token = userState.userToken;
|
|
|
|
|
+ if (!token) return;
|
|
|
|
|
+ sectors(token).then((list) => {
|
|
|
|
|
+ setSectorList(list);
|
|
|
|
|
+ if (list.length > 0) {
|
|
|
|
|
+ dashboardDispatch(actions.setSector(list[0].station_code));
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }, []);
|
|
|
|
|
+
|
|
|
|
|
+ const sectorChoices = useMemo(
|
|
|
|
|
+ () =>
|
|
|
|
|
+ sectorList.map(({ title, station_code }) => ({
|
|
|
|
|
+ title,
|
|
|
|
|
+ value: station_code,
|
|
|
|
|
+ })),
|
|
|
|
|
+ [sectorList]
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ const handleCampChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
+ const camp = campMaxMin(e.target.value);
|
|
|
|
|
+ dashboardDispatch(actions.setMinMaxDate(camp.min, camp.max));
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const handleFromChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ mustCheckDateOrder(e.target.value, dashboardState.to);
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ setError(e as any);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ return dashboardDispatch(actions.setFromControl(e.target.value));
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const handleToChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ mustCheckDateOrder(e.target.value, dashboardState.to);
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ setError(e as any);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ mustCheckDateOrder(e.target.value, dashboardState.to);
|
|
|
|
|
+ return dashboardDispatch(actions.setToControl(e.target.value));
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
- <section className="row p-lg-4 p-md-3 p-2">
|
|
|
|
|
- <div className="col-12 col-lg-4 mb-2 col-xl-3 mb-xl-0">
|
|
|
|
|
- <Select
|
|
|
|
|
- list={sectorList}
|
|
|
|
|
- onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
|
|
|
|
- dashboardDispatch(actions.setSector(e.target.value))
|
|
|
|
|
- }
|
|
|
|
|
- name="Comparación"
|
|
|
|
|
- placeholder="Fincas"
|
|
|
|
|
- />
|
|
|
|
|
- </div>
|
|
|
|
|
- <div className="col-6 col-lg-4 mb-2 col-xl-auto mb-xl-0">
|
|
|
|
|
- <CalendarInput
|
|
|
|
|
- onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
|
|
|
|
- dashboardDispatch(
|
|
|
|
|
- actions.setFromControl(e.target.valueAsDate?.toISOString() ?? "")
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
- value={dashboardState.from.slice(0, 10)}
|
|
|
|
|
- name="Comparación"
|
|
|
|
|
- />
|
|
|
|
|
- </div>
|
|
|
|
|
- <div className="col-6 col-lg-4 mb-2 col-xl-auto mb-xl-0">
|
|
|
|
|
- <CalendarInput
|
|
|
|
|
- onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
|
|
|
|
- dashboardDispatch(
|
|
|
|
|
- actions.setToControl(e.target.valueAsDate?.toISOString() ?? "")
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
- value={dashboardState.to.slice(0, 10)}
|
|
|
|
|
- name="Comparación"
|
|
|
|
|
- />
|
|
|
|
|
- </div>
|
|
|
|
|
- <div className="col-6 col-lg-4 mb-2 col-xl-auto mb-xl-0">
|
|
|
|
|
- <Select
|
|
|
|
|
- list={campaignList}
|
|
|
|
|
- onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
|
|
|
|
- dashboardDispatch(actions.setYearControl(e.target.value))
|
|
|
|
|
- }
|
|
|
|
|
- value={dashboardState.year}
|
|
|
|
|
- name="Comparación"
|
|
|
|
|
- placeholder="Año historicos"
|
|
|
|
|
- />
|
|
|
|
|
- </div>
|
|
|
|
|
-
|
|
|
|
|
- {/* <div className="col-auto"> */}
|
|
|
|
|
- {/* <button type="button" className="btn btn-primary"> */}
|
|
|
|
|
- {/* Aplicar */}
|
|
|
|
|
- {/* </button> */}
|
|
|
|
|
- {/* </div> */}
|
|
|
|
|
- </section>
|
|
|
|
|
|
|
+ <>
|
|
|
|
|
+ {error && (
|
|
|
|
|
+ <Modal
|
|
|
|
|
+ key="Error"
|
|
|
|
|
+ title={"Error"}
|
|
|
|
|
+ staticBackdrop
|
|
|
|
|
+ accept={() => setError(null)}
|
|
|
|
|
+ >
|
|
|
|
|
+ <p style={{ color: "red" }}>{error.toString()}</p>
|
|
|
|
|
+ </Modal>
|
|
|
|
|
+ )}
|
|
|
|
|
+ <section className="row p-lg-4 p-md-3 p-2">
|
|
|
|
|
+ {/* Finca */}
|
|
|
|
|
+ <div className="col-12 col-lg-4 mb-2 col-xl-3 mb-xl-0">
|
|
|
|
|
+ <Select
|
|
|
|
|
+ list={sectorChoices}
|
|
|
|
|
+ onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
|
|
|
|
+ dashboardDispatch(actions.setSector(e.target.value))
|
|
|
|
|
+ }
|
|
|
|
|
+ name="Comparación"
|
|
|
|
|
+ placeholder="Fincas"
|
|
|
|
|
+ />
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* Campania */}
|
|
|
|
|
+ <div className="col-6 col-lg-4 mb-2 col-xl-auto mb-xl-0">
|
|
|
|
|
+ <Select
|
|
|
|
|
+ list={campaignList}
|
|
|
|
|
+ onChange={handleCampChange}
|
|
|
|
|
+ value={selectedCampaignYear}
|
|
|
|
|
+ name="Comparación"
|
|
|
|
|
+ placeholder="Camapaña"
|
|
|
|
|
+ />
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* Desde */}
|
|
|
|
|
+ <div className="col-6 col-lg-4 mb-2 col-xl-auto mb-xl-0">
|
|
|
|
|
+ <CalendarInput
|
|
|
|
|
+ onChange={handleFromChange}
|
|
|
|
|
+ value={dashboardState.from}
|
|
|
|
|
+ name="Comparación"
|
|
|
|
|
+ min={dashboardState.minDate}
|
|
|
|
|
+ max={dashboardState.maxDate}
|
|
|
|
|
+ />
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* Hasta */}
|
|
|
|
|
+ <div className="col-6 col-lg-4 mb-2 col-xl-auto mb-xl-0">
|
|
|
|
|
+ <CalendarInput
|
|
|
|
|
+ onChange={handleToChange}
|
|
|
|
|
+ value={dashboardState.to}
|
|
|
|
|
+ name="Comparación"
|
|
|
|
|
+ min={dashboardState.minDate}
|
|
|
|
|
+ max={dashboardState.maxDate}
|
|
|
|
|
+ />
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </section>
|
|
|
|
|
+ </>
|
|
|
);
|
|
);
|
|
|
};
|
|
};
|
|
|
|
|
+
|
|
|
|
|
+const campMaxMin = (year: string) => {
|
|
|
|
|
+ // Las fechas extremas de la temporada de este anio
|
|
|
|
|
+ let maxDate = new Date(parseInt(year), 2, 31);
|
|
|
|
|
+ let minDate = new Date(parseInt(year) - 1, 9, 1);
|
|
|
|
|
+
|
|
|
|
|
+ // Conseguimos el string yyyy-mm-dd
|
|
|
|
|
+ const max = maxDate.toISOString().slice(0, 10);
|
|
|
|
|
+ const min = minDate.toISOString().slice(0, 10);
|
|
|
|
|
+
|
|
|
|
|
+ return { min, max };
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+// Computa las campanias para seleccionar
|
|
|
|
|
+// a partir de la ultima campania y la cantidad de anios para atras
|
|
|
|
|
+const campList = (lastCampaignYear: string, length: number) =>
|
|
|
|
|
+ Array.from({
|
|
|
|
|
+ length,
|
|
|
|
|
+ }).map((_, i) => {
|
|
|
|
|
+ const year = parseInt(lastCampaignYear) - length + 1 + i;
|
|
|
|
|
+ return {
|
|
|
|
|
+ value: year,
|
|
|
|
|
+ title: year,
|
|
|
|
|
+ };
|
|
|
|
|
+ });
|
|
|
export default Cockpit;
|
|
export default Cockpit;
|