aboutsummaryrefslogtreecommitdiff
path: root/modern/src/MainPage.js
blob: a118f17f53b955f6e8a4e882bb4b100bc9e502fb (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, { useEffect } from 'react';
import { useHistory } from 'react-router-dom';
import {
  makeStyles, Paper, Toolbar, TextField, IconButton, Button,
} from '@material-ui/core';
import { useDispatch, useSelector } from 'react-redux';
import { devicesActions } from './store/devices';
import { uiActions } from './store/ui';

import { useTheme } from '@material-ui/core/styles';
import useMediaQuery from '@material-ui/core/useMediaQuery';
import AddIcon from '@material-ui/icons/Add';
import CloseIcon from '@material-ui/icons/Close';
import ArrowBackIcon from '@material-ui/icons/ArrowBack';
import ListIcon from '@material-ui/icons/ViewList';

import DevicesList from './DevicesList';
import Map from './map/Map';
import SelectedDeviceMap from './map/SelectedDeviceMap';
import AccuracyMap from './map/AccuracyMap';
import GeofenceMap from './map/GeofenceMap';
import CurrentPositionsMap from './map/CurrentPositionsMap';
import CurrentLocationMap from './map/CurrentLocationMap';
import BottomMenu from './components/BottomMenu';
import { useTranslation } from './LocalizationProvider';
import PoiMap from './map/PoiMap';
import MapPadding from './map/MapPadding';
const useStyles = makeStyles((theme) => ({
  root: {
    height: '100vh',
  },
  sidebar: {
    display: 'flex',
    flexDirection: 'column',
    position: 'absolute',
    left: 0,
    top: 0,
    margin: theme.spacing(1.5),
    width: '450px',
    bottom: 56,
    zIndex: 1301,
    transition: 'transform .5s ease',
    backgroundColor: 'white',
    [theme.breakpoints.down('sm')]: {
      width: '100%',
      margin: 0,
    },
  },
  sidebarCollapsed: {
    transform: `translateX(-450px)`,
    marginLeft: 0,
    [theme.breakpoints.down('sm')]: {
      transform: 'translateX(-100vw)',
    },
  },
  paper: {
    zIndex: 1,
  },
  toolbar: {
    display: 'flex',
    padding: theme.spacing(0, 1),
    '& > *': {
      margin: theme.spacing(0, 1),
    },
  },
  deviceList: {
    flex: 1,
  },
  sidebarToggle: {
    position: 'absolute',
    left: theme.spacing(1.5),
    top: theme.spacing(3),
    borderRadius: '0px',
    minWidth: 0,
    [theme.breakpoints.down('md')]: {
      left: 0,
    },
  },
  sidebarToggleText: {
    marginLeft: theme.spacing(1),
    [theme.breakpoints.only('xs')]: {
      display: 'none',
    },
  },
  sidebarToggleBg: {
    backgroundColor: 'white',
    color: '#777777',
    '&:hover': {
      backgroundColor: 'white',
    },
  },
}));

const MainPage = () => {
  const classes = useStyles();
  const history = useHistory();
  const theme = useTheme();
  const dispatch = useDispatch();
  const t = useTranslation();

  const isPhone = useMediaQuery(theme.breakpoints.down('sm'));
  const isWide = useMediaQuery(theme.breakpoints.up('md')/* || theme.breakpoints.up('sm') || theme.breakpoints.up('xs')*/);

  const collapsed = useSelector((state) => state.ui.collapsed);

  const handleClose = () => {
    dispatch(uiActions.setCollapsed(!collapsed));
  };

  const filterTerm = useSelector((state) => state.devices.filterTerm);
  
  const setFilterTerm = (text) => {
    dispatch(devicesActions.setFilter(text));
  }

  const clearFilter = () => {
    dispatch(devicesActions.clearFilter());
  }

  useEffect(() => uiActions.setCollapsed(isPhone), [isPhone]);

  return (
    <div className={classes.root}>
      <Map>
        {isWide && <MapPadding left={parseInt(theme.dimensions.drawerWidthDesktop, 10)} />}
        <GeofenceMap />
        <AccuracyMap />
        <CurrentPositionsMap />
        <SelectedDeviceMap />
        <PoiMap />
      </Map>
      <Button
        variant="contained"
        color={isPhone ? 'secondary' : 'primary'}
        classes={{ containedPrimary: classes.sidebarToggleBg }}
        className={classes.sidebarToggle}
        onClick={handleClose}
        disableElevation
      >
        <ListIcon />
        <div className={classes.sidebarToggleText}>{t('deviceTitle')}</div>
      </Button>
      <Paper square elevation={3} className={`${classes.sidebar} ${(!isWide && collapsed) && classes.sidebarCollapsed}`}>
        <Paper className={classes.paper} square elevation={3}>
          <Toolbar className={classes.toolbar} disableGutters>
            {!isWide && (
            <IconButton onClick={handleClose}>
              <ArrowBackIcon />
            </IconButton>
            )}
            <TextField
              fullWidth
              name="deviceName"
              value={filterTerm}
              autoFocus
              onChange={(event) => setFilterTerm(event.target.value) }
              placeholder={t("sharedSearch")}
              variant="filled"
            />
            {filterTerm.length > 0 && (
              <IconButton onClick={() => clearFilter() }>
                <CloseIcon />
              </IconButton>
            )}
            <IconButton onClick={() => history.push('/device')}>
              <AddIcon />
            </IconButton>
          </Toolbar>
        </Paper>
        <div className={classes.deviceList}>
          <DevicesList />
        </div>
      </Paper>
      <BottomMenu />
    </div>
  );
};

export default MainPage;