diff options
author | Anton Tananaev <anton@traccar.org> | 2022-05-14 16:58:54 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-05-14 16:58:54 -0700 |
commit | 41244e5354fd05ef92d94fda0ceab36b2c23e88d (patch) | |
tree | 9b653faf3d10ca083aa5cc5da962dc8716770246 /modern/src/reports/StatisticsPage.js | |
parent | 8091d156d3379784d8d2f023c0a26944274542fd (diff) | |
download | trackermap-web-41244e5354fd05ef92d94fda0ceab36b2c23e88d.tar.gz trackermap-web-41244e5354fd05ef92d94fda0ceab36b2c23e88d.tar.bz2 trackermap-web-41244e5354fd05ef92d94fda0ceab36b2c23e88d.zip |
Improve report filter
Diffstat (limited to 'modern/src/reports/StatisticsPage.js')
-rw-r--r-- | modern/src/reports/StatisticsPage.js | 102 |
1 files changed, 7 insertions, 95 deletions
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,62 +1,20 @@ 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()); @@ -64,54 +22,8 @@ const Filter = ({ setItems }) => { }; return ( - <div className={classes.filter}> - <FormControl variant="filled" margin="normal" fullWidth> - <InputLabel>{t('reportPeriod')}</InputLabel> - <Select value={period} onChange={(e) => setPeriod(e.target.value)}> - <MenuItem value="today">{t('reportToday')}</MenuItem> - <MenuItem value="yesterday">{t('reportYesterday')}</MenuItem> - <MenuItem value="thisWeek">{t('reportThisWeek')}</MenuItem> - <MenuItem value="previousWeek">{t('reportPreviousWeek')}</MenuItem> - <MenuItem value="thisMonth">{t('reportThisMonth')}</MenuItem> - <MenuItem value="previousMonth">{t('reportPreviousMonth')}</MenuItem> - <MenuItem value="custom">{t('reportCustom')}</MenuItem> - </Select> - </FormControl> - {period === 'custom' && ( - <TextField - margin="normal" - variant="filled" - label={t('reportFrom')} - type="datetime-local" - value={from.format(moment.HTML5_FMT.DATETIME_LOCAL)} - onChange={(e) => setFrom(moment(e.target.value, moment.HTML5_FMT.DATETIME_LOCAL))} - fullWidth - /> - )} - {period === 'custom' && ( - <TextField - margin="normal" - variant="filled" - label={t('reportTo')} - type="datetime-local" - value={to.format(moment.HTML5_FMT.DATETIME_LOCAL)} - onChange={(e) => setTo(moment(e.target.value, moment.HTML5_FMT.DATETIME_LOCAL))} - fullWidth - /> - )} - <Button variant="outlined" color="secondary" onClick={handleClick} fullWidth>{t('reportShow')}</Button> - </div> - ); -}; - -const StatisticsPage = () => { - const t = useTranslation(); - - const [items, setItems] = useState([]); - - return ( <PageLayout menu={<ReportsMenu />} breadcrumbs={['reportTitle', 'statisticsTitle']}> - <Filter setItems={setItems} /> + <ReportFilter handleSubmit={handleSubmit} showOnly ignoreDevice /> <TableContainer> <Table> <TableHead> |