From 48489f116b996ceebd7e3a8d08b2223d4f8021cf Mon Sep 17 00:00:00 2001 From: Ashutosh Bishnoi Date: Tue, 13 Apr 2021 17:48:27 +0530 Subject: Event Type Localization and geofence store creation --- modern/src/App.js | 3 +++ modern/src/MainController.js | 22 ++++++++++++++++++++++ modern/src/reports/EventReportPage.js | 14 +++++++++++++- modern/src/store/geofences.js | 16 ++++++++++++++++ modern/src/store/index.js | 3 +++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 modern/src/MainController.js create mode 100644 modern/src/store/geofences.js diff --git a/modern/src/App.js b/modern/src/App.js index a01451b..ef5fd20 100644 --- a/modern/src/App.js +++ b/modern/src/App.js @@ -30,6 +30,8 @@ import MaintenancesPage from './settings/MaintenancesPage'; import MaintenancePage from './settings/MaintenancePage'; import StatisticsPage from './admin/StatisticsPage'; +import MainController from './MainController'; + const App = () => { const initialized = useSelector(state => !!state.session.server && !!state.session.user); @@ -37,6 +39,7 @@ const App = () => { <> + diff --git a/modern/src/MainController.js b/modern/src/MainController.js new file mode 100644 index 0000000..6ae98fd --- /dev/null +++ b/modern/src/MainController.js @@ -0,0 +1,22 @@ +import { useDispatch, useSelector } from 'react-redux'; +import { connect } from 'react-redux'; +import { geofencesActions } from './store'; +import { useEffectAsync } from './reactHelper'; + + +const MainController = () => { + const authenticated = useSelector(state => !!state.session.user); + const dispatch = useDispatch(); + + useEffectAsync(async () => { + if (authenticated) { + const response = await fetch('/api/geofences'); + if (response.ok) { + dispatch(geofencesActions.update(await response.json())); + } + } + }, [authenticated]); + return null; +} + +export default connect()(MainController); \ No newline at end of file diff --git a/modern/src/reports/EventReportPage.js b/modern/src/reports/EventReportPage.js index cbe2ced..2478cad 100644 --- a/modern/src/reports/EventReportPage.js +++ b/modern/src/reports/EventReportPage.js @@ -5,6 +5,8 @@ import t from '../common/localization'; import { formatDate } from '../common/formatter'; import ReportFilter from './ReportFilter'; import ReportLayoutPage from './ReportLayoutPage'; +import { prefixString } from '../common/stringUtils'; +import { useSelector } from 'react-redux'; const Filter = ({ setItems }) => { @@ -57,8 +59,17 @@ const Filter = ({ setItems }) => { const EventReportPage = () => { + 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 : ''; + } + return null; + } + const columns = [{ headerName: t('positionFixTime'), field: 'serverTime', @@ -70,11 +81,12 @@ const EventReportPage = () => { field: 'type', type: 'string', flex:1, + valueFormatter: ({ value }) => t(prefixString('event', value)), }, { headerName: t('sharedGeofence'), field: 'geofenceId', - type: 'number', flex: 1, + valueFormatter: ({ value }) => formatGeofence(value), }, { headerName: t('sharedMaintenance'), field: 'maintenanceId', diff --git a/modern/src/store/geofences.js b/modern/src/store/geofences.js new file mode 100644 index 0000000..c5e5814 --- /dev/null +++ b/modern/src/store/geofences.js @@ -0,0 +1,16 @@ +import { createSlice } from '@reduxjs/toolkit'; + +const { reducer, actions } = createSlice({ + name: 'geofences', + initialState: { + items: {}, + }, + reducers: { + update(state, action) { + action.payload.forEach(item => state.items[item['id']] = item); + }, + } +}); + +export { actions as geofencesActions }; +export { reducer as geofencesReducer }; diff --git a/modern/src/store/index.js b/modern/src/store/index.js index 432ef78..1fddae9 100644 --- a/modern/src/store/index.js +++ b/modern/src/store/index.js @@ -3,15 +3,18 @@ import { combineReducers, configureStore } from '@reduxjs/toolkit'; import { sessionReducer as session } from './session'; import { devicesReducer as devices } from './devices'; import { positionsReducer as positions } from './positions'; +import { geofencesReducer as geofences } from './geofences'; const reducer = combineReducers({ session, devices, positions, + geofences, }); export { sessionActions } from './session'; export { devicesActions } from './devices'; export { positionsActions } from './positions'; +export { geofencesActions } from './geofences'; export default configureStore({ reducer }); -- cgit v1.2.3 From 1fd5cd30a392ed0cc0937a4df8947e53a378fe28 Mon Sep 17 00:00:00 2001 From: Ashutosh Bishnoi Date: Tue, 13 Apr 2021 17:58:33 +0530 Subject: Minor code formatting --- modern/src/MainController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modern/src/MainController.js b/modern/src/MainController.js index 6ae98fd..f8f6d68 100644 --- a/modern/src/MainController.js +++ b/modern/src/MainController.js @@ -19,4 +19,4 @@ const MainController = () => { return null; } -export default connect()(MainController); \ No newline at end of file +export default connect()(MainController); -- cgit v1.2.3 From 006ee184700c3fb04267950e52a39c697500e66c Mon Sep 17 00:00:00 2001 From: Ashutosh Bishnoi Date: Fri, 16 Apr 2021 19:02:35 +0530 Subject: Rename the MainController to CachingController --- modern/src/App.js | 4 ++-- modern/src/CachingController.js | 22 ++++++++++++++++++++++ modern/src/MainController.js | 22 ---------------------- 3 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 modern/src/CachingController.js delete mode 100644 modern/src/MainController.js diff --git a/modern/src/App.js b/modern/src/App.js index ef5fd20..7f8a582 100644 --- a/modern/src/App.js +++ b/modern/src/App.js @@ -30,7 +30,7 @@ import MaintenancesPage from './settings/MaintenancesPage'; import MaintenancePage from './settings/MaintenancePage'; import StatisticsPage from './admin/StatisticsPage'; -import MainController from './MainController'; +import CachingController from './CachingController'; const App = () => { const initialized = useSelector(state => !!state.session.server && !!state.session.user); @@ -39,7 +39,7 @@ const App = () => { <> - + diff --git a/modern/src/CachingController.js b/modern/src/CachingController.js new file mode 100644 index 0000000..7adb39b --- /dev/null +++ b/modern/src/CachingController.js @@ -0,0 +1,22 @@ +import { useDispatch, useSelector } from 'react-redux'; +import { connect } from 'react-redux'; +import { geofencesActions } from './store'; +import { useEffectAsync } from './reactHelper'; + + +const CachingController = () => { + const authenticated = useSelector(state => !!state.session.user); + const dispatch = useDispatch(); + + useEffectAsync(async () => { + if (authenticated) { + const response = await fetch('/api/geofences'); + if (response.ok) { + dispatch(geofencesActions.update(await response.json())); + } + } + }, [authenticated]); + return null; +} + +export default connect()(CachingController); diff --git a/modern/src/MainController.js b/modern/src/MainController.js deleted file mode 100644 index f8f6d68..0000000 --- a/modern/src/MainController.js +++ /dev/null @@ -1,22 +0,0 @@ -import { useDispatch, useSelector } from 'react-redux'; -import { connect } from 'react-redux'; -import { geofencesActions } from './store'; -import { useEffectAsync } from './reactHelper'; - - -const MainController = () => { - const authenticated = useSelector(state => !!state.session.user); - const dispatch = useDispatch(); - - useEffectAsync(async () => { - if (authenticated) { - const response = await fetch('/api/geofences'); - if (response.ok) { - dispatch(geofencesActions.update(await response.json())); - } - } - }, [authenticated]); - return null; -} - -export default connect()(MainController); -- cgit v1.2.3 From 2691e78cd4d5486581656a490b9fb5386e68a5d9 Mon Sep 17 00:00:00 2001 From: Ashutosh Bishnoi Date: Mon, 19 Apr 2021 13:12:57 +0530 Subject: More code formatting --- modern/src/App.js | 1 - modern/src/CachingController.js | 2 +- modern/src/reports/EventReportPage.js | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/modern/src/App.js b/modern/src/App.js index 7f8a582..5fd10de 100644 --- a/modern/src/App.js +++ b/modern/src/App.js @@ -29,7 +29,6 @@ import ComputedAttributePage from './settings/ComputedAttributePage'; import MaintenancesPage from './settings/MaintenancesPage'; import MaintenancePage from './settings/MaintenancePage'; import StatisticsPage from './admin/StatisticsPage'; - import CachingController from './CachingController'; const App = () => { diff --git a/modern/src/CachingController.js b/modern/src/CachingController.js index 7adb39b..fa3b9f6 100644 --- a/modern/src/CachingController.js +++ b/modern/src/CachingController.js @@ -3,7 +3,6 @@ import { connect } from 'react-redux'; import { geofencesActions } from './store'; import { useEffectAsync } from './reactHelper'; - const CachingController = () => { const authenticated = useSelector(state => !!state.session.user); const dispatch = useDispatch(); @@ -16,6 +15,7 @@ const CachingController = () => { } } }, [authenticated]); + return null; } diff --git a/modern/src/reports/EventReportPage.js b/modern/src/reports/EventReportPage.js index 2478cad..a14fc93 100644 --- a/modern/src/reports/EventReportPage.js +++ b/modern/src/reports/EventReportPage.js @@ -63,7 +63,7 @@ const EventReportPage = () => { const [items, setItems] = useState([]); const formatGeofence = value => { - if (value !== 0) { + if (value > 0) { const geofence = geofences[value]; return geofence ? geofence.name : ''; } -- cgit v1.2.3