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

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 && 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;