diff --git a/src/App.css b/src/App.css index 1ea3cb4..75a4fe9 100644 --- a/src/App.css +++ b/src/App.css @@ -1,6 +1,8 @@ .app { padding: 2rem; height: 100%; + display: flex; + flex-direction: column; } /*.app {*/ /*text-align: center;*/ diff --git a/src/App.js b/src/App.js index e29eb4c..c221553 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import React, {useState} from 'react'; +import React, { useState } from 'react'; import {Route, Router, Switch} from 'react-router' import {Collapse, Nav, Navbar, NavbarBrand, NavbarToggler, NavItem, NavLink} from 'reactstrap'; import 'react-virtualized/styles.css'; @@ -8,13 +8,35 @@ import history from './history'; import ChartPage from './ChartPage'; import TablePage from './TablePage'; +export const FormContext = React.createContext({ + formValues: {}, + changeForm: () => {} +}); + +export const ChartContext = React.createContext({ + plot: [], + setPlot: () => {} +}); + +export const TableContext = React.createContext({ + table: [], + filter: '', + setTable: () => {}, + setFilter: () => {} +}); + export default function App() { const [isOpen, setIsOpen] = useState(false); + const [formValues, setFormValues] = useState({}); + const [plot, setPlot] = useState([]); + const [table, setTable] = useState([]); + const [filter, setFilter] = useState(''); const toggle = () => setIsOpen(!isOpen); - const navigate = (path) => () => history.push(path); + const _setFormValues = (name, value) => setFormValues({ ...formValues, [name]: value }); + return ( @@ -35,10 +57,16 @@ export default function App() {
- + + + - + + + + +
diff --git a/src/ChartPage.jsx b/src/ChartPage.jsx index fe9f908..41936ed 100644 --- a/src/ChartPage.jsx +++ b/src/ChartPage.jsx @@ -1,15 +1,16 @@ -import React, { useState } from 'react'; +import React, { useState, useContext } from 'react'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend } from 'recharts'; import { AutoSizer } from 'react-virtualized'; import './charts.css'; import { preparePlotData } from './service'; import Form from './Form'; +import { ChartContext } from './App'; export const ChartPage = () => { const [loading, setLoading] = useState(false); - const [plot, setPlot] = useState([]); + const {plot, setPlot} = useContext(ChartContext); const onSubmit = (formParams) => { setLoading(true); diff --git a/src/Form.js b/src/Form.js index 89c9988..4a29523 100644 --- a/src/Form.js +++ b/src/Form.js @@ -1,30 +1,56 @@ -import React, { useState } from 'react'; +import React, { useState, useContext, useEffect } from 'react'; import { FormGroup, Label } from 'reactstrap'; import DateRangePicker from '@wojtekmaj/react-daterange-picker'; import Select from 'react-select'; import { Button } from 'reactstrap'; import './form.css'; -import {getMinDate, getMaxDate, getSystemList, getCriticalityList} from './service'; +import { getMinDate, getMaxDate, getSystemList, getCriticalityList } from './service'; +import { FormContext } from './App'; const criticalityList = getCriticalityList().map(value => ({ value, label: value })); const systemList = getSystemList().map(value => ({ value, label: value })); const Form = ({ onSubmit, loading, className }) => { - const [date, setDate] = useState([getMinDate(), getMaxDate()]); - const [system, setSystem] = useState(null); - const [criticality, setCriticality] = useState(null); + const { formValues, changeForm} = useContext(FormContext); + const [error, setError] = useState(null); + let tmr; + const _setError = (err) => { + setError(err); + tmr = setTimeout(() => setError(null), 5000); + }; + const _onSubmit = () => { + if (tmr) { + clearTimeout(tmr); + } + if (formValues.date && formValues.system && formValues.criticality) { + onSubmit({ date: formValues.date, system: formValues.system.value, criticality: formValues.criticality.value }); + } else { + _setError('Все поля должны быть заполнены.'); + } + }; + + useEffect(() => { + return () => { + if (tmr) { + clearTimeout(tmr); + } + } + }, []); return (
+ {error &&
{error}
}
changeForm('date', value)} + value={formValues.date} disabled={loading} + minDate={getMinDate()} + maxDate={getMaxDate()} />
@@ -33,9 +59,8 @@ const Form = ({ onSubmit, loading, className }) => { { - setCriticality(picked.value); - }} + value={formValues.criticality} + onChange={ picked => changeForm('criticality', picked)} isDisabled={loading} />