From 66d2b91dfdcfb4b5c5fe90ddb7a3e3e66e01e502 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 30 Sep 2020 22:43:57 -0700 Subject: Migrate Select to SelectField --- modern/src/DevicePage.js | 54 ++++++++------------- modern/src/form/SelectField.js | 33 +++++++++---- modern/src/settings/NotificationPage.js | 85 ++++++++++++--------------------- 3 files changed, 73 insertions(+), 99 deletions(-) diff --git a/modern/src/DevicePage.js b/modern/src/DevicePage.js index 45eba3fa..d4b83e6e 100644 --- a/modern/src/DevicePage.js +++ b/modern/src/DevicePage.js @@ -3,11 +3,11 @@ import TextField from '@material-ui/core/TextField'; import t from './common/localization'; import EditItemView from './EditItemView'; -import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, FormControl, InputLabel, Select, FormControlLabel, Checkbox } from '@material-ui/core'; +import { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, FormControlLabel, Checkbox } from '@material-ui/core'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; -import { useEffectAsync } from './reactHelper'; import EditAttributesView from './attributes/EditAttributesView'; import deviceAttributes from './attributes/deviceAttributes'; +import SelectField from './form/SelectField'; const useStyles = makeStyles(() => ({ details: { @@ -43,14 +43,6 @@ const DevicePage = () => { const classes = useStyles(); const [item, setItem] = useState(); - const [groups, setGroups] = useState(); - - useEffectAsync(async () => { - const response = await fetch('/api/groups'); - if (response.ok) { - setGroups(await response.json()); - } - }, []); return ( @@ -84,20 +76,13 @@ const DevicePage = () => { - {groups && - - {t('groupParent')} - - - } + setItem({...item, groupId: Number(event.target.value)})} + endpoint="/api/groups" + label={t('groupParent')} + variant="filled" /> { onChange={event => setItem({...item, contact: event.target.value})} label={t('deviceContact')} variant="filled" /> - - {t('deviceCategory')} - - + setItem({...item, category: event.target.value})} + data={deviceCategories.map(category => ({ + id: category, + name: t(`category${category.replace(/^\w/, c => c.toUpperCase())}`) + }))} + label={t('deviceCategory')} + variant="filled" /> setItem({...item, disabled: event.target.checked})} />} label={t('sharedDisabled')} /> diff --git a/modern/src/form/SelectField.js b/modern/src/form/SelectField.js index 78a16887..fa51a366 100644 --- a/modern/src/form/SelectField.js +++ b/modern/src/form/SelectField.js @@ -1,14 +1,27 @@ -import { FormControl, InputLabel, Select } from '@material-ui/core'; +import { FormControl, InputLabel, MenuItem, Select } from '@material-ui/core'; import React, { useState } from 'react'; import { useEffectAsync } from '../reactHelper'; -const SelectField = ({ margin, variant, label, defaultValue, onChange, endpoint }) => { - const [items, setItems] = useState(); +const SelectField = ({ + margin, + variant, + label, + multiple, + defaultValue, + onChange, + endpoint, + data, + keyGetter = item => item.id, + titleGetter = item => item.name, +}) => { + const [items, setItems] = useState(data); useEffectAsync(async () => { - const response = await fetch(endpoint); - if (response.ok) { - setItems(await response.json()); + if (endpoint) { + const response = await fetch(endpoint); + if (response.ok) { + setItems(await response.json()); + } } }, []); @@ -17,12 +30,14 @@ const SelectField = ({ margin, variant, label, defaultValue, onChange, endpoint {label} diff --git a/modern/src/settings/NotificationPage.js b/modern/src/settings/NotificationPage.js index 5a772d0c..2cf8a62f 100644 --- a/modern/src/settings/NotificationPage.js +++ b/modern/src/settings/NotificationPage.js @@ -2,10 +2,10 @@ 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 { Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, FormControlLabel, Checkbox } from '@material-ui/core'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; -import { useEffectAsync } from '../reactHelper'; import { prefixString, unprefixString } from '../common/stringUtils'; +import SelectField from '../form/SelectField'; const useStyles = makeStyles(() => ({ details: { @@ -17,28 +17,12 @@ 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 ( {item && @@ -50,44 +34,35 @@ const NotificationPage = () => { - {types && - - {t('sharedType')} - - - } - {notificators && - - {t('notificationNotificators')} - - - } + 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" /> + 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' && - - {t('sharedAlarms')} - - + setItem({...item, attributes: {...item.attributes, alarms: e.target.value.join()}})} + data={alarms} + keyGetter={it => it.key} + label={t('sharedAlarms')} + variant="filled" /> }