Sfoglia il codice sorgente

Controles de fechas

wilitp 4 anni fa
parent
commit
5f0d47839f

+ 2 - 4
app/src/components/UI/forms/calendarInput.tsx

@@ -1,4 +1,4 @@
-import React, { FC, useState } from "react";
+import React, { FC } from "react";
 
 interface calendarInputProps {
   onChange: Function;
@@ -10,12 +10,10 @@ interface calendarInputProps {
 }
 
 const CalendarInput: FC<calendarInputProps> = ({ className, ...props }) => {
-  const defaultValue = new Date().toISOString().substr(0, 10);
-
   return (
     <input
       {...(props as any)}
-      defaultValue={defaultValue}
+      // defaultValue={defaultValue}
       className={`form-control ${className}`}
       type="date"
       id="start"

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

@@ -1,9 +1,9 @@
 import React, { createContext, useReducer, Dispatch, FC } from "react";
-import reducer, { State, defaultState } from "./reducer";
+import reducer, { State, getInitialState } from "./reducer";
 import { Action } from "./actionTypes";
 
 const Provider: FC = ({ children }) => {
-  const [state, dispatch] = useReducer(reducer, defaultState);
+  const [state, dispatch] = useReducer(reducer, getInitialState());
 
   return (
     <StateContext.Provider value={state}>

+ 24 - 6
app/src/context/dashboard/reducer.ts

@@ -7,28 +7,46 @@ export const defaultState = {
   to: null,
 };
 
+export const getInitialState = (): State => {
+  const now = new Date();
+  const dayNum = now.getDate();
+  const day = `${now.getFullYear()}-${now.getMonth() + 1}-${
+    dayNum < 10 ? `0${dayNum}` : dayNum
+  }`;
+  return {
+    year: now.getFullYear().toString(),
+    from: day,
+    to: day,
+  };
+};
+
 export type State = {
-  to?: string | null;
-  from?: string | null;
-  year?: string | null;
+  to: string;
+  from: string;
+  year: string;
 };
 
+const fixYear = (year: string, dateString: string) =>
+  `${year}-${dateString.slice(5)}`;
+
 const reducer: Reducer<State, Action> = (state, action) => {
   switch (action.type) {
     case "SET_TO_CONTROL":
       return {
         ...state,
-        to: action.to,
+        to: fixYear(state.year, action.to!),
       };
     case "SET_FROM_CONTROL":
       return {
         ...state,
-        from: action.from,
+        from: fixYear(state.year, action.from!),
       };
     case "SET_YEAR_CONTROL":
       return {
         ...state,
-        year: action.year,
+        year: action.year!,
+        to: fixYear(action.year!, state.to),
+        from: fixYear(action.year!, state.from),
       };
     default:
       return state;