aboutsummaryrefslogtreecommitdiff
path: root/modern/src/reports/ChartReportPage.js
blob: 80424cce9773b1bcc2929c7d9a8b1fb4dbc07be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, { useState } from 'react';
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 { speedFromKnots } from '../common/converter';
import t from '../common/localization';

const Filter = ({ children, setItems }) => {

  const speedUnit = useAttributePreference('speedUnit');

  const handleSubmit = async (deviceId, from, to, mail, headers) => {
    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(speedFromKnots(position.speed, speedUnit)),
          altitude: position.altitude,
          accuracy: position.accuracy,
          fixTime: formatDate(position.fixTime)
        }
      });
      setItems(formattedPositions);
    }
  }
  return (
    <>
      <ReportFilter handleSubmit={handleSubmit} showOnly />
      {children}
    </>
  )
}

const ChartType = ({ type, setType }) => {

  return (
    <FormControl variant="filled" margin="normal" fullWidth>
      <InputLabel>{t('reportChartType')}</InputLabel>
      <Select value={type} onChange={e => setType(e.target.value)}>
        <MenuItem value="speed">{t('positionSpeed')}</MenuItem>
        <MenuItem value="accuracy">{t('positionAccuracy')}</MenuItem>
        <MenuItem value="altitude">{t('positionAltitude')}</MenuItem>
      </Select>
    </FormControl>
  )
}


const ChartReportPage = () => {
  
  const [items, setItems] = useState([]);
  const [type, setType] = useState('speed');

  return (
    <>
      <ReportLayoutPage filter={ 
        <Filter setItems={setItems}>
          <ChartType type={type} setType={setType} />
        </Filter>
      }>
        <Graph items={items} type={type} />
      </ReportLayoutPage>
    </>
  )
}

export default ChartReportPage;