aboutsummaryrefslogtreecommitdiff
path: root/modern/src/settings/NotificationPage.js
blob: 2cf8a62f1f7bb0c28b06d42bf0adf81e91889241 (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
import React, { useState } from 'react';

import t, { findStringKeys } from '../common/localization';
import EditItemView from '../EditItemView';
import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, FormControlLabel, Checkbox } from '@material-ui/core';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import { prefixString, unprefixString } from '../common/stringUtils';
import SelectField from '../form/SelectField';

const useStyles = makeStyles(() => ({
  details: {
    flexDirection: 'column',
  },
}));

const NotificationPage = () => {
  const classes = useStyles();

  const [item, setItem] = useState();

  const alarms = findStringKeys(it => it.startsWith('alarm')).map(it => ({
    key: unprefixString('alarm', it),
    name: t(it),
  }));

  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}>
              <SelectField
                margin="normal"
                defaultValue={item.type}
                onChange={e => setItem({...item, type: e.target.value})}
                endpoint="/api/notifications/types"
                keyGetter={it => it.type}
                titleGetter={it => t(prefixString('event', it.type))}
                label={t('sharedType')}
                variant="filled" />
              <SelectField
                multiple
                margin="normal"
                defaultValue={item.notificators ? item.notificators.split(/[, ]+/) : []}
                onChange={e => setItem({...item, notificators: e.target.value.join()})}
                endpoint="/api/notifications/notificators"
                keyGetter={it => it.type}
                titleGetter={it => t(prefixString('notificator', it.type))}
                label={t('notificationNotificators')}
                variant="filled" />
              {item.type === 'alarm' &&
                <SelectField
                  multiple
                  margin="normal"
                  defaultValue={item.attributes && item.attributes.alarms ? item.attributes.alarms.split(/[, ]+/) : []}
                  onChange={e => setItem({...item, attributes: {...item.attributes, alarms: e.target.value.join()}})}
                  data={alarms}
                  keyGetter={it => it.key}
                  label={t('sharedAlarms')}
                  variant="filled" />
              }
              <FormControlLabel
                control={
                  <Checkbox
                    checked={item.always}
                    onChange={event => setItem({...item, always: event.target.checked})}
                    />
                }
                label={t('notificationAlways')} />
            </AccordionDetails>
          </Accordion>
        </>
      }
    </EditItemView>
  );
}

export default NotificationPage;