From f8bb2fb5dfd801b7c49a3a3b66042e94dc7640dc Mon Sep 17 00:00:00 2001 From: Ashutosh Bishnoi Date: Sun, 13 Dec 2020 19:31:39 +0530 Subject: Refactored all the reports components to make them more generic --- modern/src/reports/ChartReportPage.js | 73 ++++++++++++++------------------- modern/src/reports/EventReportPage.js | 4 +- modern/src/reports/Graph.js | 35 ++++++++++++++++ modern/src/reports/ReportLayoutPage.js | 4 +- modern/src/reports/RouteReportPage.js | 4 +- modern/src/reports/StopReportPage.js | 4 +- modern/src/reports/SummaryReportPage.js | 4 +- modern/src/reports/TripReportPage.js | 4 +- 8 files changed, 77 insertions(+), 55 deletions(-) create mode 100644 modern/src/reports/Graph.js (limited to 'modern/src/reports') diff --git a/modern/src/reports/ChartReportPage.js b/modern/src/reports/ChartReportPage.js index 6177341..543c8a9 100644 --- a/modern/src/reports/ChartReportPage.js +++ b/modern/src/reports/ChartReportPage.js @@ -1,14 +1,14 @@ import React, { useState } from 'react'; -import { Box, Paper, FormControl, InputLabel, Select, MenuItem } from '@material-ui/core'; -import ReportFilter from './ReportFilter'; +import { FormControl, InputLabel, Select, MenuItem } from '@material-ui/core'; import ReportLayoutPage from './ReportLayoutPage'; +import ReportFilter from './ReportFilter'; +import Graph from './Graph'; import { useAttributePreference } from '../common/preferences'; import { formatDate } from '../common/formatter'; import { speedConverter } from '../common/converter'; -import {LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts'; import t from '../common/localization'; -const ReportFilterForm = ({ setItems, type, setType }) => { +const Filter = ({ children, setItems }) => { const speedUnit = useAttributePreference('speedUnit'); @@ -16,9 +16,7 @@ const ReportFilterForm = ({ setItems, type, setType }) => { const query = new URLSearchParams({ deviceId, from, to, mail }); const response = await fetch(`/api/reports/route?${query.toString()}`, { headers }); if (response.ok) { - const positions = await response.json(); - let formattedPositions = positions.map(position => { return { speed: Number(speedConverter(position.speed, speedUnit)), @@ -27,59 +25,48 @@ const ReportFilterForm = ({ setItems, type, setType }) => { fixTime: formatDate(position.fixTime) } }); - setItems(formattedPositions); } } - return ( <> - - {t('reportChartType')} - - + {children} ) -}; +} + +const ChartType = ({ type, setType }) => { -const CustomizedAxisTick = ({ x, y, payload }) =>{ - const parts = payload.value.split(' '); return ( - - {parts[0]} - {parts[1]} - - ); + + {t('reportChartType')} + + + ) } -const ChartReportPage = () => { +const ChartReportPage = () => { + const [items, setItems] = useState([]); const [type, setType] = useState('speed'); return ( - - - - - - } height={60} /> - - - - - - - - - - - ); -}; + <> + + + + }> + + + + ) +} export default ChartReportPage; diff --git a/modern/src/reports/EventReportPage.js b/modern/src/reports/EventReportPage.js index 13e081e..054181d 100644 --- a/modern/src/reports/EventReportPage.js +++ b/modern/src/reports/EventReportPage.js @@ -6,7 +6,7 @@ import { formatPosition } from '../common/formatter'; import ReportFilter from './ReportFilter'; import ReportLayoutPage from './ReportLayoutPage'; -const ReportFilterForm = ({ setItems }) => { +const Filter = ({ setItems }) => { const [eventTypes, setEventTypes] = useState(['allEvents']); @@ -60,7 +60,7 @@ const EventReportPage = () => { const [items, setItems] = useState([]); return ( - + }> diff --git a/modern/src/reports/Graph.js b/modern/src/reports/Graph.js new file mode 100644 index 0000000..af54e10 --- /dev/null +++ b/modern/src/reports/Graph.js @@ -0,0 +1,35 @@ +import React from 'react'; +import { Box, Paper } from '@material-ui/core'; +import {LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts'; + +const CustomizedAxisTick = ({ x, y, payload }) =>{ + const parts = payload.value.split(' '); + return ( + + {parts[0]} + {parts[1]} + + ); +} + +const Graph = ({ type, items }) => { + + return ( + + + + + } height={60} /> + + + + + + + + + + ); +} + +export default Graph; diff --git a/modern/src/reports/ReportLayoutPage.js b/modern/src/reports/ReportLayoutPage.js index 93cf6da..d6fac05 100644 --- a/modern/src/reports/ReportLayoutPage.js +++ b/modern/src/reports/ReportLayoutPage.js @@ -18,7 +18,7 @@ const useStyles = makeStyles(theme => ({ }, })); -const ReportLayoutPage = ({ reportFilterForm:ReportFilterForm, setItems, type, setType, children }) => { +const ReportLayoutPage = ({ children, filter }) => { const classes = useStyles(); return (
@@ -27,7 +27,7 @@ const ReportLayoutPage = ({ reportFilterForm:ReportFilterForm, setItems, type, s - + {filter} diff --git a/modern/src/reports/RouteReportPage.js b/modern/src/reports/RouteReportPage.js index 43d310b..f2b0361 100644 --- a/modern/src/reports/RouteReportPage.js +++ b/modern/src/reports/RouteReportPage.js @@ -5,7 +5,7 @@ import { formatPosition } from '../common/formatter'; import ReportFilter from './ReportFilter'; import ReportLayoutPage from './ReportLayoutPage'; -const ReportFilterForm = ({ setItems }) => { +const Filter = ({ setItems }) => { const handleSubmit = async (deviceId, from, to, mail, headers) => { const query = new URLSearchParams({ deviceId, from, to, mail }); @@ -30,7 +30,7 @@ const RouteReportPage = () => { const [items, setItems] = useState([]); return ( - + }>
diff --git a/modern/src/reports/StopReportPage.js b/modern/src/reports/StopReportPage.js index 77d3dfa..d06997e 100644 --- a/modern/src/reports/StopReportPage.js +++ b/modern/src/reports/StopReportPage.js @@ -6,7 +6,7 @@ import ReportFilter from './ReportFilter'; import ReportLayoutPage from './ReportLayoutPage'; import { useAttributePreference } from '../common/preferences'; -const ReportFilterForm = ({ setItems }) => { +const Filter = ({ setItems }) => { const handleSubmit = async (deviceId, from, to, mail, headers) => { const query = new URLSearchParams({ deviceId, from, to, mail }); @@ -32,7 +32,7 @@ const StopReportPage = () => { const [items, setItems] = useState([]); return ( - + }>
diff --git a/modern/src/reports/SummaryReportPage.js b/modern/src/reports/SummaryReportPage.js index dcd3323..5cfd857 100644 --- a/modern/src/reports/SummaryReportPage.js +++ b/modern/src/reports/SummaryReportPage.js @@ -6,7 +6,7 @@ import ReportFilter from './ReportFilter'; import ReportLayoutPage from './ReportLayoutPage'; import { useAttributePreference } from '../common/preferences'; -const ReportFilterForm = ({ setItems }) => { +const Filter = ({ setItems }) => { const [daily, setDaily] = useState(false); @@ -41,7 +41,7 @@ const SummaryReportPage = () => { const [items, setItems] = useState([]); return ( - + }>
diff --git a/modern/src/reports/TripReportPage.js b/modern/src/reports/TripReportPage.js index 10f1c46..c1ba664 100644 --- a/modern/src/reports/TripReportPage.js +++ b/modern/src/reports/TripReportPage.js @@ -6,7 +6,7 @@ import ReportFilter from './ReportFilter'; import ReportLayoutPage from './ReportLayoutPage'; import { useAttributePreference } from '../common/preferences'; -const ReportFilterForm = ({ setItems }) => { +const Filter = ({ setItems }) => { const handleSubmit = async (deviceId, from, to, mail, headers) => { const query = new URLSearchParams({ deviceId, from, to, mail }); @@ -33,7 +33,7 @@ const TripReportPage = () => { const [items, setItems] = useState([]); return ( - + }>
-- cgit v1.2.3