diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-09-27 21:54:39 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2020-09-27 21:54:39 -0700 |
commit | 236b10503612706ed914678d942ac604f2973f47 (patch) | |
tree | e557381dbdc721002346a0fc64b1a679280fa3f1 /modern/src/settings/NotificationPage.js | |
parent | 96af05dbf2171c4e194cf67c3c93d134ef467e50 (diff) | |
download | trackermap-web-236b10503612706ed914678d942ac604f2973f47.tar.gz trackermap-web-236b10503612706ed914678d942ac604f2973f47.tar.bz2 trackermap-web-236b10503612706ed914678d942ac604f2973f47.zip |
Implement notification settings
Diffstat (limited to 'modern/src/settings/NotificationPage.js')
-rw-r--r-- | modern/src/settings/NotificationPage.js | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/modern/src/settings/NotificationPage.js b/modern/src/settings/NotificationPage.js new file mode 100644 index 00000000..f09f3fe2 --- /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; |