From 41244e5354fd05ef92d94fda0ceab36b2c23e88d Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 14 May 2022 16:58:54 -0700 Subject: Improve report filter --- modern/src/common/theme/dimensions.js | 1 + modern/src/reports/ChartReportPage.js | 57 +++++------- modern/src/reports/EventReportPage.js | 9 +- modern/src/reports/StatisticsPage.js | 102 ++------------------- modern/src/reports/SummaryReportPage.js | 22 +++-- modern/src/reports/components/ReportFilter.js | 122 +++++++++++++++----------- 6 files changed, 118 insertions(+), 195 deletions(-) (limited to 'modern') diff --git a/modern/src/common/theme/dimensions.js b/modern/src/common/theme/dimensions.js index fa7d3b25..fcb3b31f 100644 --- a/modern/src/common/theme/dimensions.js +++ b/modern/src/common/theme/dimensions.js @@ -5,6 +5,7 @@ export default { sidebarWidthTablet: '52px', drawerWidthDesktop: '360px', drawerWidthTablet: '320px', + filterFormWidth: '160px', bottomBarHeight: 56, columnWidthDate: 160, columnWidthNumber: 130, diff --git a/modern/src/reports/ChartReportPage.js b/modern/src/reports/ChartReportPage.js index 4ddb5f75..57c7c689 100644 --- a/modern/src/reports/ChartReportPage.js +++ b/modern/src/reports/ChartReportPage.js @@ -1,8 +1,8 @@ import React, { useState } from 'react'; import { - Grid, FormControl, InputLabel, Select, MenuItem, + FormControl, InputLabel, Select, MenuItem, } from '@material-ui/core'; -import ReportFilter from './components/ReportFilter'; +import ReportFilter, { useFilterStyles } from './components/ReportFilter'; import Graph from './components/Graph'; import { useAttributePreference } from '../common/util/preferences'; import { formatDate } from '../common/util/formatter'; @@ -11,9 +11,15 @@ import { useTranslation } from '../common/components/LocalizationProvider'; import PageLayout from '../common/components/PageLayout'; import ReportsMenu from './components/ReportsMenu'; -const Filter = ({ children, setItems }) => { +const ChartReportPage = () => { + const classes = useFilterStyles(); + const t = useTranslation(); + const speedUnit = useAttributePreference('speedUnit'); + const [items, setItems] = useState([]); + const [type, setType] = useState('speed'); + const handleSubmit = async (deviceId, from, to, mail, headers) => { const query = new URLSearchParams({ deviceId, from, to, mail, @@ -30,42 +36,21 @@ const Filter = ({ children, setItems }) => { setItems(formattedPositions); } }; - return ( - <> - - {children} - - ); -}; - -const ChartType = ({ type, setType }) => { - const t = useTranslation(); - - return ( - - - - {t('reportChartType')} - - - - - ); -}; - -const ChartReportPage = () => { - const [items, setItems] = useState([]); - const [type, setType] = useState('speed'); return ( } breadcrumbs={['reportTitle', 'reportChart']}> - - - + +
+ + {t('reportChartType')} + + +
+
); diff --git a/modern/src/reports/EventReportPage.js b/modern/src/reports/EventReportPage.js index 71ca08d9..33f656b6 100644 --- a/modern/src/reports/EventReportPage.js +++ b/modern/src/reports/EventReportPage.js @@ -1,18 +1,19 @@ import React, { useState } from 'react'; import { DataGrid } from '@material-ui/data-grid'; import { - Grid, FormControl, InputLabel, Select, MenuItem, + FormControl, InputLabel, Select, MenuItem, } from '@material-ui/core'; import { useTheme } from '@material-ui/core/styles'; import { useSelector } from 'react-redux'; import { formatDate } from '../common/util/formatter'; -import ReportFilter from './components/ReportFilter'; +import ReportFilter, { useFilterStyles } from './components/ReportFilter'; import { prefixString } from '../common/util/stringUtils'; import { useTranslation } from '../common/components/LocalizationProvider'; import PageLayout from '../common/components/PageLayout'; import ReportsMenu from './components/ReportsMenu'; const Filter = ({ setItems }) => { + const classes = useFilterStyles(); const t = useTranslation(); const [eventTypes, setEventTypes] = useState(['allEvents']); @@ -37,7 +38,7 @@ const Filter = ({ setItems }) => { return ( - +
{t('reportEventTypes')} - +
); }; diff --git a/modern/src/reports/StatisticsPage.js b/modern/src/reports/StatisticsPage.js index f04f6e43..fcdf22d6 100644 --- a/modern/src/reports/StatisticsPage.js +++ b/modern/src/reports/StatisticsPage.js @@ -1,117 +1,29 @@ import React, { useState } from 'react'; import { - FormControl, InputLabel, Select, MenuItem, TextField, Button, TableContainer, Table, TableRow, TableCell, TableHead, TableBody, makeStyles, + TableContainer, Table, TableRow, TableCell, TableHead, TableBody, } from '@material-ui/core'; -import moment from 'moment'; import { formatDate } from '../common/util/formatter'; import { useTranslation } from '../common/components/LocalizationProvider'; import PageLayout from '../common/components/PageLayout'; import ReportsMenu from './components/ReportsMenu'; +import ReportFilter from './components/ReportFilter'; -const useStyles = makeStyles((theme) => ({ - filter: { - margin: theme.spacing(1), - }, -})); - -const Filter = ({ setItems }) => { - const classes = useStyles(); +const StatisticsPage = () => { const t = useTranslation(); - const [period, setPeriod] = useState('today'); - const [from, setFrom] = useState(moment().subtract(1, 'hour')); - const [to, setTo] = useState(moment()); - - const handleClick = async () => { - let selectedFrom; - let selectedTo; - switch (period) { - case 'today': - selectedFrom = moment().startOf('day'); - selectedTo = moment().endOf('day'); - break; - case 'yesterday': - selectedFrom = moment().subtract(1, 'day').startOf('day'); - selectedTo = moment().subtract(1, 'day').endOf('day'); - break; - case 'thisWeek': - selectedFrom = moment().startOf('week'); - selectedTo = moment().endOf('week'); - break; - case 'previousWeek': - selectedFrom = moment().subtract(1, 'week').startOf('week'); - selectedTo = moment().subtract(1, 'week').endOf('week'); - break; - case 'thisMonth': - selectedFrom = moment().startOf('month'); - selectedTo = moment().endOf('month'); - break; - case 'previousMonth': - selectedFrom = moment().subtract(1, 'month').startOf('month'); - selectedTo = moment().subtract(1, 'month').endOf('month'); - break; - default: - selectedFrom = from; - selectedTo = to; - break; - } + const [items, setItems] = useState([]); - const query = new URLSearchParams({ from: selectedFrom.toISOString(), to: selectedTo.toISOString() }); + const handleSubmit = async (from, to) => { + const query = new URLSearchParams({ from, to }); const response = await fetch(`/api/statistics?${query.toString()}`, { Accept: 'application/json' }); if (response.ok) { setItems(await response.json()); } }; - return ( -
- - {t('reportPeriod')} - - - {period === 'custom' && ( - setFrom(moment(e.target.value, moment.HTML5_FMT.DATETIME_LOCAL))} - fullWidth - /> - )} - {period === 'custom' && ( - setTo(moment(e.target.value, moment.HTML5_FMT.DATETIME_LOCAL))} - fullWidth - /> - )} - -
- ); -}; - -const StatisticsPage = () => { - const t = useTranslation(); - - const [items, setItems] = useState([]); - return ( } breadcrumbs={['reportTitle', 'statisticsTitle']}> - + diff --git a/modern/src/reports/SummaryReportPage.js b/modern/src/reports/SummaryReportPage.js index b9f5e5dd..5a8bb34e 100644 --- a/modern/src/reports/SummaryReportPage.js +++ b/modern/src/reports/SummaryReportPage.js @@ -1,17 +1,20 @@ import React, { useState } from 'react'; import { DataGrid } from '@material-ui/data-grid'; -import { Grid, FormControlLabel, Checkbox } from '@material-ui/core'; +import { + FormControl, InputLabel, Select, MenuItem, +} from '@material-ui/core'; import { useTheme } from '@material-ui/core/styles'; import { formatDistance, formatHours, formatDate, formatSpeed, formatVolume, } from '../common/util/formatter'; -import ReportFilter from './components/ReportFilter'; +import ReportFilter, { useFilterStyles } from './components/ReportFilter'; import { useAttributePreference } from '../common/util/preferences'; import { useTranslation } from '../common/components/LocalizationProvider'; import PageLayout from '../common/components/PageLayout'; import ReportsMenu from './components/ReportsMenu'; const Filter = ({ setItems }) => { + const classes = useFilterStyles(); const t = useTranslation(); const [daily, setDaily] = useState(false); @@ -35,12 +38,15 @@ const Filter = ({ setItems }) => { return ( - - setDaily(e.target.checked)} />} - label={t('reportDaily')} - /> - +
+ + {t('sharedType')} + + +
); }; diff --git a/modern/src/reports/components/ReportFilter.js b/modern/src/reports/components/ReportFilter.js index 739d5e47..30a49c81 100644 --- a/modern/src/reports/components/ReportFilter.js +++ b/modern/src/reports/components/ReportFilter.js @@ -1,23 +1,42 @@ import React, { useState } from 'react'; import { - FormControl, InputLabel, Select, MenuItem, Button, TextField, Grid, Typography, makeStyles, + FormControl, InputLabel, Select, MenuItem, Button, TextField, Typography, makeStyles, } from '@material-ui/core'; import { useSelector } from 'react-redux'; import moment from 'moment'; import { useTranslation } from '../../common/components/LocalizationProvider'; +import dimensions from '../../common/theme/dimensions'; -const useStyles = makeStyles((theme) => ({ +export const useFilterStyles = makeStyles((theme) => ({ filter: { + display: 'flex', + flexWrap: 'wrap', + gap: theme.spacing(1), padding: theme.spacing(2), }, + item: { + flex: `1 1 ${dimensions.filterFormWidth}`, + }, + buttons: { + display: 'flex', + gap: theme.spacing(1), + flex: `1 1 ${dimensions.filterFormWidth}`, + }, + button: { + flexGrow: 1, + }, })); -const ReportFilter = ({ children, handleSubmit, showOnly }) => { - const classes = useStyles(); +const ReportFilter = ({ + children, handleSubmit, showOnly, ignoreDevice, +}) => { + const classes = useFilterStyles(); const t = useTranslation(); const devices = useSelector((state) => state.devices.items); - const [deviceId, setDeviceId] = useState(); + const selectedDeviceId = useSelector((state) => state.devices.selectedId); + + const [deviceId, setDeviceId] = useState(selectedDeviceId); const [period, setPeriod] = useState('today'); const [from, setFrom] = useState(moment().subtract(1, 'hour')); const [to, setTo] = useState(moment()); @@ -67,18 +86,20 @@ const ReportFilter = ({ children, handleSubmit, showOnly }) => { }; return ( - - - - {t('reportDevice')} - - - - +
+ {!ignoreDevice && ( +
+ + {t('reportDevice')} + + +
+ )} +
{t('reportPeriod')} - +
{period === 'custom' && ( - - setFrom(moment(e.target.value, moment.HTML5_FMT.DATETIME_LOCAL))} - fullWidth - /> - +
+ setFrom(moment(e.target.value, moment.HTML5_FMT.DATETIME_LOCAL))} + fullWidth + /> +
)} {period === 'custom' && ( - - setTo(moment(e.target.value, moment.HTML5_FMT.DATETIME_LOCAL))} - fullWidth - /> - +
+ setTo(moment(e.target.value, moment.HTML5_FMT.DATETIME_LOCAL))} + fullWidth + /> +
)} {children} - +
- - {!showOnly - && ( - + {!showOnly && ( - )} - {!showOnly - && ( - + {!showOnly && ( - )} - +
+
); }; -- cgit v1.2.3