Ver Fonte

Merge branch 'controles-dashboard' into develop

wilitp há 4 anos atrás
pai
commit
6282194ed9

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

@@ -20,7 +20,7 @@ const Cockpit: FC = () => {
         <Select
           list={fincaList}
           onChange={(e: ChangeEvent<HTMLInputElement>) =>
-            console.log(e.target.value)
+            dashboardDispatch(actions.setSector(e.target.value))
           }
           name="Comparación"
           placeholder="Fincas"

+ 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}>

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

@@ -1,10 +1,12 @@
 export type ActionType =
   | "SET_FROM_CONTROL"
   | "SET_TO_CONTROL"
-  | "SET_YEAR_CONTROL";
+  | "SET_YEAR_CONTROL"
+  | "SET_SECTOR";
 
 export type Action = {
   type: ActionType;
+  sector?: string | null;
   to?: string | null;
   from?: string | null;
   year?: string | null;

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

@@ -15,4 +15,9 @@ export const setToControl = (to: string): Action => ({
   to,
 });
 
+export const setSector = (sector: string): Action => ({
+  type: "SET_SECTOR",
+  sector,
+});
+
 export default {};

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

@@ -7,28 +7,54 @@ 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 {
+    sector: null,
+    year: now.getFullYear().toString(),
+    from: day,
+    to: day,
+  };
+};
+
+// Sector = Station
 export type State = {
-  to?: string | null;
-  from?: string | null;
-  year?: string | null;
+  sector: 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_SECTOR":
+      return {
+        ...state,
+        sector: action.sector!,
+      };
     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;