aboutsummaryrefslogtreecommitdiff
path: root/modern/src/settings
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-09-27 21:54:39 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-09-27 21:54:39 -0700
commit236b10503612706ed914678d942ac604f2973f47 (patch)
treee557381dbdc721002346a0fc64b1a679280fa3f1 /modern/src/settings
parent96af05dbf2171c4e194cf67c3c93d134ef467e50 (diff)
downloadetbsa-traccar-web-236b10503612706ed914678d942ac604f2973f47.tar.gz
etbsa-traccar-web-236b10503612706ed914678d942ac604f2973f47.tar.bz2
etbsa-traccar-web-236b10503612706ed914678d942ac604f2973f47.zip
Implement notification settings
Diffstat (limited to 'modern/src/settings')
-rw-r--r--modern/src/settings/NotificationPage.js109
-rw-r--r--modern/src/settings/NotificationsPage.js82
2 files changed, 191 insertions, 0 deletions
diff --git a/modern/src/settings/NotificationPage.js b/modern/src/settings/NotificationPage.js
new file mode 100644
index 0000000..f09f3fe
--- /dev/null
+++ b/modern/src/settings/NotificationPage.js
@@ -0,0 +1,109 @@
+import React, { useState } from 'react';
+import TextField from '@material-ui/core/TextField';
+
+import t, { findStringKeys } from '../common/localization';
+import EditItemView from '../EditItemView';
+import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, FormControlLabel, Checkbox, FormControl, InputLabel, Select, MenuItem } from '@material-ui/core';
+import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
+import { useEffectAsync } from '../reactHelper';
+import { prefixString, unprefixString } from '../common/stringUtils';
+
+const useStyles = makeStyles(() => ({
+ details: {
+ flexDirection: 'column',
+ },
+}));
+
+const NotificationPage = () => {
+ const classes = useStyles();
+
+ const [item, setItem] = useState();
+ const [types, setTypes] = useState();
+ const [notificators, setNotificators] = useState();
+
+ const alarms = findStringKeys(it => it.startsWith('alarm')).map(it => ({
+ key: unprefixString('alarm', it),
+ name: t(it),
+ }));
+
+ useEffectAsync(async () => {
+ const response = await fetch('/api/notifications/types');
+ if (response.ok) {
+ setTypes(await response.json());
+ }
+ }, []);
+
+ useEffectAsync(async () => {
+ const response = await fetch('/api/notifications/notificators');
+ if (response.ok) {
+ setNotificators(await response.json());
+ }
+ }, []);
+
+ return (
+ <EditItemView endpoint="notifications" item={item} setItem={setItem}>
+ {item &&
+ <>
+ <Accordion defaultExpanded>
+ <AccordionSummary expandIcon={<ExpandMoreIcon />}>
+ <Typography variant="subtitle1">
+ {t('sharedRequired')}
+ </Typography>
+ </AccordionSummary>
+ <AccordionDetails className={classes.details}>
+ {types &&
+ <FormControl margin="normal" variant="filled">
+ <InputLabel>{t('sharedType')}</InputLabel>
+ <Select
+ native
+ defaultValue={item.type}
+ onChange={e => setItem({...item, type: e.target.value})}>
+ {types.map(it => (
+ <option key={it.type} value={it.type}>{t(prefixString('event', it.type))}</option>
+ ))}
+ </Select>
+ </FormControl>
+ }
+ {notificators &&
+ <FormControl margin="normal" variant="filled">
+ <InputLabel>{t('notificationNotificators')}</InputLabel>
+ <Select
+ multiple
+ defaultValue={item.notificators ? item.notificators.split(/[, ]+/) : []}
+ onChange={e => setItem({...item, notificators: e.target.value.join()})}>
+ {notificators.map(it => (
+ <MenuItem key={it.type} value={it.type}>{t(prefixString('notificator', it.type))}</MenuItem>
+ ))}
+ </Select>
+ </FormControl>
+ }
+ {item.type === 'alarm' &&
+ <FormControl margin="normal" variant="filled">
+ <InputLabel>{t('sharedAlarms')}</InputLabel>
+ <Select
+ multiple
+ defaultValue={item.attributes.alarms ? item.attributes.alarms.split(/[, ]+/) : []}
+ onChange={e => setItem({...item, attributes: {...item.attributes, alarms: e.target.value.join()}})}>
+ {alarms.map(it => (
+ <MenuItem key={it.key} value={it.key}>{it.name}</MenuItem>
+ ))}
+ </Select>
+ </FormControl>
+ }
+ <FormControlLabel
+ control={
+ <Checkbox
+ checked={item.always}
+ onChange={event => setItem({...item, always: event.target.checked})}
+ />
+ }
+ label={t('notificationAlways')} />
+ </AccordionDetails>
+ </Accordion>
+ </>
+ }
+ </EditItemView>
+ );
+}
+
+export default NotificationPage;
diff --git a/modern/src/settings/NotificationsPage.js b/modern/src/settings/NotificationsPage.js
new file mode 100644
index 0000000..15da0de
--- /dev/null
+++ b/modern/src/settings/NotificationsPage.js
@@ -0,0 +1,82 @@
+import React, { useState } from 'react';
+import MainToolbar from '../MainToolbar';
+import { TableContainer, Table, TableRow, TableCell, TableHead, TableBody, makeStyles, IconButton } from '@material-ui/core';
+import MoreVertIcon from '@material-ui/icons/MoreVert';
+import t from '../common/localization';
+import { useEffectAsync } from '../reactHelper';
+import EditCollectionView from '../EditCollectionView';
+import { prefixString } from '../common/stringUtils';
+import { formatBoolean } from '../common/formatter';
+
+const useStyles = makeStyles(theme => ({
+ columnAction: {
+ width: theme.spacing(1),
+ padding: theme.spacing(0, 1),
+ },
+}));
+
+const NotificationsView = ({ updateTimestamp, onMenuClick }) => {
+ const classes = useStyles();
+
+ const [items, setItems] = useState([]);
+
+ useEffectAsync(async () => {
+ const response = await fetch('/api/notifications');
+ if (response.ok) {
+ setItems(await response.json());
+ }
+ }, [updateTimestamp]);
+
+ const formatList = (prefix, value) => {
+ if (value) {
+ return value
+ .split(/[, ]+/)
+ .filter(Boolean)
+ .map(it => t(prefixString(prefix, it)))
+ .join(', ');
+ }
+ return '';
+ };
+
+ return (
+ <TableContainer>
+ <Table>
+ <TableHead>
+ <TableRow>
+ <TableCell className={classes.columnAction} />
+ <TableCell>{t('notificationType')}</TableCell>
+ <TableCell>{t('notificationAlways')}</TableCell>
+ <TableCell>{t('sharedAlarms')}</TableCell>
+ <TableCell>{t('notificationNotificators')}</TableCell>
+ </TableRow>
+ </TableHead>
+ <TableBody>
+ {items.map(item => (
+ <TableRow key={item.id}>
+ <TableCell className={classes.columnAction} padding="none">
+ <IconButton onClick={(event) => onMenuClick(event.currentTarget, item.id)}>
+ <MoreVertIcon />
+ </IconButton>
+ </TableCell>
+ <TableCell>{t(prefixString('event', item.type))}</TableCell>
+ <TableCell>{formatBoolean(item.always)}</TableCell>
+ <TableCell>{formatList('alarm', item.attributes.alarms)}</TableCell>
+ <TableCell>{formatList('notificator', item.notificators)}</TableCell>
+ </TableRow>
+ ))}
+ </TableBody>
+ </Table>
+ </TableContainer>
+ );
+}
+
+const NotificationsPage = () => {
+ return (
+ <>
+ <MainToolbar />
+ <EditCollectionView content={NotificationsView} editPath="/settings/notification" endpoint="notifications" />
+ </>
+ );
+}
+
+export default NotificationsPage;