|
@@ -16,11 +16,7 @@ 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";
|
|
|
import { Station } from "../../../../types";
|
|
import { Station } from "../../../../types";
|
|
|
-
|
|
|
|
|
-const campaignList: any[] = ["2018", "2019", "2020", "2021"].map((c) => ({
|
|
|
|
|
- value: c,
|
|
|
|
|
- title: c,
|
|
|
|
|
-}));
|
|
|
|
|
|
|
+import { mustCheckDateOrder } from "../../../../utils";
|
|
|
|
|
|
|
|
const Cockpit: FC = () => {
|
|
const Cockpit: FC = () => {
|
|
|
const dashboardState = useContext(StateContext);
|
|
const dashboardState = useContext(StateContext);
|
|
@@ -28,6 +24,18 @@ const Cockpit: FC = () => {
|
|
|
const userState = useContext(UserStateContext);
|
|
const userState = useContext(UserStateContext);
|
|
|
const [sectorList, setSectorList] = useState<Station[]>([]);
|
|
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(() => {
|
|
useEffect(() => {
|
|
|
const token = userState.userToken;
|
|
const token = userState.userToken;
|
|
@@ -49,8 +57,24 @@ const Cockpit: FC = () => {
|
|
|
[sectorList]
|
|
[sectorList]
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
|
|
+ const handleCampChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
+ const camp = campMaxMin(e.target.value);
|
|
|
|
|
+ dashboardDispatch(actions.setMinMaxDate(camp.min, camp.max));
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const handleFromChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
+ mustCheckDateOrder(e.target.value, dashboardState.to);
|
|
|
|
|
+ return dashboardDispatch(actions.setFromControl(e.target.value));
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const handleToChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
+ 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">
|
|
<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">
|
|
<div className="col-12 col-lg-4 mb-2 col-xl-3 mb-xl-0">
|
|
|
<Select
|
|
<Select
|
|
|
list={sectorChoices}
|
|
list={sectorChoices}
|
|
@@ -61,40 +85,65 @@ const Cockpit: FC = () => {
|
|
|
placeholder="Fincas"
|
|
placeholder="Fincas"
|
|
|
/>
|
|
/>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* Campania */}
|
|
|
<div className="col-6 col-lg-4 mb-2 col-xl-auto mb-xl-0">
|
|
<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)}
|
|
|
|
|
|
|
+ <Select
|
|
|
|
|
+ list={campaignList}
|
|
|
|
|
+ onChange={handleCampChange}
|
|
|
|
|
+ value={selectedCampaignYear}
|
|
|
name="Comparación"
|
|
name="Comparación"
|
|
|
|
|
+ placeholder="Camapaña"
|
|
|
/>
|
|
/>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* Desde */}
|
|
|
<div className="col-6 col-lg-4 mb-2 col-xl-auto mb-xl-0">
|
|
<div className="col-6 col-lg-4 mb-2 col-xl-auto mb-xl-0">
|
|
|
<CalendarInput
|
|
<CalendarInput
|
|
|
- onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
|
|
|
|
- dashboardDispatch(
|
|
|
|
|
- actions.setToControl(e.target.valueAsDate?.toISOString() ?? "")
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
- value={dashboardState.to.slice(0, 10)}
|
|
|
|
|
|
|
+ onChange={handleFromChange}
|
|
|
|
|
+ value={dashboardState.from}
|
|
|
name="Comparación"
|
|
name="Comparación"
|
|
|
|
|
+ min={dashboardState.minDate}
|
|
|
|
|
+ max={dashboardState.maxDate}
|
|
|
/>
|
|
/>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* Hasta */}
|
|
|
<div className="col-6 col-lg-4 mb-2 col-xl-auto mb-xl-0">
|
|
<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}
|
|
|
|
|
|
|
+ <CalendarInput
|
|
|
|
|
+ onChange={handleToChange}
|
|
|
|
|
+ value={dashboardState.to}
|
|
|
name="Comparación"
|
|
name="Comparación"
|
|
|
- placeholder="Año historicos"
|
|
|
|
|
|
|
+ min={dashboardState.minDate}
|
|
|
|
|
+ max={dashboardState.maxDate}
|
|
|
/>
|
|
/>
|
|
|
</div>
|
|
</div>
|
|
|
</section>
|
|
</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;
|