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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
import React, { useState } from 'react';
import {
Accordion,
AccordionSummary,
AccordionDetails,
Typography,
FormControlLabel,
Checkbox,
FormGroup,
Button,
} from '@mui/material';
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';
import { useCatch } from '../reactHelper';
import useSettingsStyles from './common/useSettingsStyles';
const NotificationPage = () => {
const classes = useSettingsStyles();
const t = useTranslation();
const [item, setItem] = useState();
const alarms = useTranslationKeys((it) => it.startsWith('alarm')).map((it) => ({
key: unprefixString('alarm', it),
name: t(it),
}));
const testNotificators = useCatch(async () => {
await Promise.all(item.notificators.split(/[, ]+/).map(async (notificator) => {
const response = await fetch(`/api/notifications/test/${notificator}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(item),
});
if (!response.ok) {
throw Error(await response.text());
}
}));
});
const validate = () => item && item.type && item.notificators && (!item.notificators?.includes('command') || item.commandId);
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')}
/>
{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')}
/>
)}
<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.notificators?.includes('command') && (
<SelectField
value={item.commandId || 0}
onChange={(event) => setItem({ ...item, commandId: Number(event.target.value) })}
endpoint="/api/commands"
titleGetter={(it) => it.description}
label={t('sharedSavedCommand')}
/>
)}
<Button
variant="outlined"
color="primary"
onClick={testNotificators}
disabled={!item.notificators}
>
{t('sharedTestNotificators')}
</Button>
<FormGroup>
<FormControlLabel
control={(
<Checkbox
checked={item.always}
onChange={(event) => setItem({ ...item, always: event.target.checked })}
/>
)}
label={t('notificationAlways')}
/>
</FormGroup>
</AccordionDetails>
</Accordion>
<Accordion>
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
<Typography variant="subtitle1">
{t('sharedExtra')}
</Typography>
</AccordionSummary>
<AccordionDetails className={classes.details}>
<SelectField
value={item.calendarId || 0}
onChange={(event) => setItem({ ...item, calendarId: Number(event.target.value) })}
endpoint="/api/calendars"
label={t('sharedCalendar')}
/>
</AccordionDetails>
</Accordion>
</>
)}
</EditItemView>
);
};
export default NotificationPage;
|