Bläddra i källkod

minDate y maxDate

wilitp 4 år sedan
förälder
incheckning
a842d56297

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

@@ -37,6 +37,8 @@ export const StateContext = createContext<State>({
   year: "",
   from: "",
   to: "",
+  maxDate: "",
+  minDate: "",
 });
 export const DispatchContext = createContext<
   (action: Action | ((...args: any) => Promise<any>)) => void

+ 3 - 0
app/src/context/dashboard/actionTypes.ts

@@ -2,6 +2,8 @@ export type ActionType =
   | "SET_FROM_CONTROL"
   | "SET_TO_CONTROL"
   | "SET_YEAR_CONTROL"
+  | "SET_MIN_DATE"
+  | "SET_MAX_DATE"
   | "SET_SECTOR";
 
 export type Action = {
@@ -10,4 +12,5 @@ export type Action = {
   to?: string | null;
   from?: string | null;
   year?: string | null;
+  date?: string;
 };

+ 10 - 0
app/src/context/dashboard/actions.ts

@@ -20,4 +20,14 @@ export const setSector = (sector: string): Action => ({
   sector,
 });
 
+export const setMaxDate = (date: string): Action => ({
+  type: "SET_MAX_DATE",
+  date,
+});
+
+export const setMinDate = (date: string): Action => ({
+  type: "SET_MIN_DATE",
+  date,
+});
+
 export default {};

+ 29 - 0
app/src/context/dashboard/reducer.ts

@@ -10,11 +10,28 @@ export const defaultState = {
 export const getInitialState = (): State => {
   const now = new Date();
   const day = now.toISOString();
+
+  // 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 = maxDate.toISOString().slice(0, 10);
+
   return {
     sector: null,
     year: now.getFullYear().toString(),
     from: day,
     to: day,
+    minDate: minDateString,
+    maxDate: maxDateString,
   };
 };
 
@@ -24,6 +41,8 @@ export type State = {
   to: string;
   from: string;
   year: string;
+  maxDate: string;
+  minDate: string;
 };
 
 const fixYear = (year: string, dateString: string) =>
@@ -36,6 +55,16 @@ const reducer: Reducer<State, Action> = (state, action) => {
         ...state,
         sector: action.sector!,
       };
+    case "SET_MIN_DATE":
+      return {
+        ...state,
+        minDate: action.date ?? "",
+      };
+    case "SET_MAX_DATE":
+      return {
+        ...state,
+        maxDate: action.date ?? "",
+      };
     case "SET_TO_CONTROL":
       return {
         ...state,