From 4807ad55511c936bcfefca59a584ac8712ef1efd Mon Sep 17 00:00:00 2001 From: Ashutosh Bishnoi Date: Wed, 24 Feb 2021 12:06:13 +0530 Subject: More code improvements --- modern/src/admin/StatisticsPage.js | 85 +++++++++++++++++++++++++++++++++++--- modern/src/reports/ReportFilter.js | 22 +++++----- 2 files changed, 89 insertions(+), 18 deletions(-) (limited to 'modern') diff --git a/modern/src/admin/StatisticsPage.js b/modern/src/admin/StatisticsPage.js index 4aaec3b..b7fb474 100644 --- a/modern/src/admin/StatisticsPage.js +++ b/modern/src/admin/StatisticsPage.js @@ -1,21 +1,94 @@ import React, { useState } from 'react'; -import { TableContainer, Table, TableRow, TableCell, TableHead, TableBody, Paper } from '@material-ui/core'; +import { FormControl, InputLabel,Select, MenuItem, TextField, Button, TableContainer, Table, TableRow, TableCell, TableHead, TableBody, Paper } from '@material-ui/core'; import t from '../common/localization'; import { formatDate } from '../common/formatter'; -import ReportFilter from '../reports/ReportFilter'; import ReportLayoutPage from '../reports/ReportLayoutPage'; +import moment from 'moment'; const Filter = ({ setItems }) => { + const [period, setPeriod] = useState('today'); + const [from, setFrom] = useState(moment().subtract(1, 'hour')); + const [to, setTo] = useState(moment()); - const handleSubmit = async (deviceId, from, to, _, headers) => { - const query = new URLSearchParams({ from, to }); - const response = await fetch(`/api/statistics?${query.toString()}`, { headers }); + 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 query = new URLSearchParams({ from: selectedFrom.toISOString(), to: selectedTo.toISOString() }); + const response = await fetch(`/api/statistics?${query.toString()}`, { Accept: 'application/json' }); if (response.ok) { setItems(await response.json()); } } - return ; + + 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 = () => { diff --git a/modern/src/reports/ReportFilter.js b/modern/src/reports/ReportFilter.js index 48da114..9af3f7d 100644 --- a/modern/src/reports/ReportFilter.js +++ b/modern/src/reports/ReportFilter.js @@ -4,7 +4,7 @@ import t from '../common/localization'; import { useSelector } from 'react-redux'; import moment from 'moment'; -const ReportFilter = ({ children, handleSubmit, showOnly, showDevices }) => { +const ReportFilter = ({ children, handleSubmit, showOnly }) => { const devices = useSelector(state => Object.values(state.devices.items)); const [deviceId, setDeviceId] = useState(); const [period, setPeriod] = useState('today'); @@ -57,16 +57,14 @@ const ReportFilter = ({ children, handleSubmit, showOnly, showDevices }) => { return ( <> - {!showDevices && ( - - {t('reportDevice')} - - - )} + + {t('reportDevice')} + + {t('reportPeriod')}