Преглед изворни кода

Fix max date on current campaign

Francesco Silvetti пре 4 година
родитељ
комит
d1af9ed51f

+ 5 - 37
app/src/components/UI/dashboard/cockpit/index.tsx

@@ -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;

+ 1 - 0
app/src/context/dashboard/Provider.tsx

@@ -39,6 +39,7 @@ export const StateContext = createContext<State>({
   to: "",
   maxDate: "",
   minDate: "",
+  years: [],
 });
 export const DispatchContext = createContext<
   (action: Action | ((...args: any) => Promise<any>)) => void

+ 10 - 20
app/src/context/dashboard/reducer.ts

@@ -1,4 +1,5 @@
 import { Reducer } from "react";
+import { campMaxMin, getCampList, getLastOrCurrentCamp } from "../../utils";
 import { Action } from "./actionTypes";
 
 export const defaultState = {
@@ -8,29 +9,17 @@ export const defaultState = {
 };
 
 export const getInitialState = (): State => {
-  const now = new Date();
-
-  // Las fechas extremas de la temporada de este anio
-  let maxDate = new Date(now.getFullYear(), 2, 31);
-  let minDate = new Date(now.getFullYear() - 1, 9, 1);
-
-  // Si ya empezo la temporada del anio siguiente, movemos las fechas por defecto
-  if (now.getTime() > maxDate.getTime()) {
-    maxDate.setFullYear(maxDate.getFullYear() + 1);
-    minDate.setFullYear(minDate.getFullYear() + 1);
-  }
-
-  // Conseguimos el string yyyy-mm-dd
-  const maxDateString = maxDate.toISOString().slice(0, 10);
-  const minDateString = minDate.toISOString().slice(0, 10);
+  const lastCamp = getLastOrCurrentCamp();
+  const {min, max} = campMaxMin(lastCamp);
 
   return {
     sector: null,
-    year: now.getFullYear().toString(),
-    from: minDateString,
-    to: maxDateString,
-    minDate: minDateString,
-    maxDate: maxDateString,
+    year: lastCamp,
+    years: getCampList(),
+    from: min,
+    to: max,
+    minDate: min,
+    maxDate: max,
   };
 };
 
@@ -40,6 +29,7 @@ export type State = {
   to: string;
   from: string;
   year: string;
+  years: Array<string>;
   maxDate: string;
   minDate: string;
 };

+ 52 - 0
app/src/utils.ts

@@ -1,9 +1,11 @@
 import { scale } from "chroma-js";
+
 export function checkDateOrder(first: string, second: string) {
   const firstTs = Date.parse(first);
   const secondTs = Date.parse(second);
   return firstTs <= secondTs;
 }
+
 export function mustCheckDateOrder(first: string, second: string) {
   if (!checkDateOrder(first, second)) {
     throw new Error(
@@ -12,6 +14,56 @@ export function mustCheckDateOrder(first: string, second: string) {
   }
 }
 
+export const campMaxMin = (year: string) => {
+  let maxDate;
+  let minDate;
+
+  let now = new Date();
+
+  if (isCampInProgress(year, now)) {
+    // La temporada empezó y es la de este año
+    maxDate = now;
+    minDate = new Date(parseInt(year) - 1, 9, 1);
+  } else {
+    // Las fechas extremas de la temporada del año seleccionado
+    maxDate = new Date(parseInt(year), 2, 31);
+    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 };
+};
+
+
+export const isCampInProgress = (camp: string, date: Date): boolean => {
+  let campStartDate = new Date(parseInt(camp) - 1, 9, 1);
+  let campEndDate = new Date(parseInt(camp), 2, 31);
+
+  return campStartDate <= date && date <= campEndDate;
+}
+
+export const getLastOrCurrentCamp = (): string => {
+  let now = new Date();
+
+  if (now.getMonth() >= 9 && now.getDay() >= 1) {
+    return (now.getFullYear() + 1).toString();
+  }
+
+  return now.getFullYear().toString();
+}
+
+// Computa las campanias para seleccionar
+// a partir de la ultima campania y la cantidad de anios para atras
+const campList = (lastCampaignYear: string, length: number): string[] =>
+  Array.from({length,}).map((_, i) => (parseInt(lastCampaignYear) - length + 1 + i).toString());
+
+export const getCampList = (): string[] => {
+  return campList(getLastOrCurrentCamp(), 5);
+}
+
 function campYear(ISOFrom: string): number {
   // El año de la temporada 2021 - 2022 es 2022
   // "El año empieza el 1 de Octubre"