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

import {
  Accordion,
  AccordionSummary,
  AccordionDetails,
  Typography,
  FormControlLabel,
  Checkbox,
  FormGroup,
} from '@mui/material';
import makeStyles from '@mui/styles/makeStyles';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import { useTranslation, useTranslationKeys } from '../common/components/LocalizationProvider';
import EditItemView from './components/EditItemView';
import { prefixString, unprefixString } from '../common/util/stringUtils';
import SelectField from '../common/components/SelectField';
import SettingsMenu from './components/SettingsMenu';

const useStyles = makeStyles((theme) => ({
  details: {
    display: 'flex',
    flexDirection: 'column',
    gap: theme.spacing(2),
    paddingBottom: theme.spacing(3),
  },
}));

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

  const [item, setItem] = useState();

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

  const validate = () => item && item.type && item.notificators;

  return (
    <EditItemView
      endpoint="notifications"
      item={item}
      setItem={setItem}
      validate={validate}
      menu={<SettingsMenu />}
      breadcrumbs={['settingsTitle', 'sharedNotification']}
    >
      {item && (
        <Accordion defaultExpanded>
          <AccordionSummary expandIcon={<ExpandMoreIcon />}>
            <Typography variant="subtitle1">
              {t('sharedRequired')}
            </Typography>
          </AccordionSummary>
          <AccordionDetails className={classes.details}>
            <SelectField
              value={item.type}
              emptyValue={null}
              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')}
            />
            <SelectField
              multiple
              value={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')}
            />
            {(!item.type || item.type === 'alarm') && (
              <SelectField
                multiple
                value={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')}
              />
            )}
            <FormGroup>
              <FormControlLabel
                control={(
                  <Checkbox
                    checked={item.always}
                    onChange={(event) => setItem({ ...item, always: event.target.checked })}
                  />
                  )}
                label={t('notificationAlways')}
              />
            </FormGroup>
          </AccordionDetails>
        </Accordion>
      )}
    </EditItemView>
  );
};

export default NotificationPage;