|
|
@@ -16,7 +16,7 @@ import * as actions from "../../../../context/dashboard/actions";
|
|
|
import { sectors } from "../../../../api";
|
|
|
import { UserStateContext } from "../../../../context/auth/AuthProvider";
|
|
|
import { Station } from "../../../../types";
|
|
|
-import { campString, mustCheckDateOrder } from "../../../../utils";
|
|
|
+import { campMaxMin, campString, getCampList, mustCheckDateOrder } from "../../../../utils";
|
|
|
import Modal from "../../../portals/modal";
|
|
|
|
|
|
const Cockpit: FC = () => {
|
|
|
@@ -28,16 +28,8 @@ const Cockpit: FC = () => {
|
|
|
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),
|
|
|
- []
|
|
|
- );
|
|
|
+ const selectedCampaignYear = dashboardState.year;
|
|
|
+ const campaignList = dashboardState.years.map((v, _) => ({ title: v, value: v }));
|
|
|
|
|
|
// Inicializacion del selector de fincas
|
|
|
useEffect(() => {
|
|
|
@@ -63,6 +55,7 @@ const Cockpit: FC = () => {
|
|
|
const handleCampChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
const camp = campMaxMin(e.target.value);
|
|
|
dashboardDispatch(actions.setMinMaxDate(camp.min, camp.max));
|
|
|
+ dashboardDispatch(actions.setYearControl(e.target.value));
|
|
|
};
|
|
|
|
|
|
const handleFromChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
@@ -77,12 +70,11 @@ const Cockpit: FC = () => {
|
|
|
|
|
|
const handleToChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
try {
|
|
|
- mustCheckDateOrder(e.target.value, dashboardState.to);
|
|
|
+ mustCheckDateOrder(dashboardState.from, e.target.value);
|
|
|
} catch (e) {
|
|
|
setError(e as any);
|
|
|
return;
|
|
|
}
|
|
|
- mustCheckDateOrder(e.target.value, dashboardState.to);
|
|
|
return dashboardDispatch(actions.setToControl(e.target.value));
|
|
|
};
|
|
|
|
|
|
@@ -155,28 +147,4 @@ const Cockpit: FC = () => {
|
|
|
);
|
|
|
};
|
|
|
|
|
|
-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;
|