From 73296469de0cf1318347dcabc5cef5ad759f067b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 14 May 2022 20:02:10 -0700 Subject: Migrate events report --- modern/src/reports/EventReportPage.js | 158 +++++++++++++++++----------------- 1 file changed, 81 insertions(+), 77 deletions(-) (limited to 'modern/src/reports/EventReportPage.js') diff --git a/modern/src/reports/EventReportPage.js b/modern/src/reports/EventReportPage.js index 6dd287d9..7baf2205 100644 --- a/modern/src/reports/EventReportPage.js +++ b/modern/src/reports/EventReportPage.js @@ -1,16 +1,16 @@ import React, { useState } from 'react'; -import { DataGrid } from '@material-ui/data-grid'; import { - FormControl, InputLabel, Select, MenuItem, + FormControl, InputLabel, Select, MenuItem, TableContainer, Table, TableHead, TableRow, TableCell, TableBody, } from '@material-ui/core'; -import { useTheme } from '@material-ui/core/styles'; import { useSelector } from 'react-redux'; -import { formatDate } from '../common/util/formatter'; +import { formatTime } from '../common/util/formatter'; 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'; +import usePersistedState from '../common/util/usePersistedState'; +import ColumnSelect from './components/ColumnSelect'; const typesArray = [ ['allEvents', 'eventAll'], @@ -34,11 +34,24 @@ const typesArray = [ ]; const typesMap = new Map(typesArray); -const Filter = ({ setItems }) => { +const columnsArray = [ + ['eventTime', 'positionFixTime'], + ['type', 'sharedType'], + ['geofenceId', 'sharedGeofence'], + ['maintenanceId', 'sharedMaintenance'], + ['alarm', 'positionAlarm'], +]; +const columnsMap = new Map(columnsArray); + +const EventReportPage = () => { const classes = useFilterStyles(); const t = useTranslation(); + const geofences = useSelector((state) => state.geofences.items); + + const [columns, setColumns] = usePersistedState('eventColumns', ['eventTime', 'type', 'alarm']); const [eventTypes, setEventTypes] = useState(['allEvents']); + const [items, setItems] = useState([]); const handleSubmit = async (deviceId, from, to, mail, headers) => { const query = new URLSearchParams({ @@ -58,83 +71,74 @@ const Filter = ({ setItems }) => { } }; - return ( - -
- - {t('reportEventTypes')} - - -
-
- ); -}; - -const EventReportPage = () => { - const theme = useTheme(); - const t = useTranslation(); - - const geofences = useSelector((state) => state.geofences.items); - - const [items, setItems] = useState([]); - - const formatGeofence = (value) => { - if (value > 0) { - const geofence = geofences[value]; - return geofence ? geofence.name : ''; + const formatValue = (item, key) => { + switch (key) { + case 'eventTime': + return formatTime(item[key]); + case 'type': + return t(prefixString('event', item[key])); + case 'geofenceId': + if (item[key] > 0) { + const geofence = geofences[item[key]]; + return geofence && geofence.name; + } + return null; + case 'maintenanceId': + return item[key] > 0 ? item[key] > 0 : null; + case 'alarm': + return item.attributes[key] ? t(prefixString('alarm', item.attributes[key])) : null; + default: + return item[key]; } - return null; }; - const columns = [{ - headerName: t('positionFixTime'), - field: 'serverTime', - type: 'dateTime', - width: theme.dimensions.columnWidthDate, - valueFormatter: ({ value }) => formatDate(value), - }, { - headerName: t('sharedType'), - field: 'type', - type: 'string', - width: theme.dimensions.columnWidthString, - valueFormatter: ({ value }) => t(prefixString('event', value)), - }, { - headerName: t('sharedGeofence'), - field: 'geofenceId', - width: theme.dimensions.columnWidthString, - valueFormatter: ({ value }) => formatGeofence(value), - }, { - headerName: t('sharedMaintenance'), - field: 'maintenanceId', - type: 'number', - width: theme.dimensions.columnWidthString, - }]; - return ( } breadcrumbs={['reportTitle', 'reportEvents']}> - - + +
+ + {t('reportEventTypes')} + + +
+ +
+ + + + + {columns.map((key) => ({t(columnsMap.get(key))}))} + + + + {items.map((item) => ( + + {columns.map((key) => ( + + {formatValue(item, key)} + + ))} + + ))} + +
+
); }; -- cgit v1.2.3