diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-11-03 13:58:37 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2020-11-03 13:58:37 -0800 |
commit | d53632348f684f35719b035ff39744a41c088f3e (patch) | |
tree | d693d971a58e96dd0aed072d5fbec45151c2c780 | |
parent | 3c9f0117b2b3cdffc98f4ea19e5dbc7ed40e9b4f (diff) | |
download | trackermap-web-d53632348f684f35719b035ff39744a41c088f3e.tar.gz trackermap-web-d53632348f684f35719b035ff39744a41c088f3e.tar.bz2 trackermap-web-d53632348f684f35719b035ff39744a41c088f3e.zip |
Show replay path
-rw-r--r-- | modern/src/RemoveDialog.js | 2 | ||||
-rw-r--r-- | modern/src/map/ReplayPathMap.js | 59 | ||||
-rw-r--r-- | modern/src/map/mapUtil.js | 24 | ||||
-rw-r--r-- | modern/src/reports/FilterForm.js | 88 | ||||
-rw-r--r-- | modern/src/reports/ReplayPage.js | 58 |
5 files changed, 204 insertions, 27 deletions
diff --git a/modern/src/RemoveDialog.js b/modern/src/RemoveDialog.js index 8e7d97f4..bbcfb226 100644 --- a/modern/src/RemoveDialog.js +++ b/modern/src/RemoveDialog.js @@ -8,7 +8,7 @@ import DialogContentText from '@material-ui/core/DialogContentText'; const RemoveDialog = ({ open, endpoint, itemId, onResult }) => { const handleRemove = async () => { - const response = await fetch(`/api/${endpoint}/${itemId}`, { method: 'DELETE' }) + const response = await fetch(`/api/${endpoint}/${itemId}`, { method: 'DELETE' }); if (response.ok) { onResult(true); } diff --git a/modern/src/map/ReplayPathMap.js b/modern/src/map/ReplayPathMap.js new file mode 100644 index 00000000..feab0718 --- /dev/null +++ b/modern/src/map/ReplayPathMap.js @@ -0,0 +1,59 @@ +import mapboxgl from 'mapbox-gl'; +import { useEffect } from 'react'; +import { map } from './Map'; + +const ReplayPathMap = ({ positions }) => { + const id = 'replay'; + + useEffect(() => { + map.addSource(id, { + 'type': 'geojson', + 'data': { + type: 'Feature', + geometry: { + type: 'LineString', + coordinates: [], + }, + }, + }); + map.addLayer({ + 'source': id, + 'id': id, + 'type': 'line', + 'layout': { + 'line-join': 'round', + 'line-cap': 'round', + }, + 'paint': { + 'line-color': '#333', + 'line-width': 5, + }, + }); + + return () => { + map.removeLayer(id); + map.removeSource(id); + }; + }, []); + + useEffect(() => { + const coordinates = positions.map(item => [item.longitude, item.latitude]); + map.getSource(id).setData({ + type: 'Feature', + geometry: { + type: 'LineString', + coordinates: coordinates, + }, + }); + if (coordinates.length) { + const bounds = coordinates.reduce((bounds, item) => bounds.extend(item), new mapboxgl.LngLatBounds(coordinates[0], coordinates[0])); + map.fitBounds(bounds, { + padding: { top: 50, bottom: 250, left: 25, right: 25 }, + }); + } + }, [positions]); + + return null; +} + +export default ReplayPathMap; diff --git a/modern/src/map/mapUtil.js b/modern/src/map/mapUtil.js index 71d7b3c9..15f16202 100644 --- a/modern/src/map/mapUtil.js +++ b/modern/src/map/mapUtil.js @@ -27,30 +27,6 @@ export const loadIcon = async (key, background, url) => { return context.getImageData(0, 0, canvas.width, canvas.height); }; -export const calculateBounds = features => { - if (features && features.length) { - const first = features[0].geometry.coordinates; - const bounds = [[...first], [...first]]; - for (let feature of features) { - const longitude = feature.geometry.coordinates[0] - const latitude = feature.geometry.coordinates[1] - if (longitude < bounds[0][0]) { - bounds[0][0] = longitude; - } else if (longitude > bounds[1][0]) { - bounds[1][0] = longitude; - } - if (latitude < bounds[0][1]) { - bounds[0][1] = latitude; - } else if (latitude > bounds[1][1]) { - bounds[1][1] = latitude; - } - } - return bounds; - } else { - return null; - } -} - export const reverseCoordinates = it => { if (!it) { return it; diff --git a/modern/src/reports/FilterForm.js b/modern/src/reports/FilterForm.js new file mode 100644 index 00000000..86339d2d --- /dev/null +++ b/modern/src/reports/FilterForm.js @@ -0,0 +1,88 @@ +import React, { useEffect, useState } from 'react'; +import { FormControl, InputLabel, Select, MenuItem, TextField } from '@material-ui/core'; +import t from '../common/localization'; +import { useSelector } from 'react-redux'; +import moment from 'moment'; + +const FilterForm = ({ deviceId, setDeviceId, from, setFrom, to, setTo }) => { + const devices = useSelector(state => Object.values(state.devices.items)); + + const [period, setPeriod] = useState('today'); + + useEffect(() => { + switch (period) { + default: + case 'today': + setFrom(moment().startOf('day')); + setTo(moment().endOf('day')); + break; + case 'yesterday': + setFrom(moment().subtract(1, 'day').startOf('day')); + setTo(moment().subtract(1, 'day').endOf('day')); + break; + case 'thisWeek': + setFrom(moment().startOf('week')); + setTo(moment().endOf('week')); + break; + case 'previousWeek': + setFrom(moment().subtract(1, 'week').startOf('week')); + setTo(moment().subtract(1, 'week').endOf('week')); + break; + case 'thisMonth': + setFrom(moment().startOf('month')); + setTo(moment().endOf('month')); + break; + case 'previousMonth': + setFrom(moment().subtract(1, 'month').startOf('month')); + setTo(moment().subtract(1, 'month').endOf('month')); + break; + } + }, [period, setFrom, setTo]); + + return ( + <> + <FormControl variant='filled' margin='normal' fullWidth> + <InputLabel>{t('reportDevice')}</InputLabel> + <Select value={deviceId || ''} onChange={e => setDeviceId(e.target.value)}> + {devices.map((device) => ( + <MenuItem key={device.id} value={device.id}>{device.name}</MenuItem> + ))} + </Select> + </FormControl> + <FormControl variant='filled' margin='normal' fullWidth> + <InputLabel>{t('reportPeriod')}</InputLabel> + <Select value={period} onChange={e => setPeriod(e.target.value)}> + <MenuItem key='today' value='today'>{t('reportToday')}</MenuItem> + <MenuItem key='yesterday' value='yesterday'>{t('reportYesterday')}</MenuItem> + <MenuItem key='thisWeek' value='thisWeek'>{t('reportThisWeek')}</MenuItem> + <MenuItem key='previousWeek' value='previousWeek'>{t('reportPreviousWeek')}</MenuItem> + <MenuItem key='thisMonth' value='thisMonth'>{t('reportThisMonth')}</MenuItem> + <MenuItem key='previousMonth' value='previousMonth'>{t('reportPreviousMonth')}</MenuItem> + <MenuItem key='custom' 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 /> + } + </> + ); +} + +export default FilterForm; diff --git a/modern/src/reports/ReplayPage.js b/modern/src/reports/ReplayPage.js index 09882840..bdc9edb4 100644 --- a/modern/src/reports/ReplayPage.js +++ b/modern/src/reports/ReplayPage.js @@ -1,7 +1,11 @@ -import React from 'react'; -import { Container, makeStyles, Paper, Slider } from '@material-ui/core'; +import React, { useState } from 'react'; +import { Accordion, AccordionDetails, AccordionSummary, Button, Container, FormControl, makeStyles, Paper, Slider, Typography } from '@material-ui/core'; +import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import MainToolbar from '../MainToolbar'; import Map from '../map/Map'; +import t from '../common/localization'; +import FilterForm from './FilterForm'; +import ReplayPathMap from '../map/ReplayPathMap'; const useStyles = makeStyles(theme => ({ root: { @@ -17,21 +21,71 @@ const useStyles = makeStyles(theme => ({ }, controlContent: { padding: theme.spacing(2), + marginBottom: theme.spacing(2), + }, + configForm: { + display: 'flex', + flexDirection: 'column', }, })); const ReplayPage = () => { const classes = useStyles(); + const [expanded, setExpanded] = useState(true); + + const [deviceId, setDeviceId] = useState(); + const [from, setFrom] = useState(); + const [to, setTo] = useState(); + + const [positions, setPositions] = useState([]); + + const handleShow = async () => { + const query = new URLSearchParams({ + deviceId, + from: from.toISOString(), + to: to.toISOString(), + }); + const response = await fetch(`/api/positions?${query.toString()}`, { headers: { 'Accept': 'application/json' } }); + if (response.ok) { + setPositions(await response.json()); + setExpanded(false); + } + }; + return ( <div className={classes.root}> <MainToolbar /> <Map> + <ReplayPathMap positions={positions} /> </Map> <Container maxWidth="sm" className={classes.controlPanel}> <Paper className={classes.controlContent}> <Slider defaultValue={30} /> </Paper> + <div> + <Accordion expanded={expanded} onChange={() => setExpanded(!expanded)}> + <AccordionSummary expandIcon={<ExpandMoreIcon />}> + <Typography align='center'> + {t('reportConfigure')} + </Typography> + </AccordionSummary> + <AccordionDetails className={classes.configForm}> + <FilterForm + deviceId={deviceId} + setDeviceId={setDeviceId} + from={from} + setFrom={setFrom} + to={to} + setTo={setTo} /> + <FormControl margin='normal' fullWidth> + <Button type='button' color='primary' variant='contained' disabled={!deviceId} onClick={handleShow}> + {t('reportShow')} + </Button> + </FormControl> + </AccordionDetails> + </Accordion> + </div> </Container> </div> ); |