From ff8e5ad793352facbd6200d0b059a0c970b6da84 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 26 Jun 2022 19:11:48 -0700 Subject: Computed attributes testing --- modern/src/settings/ComputedAttributePage.js | 181 +++++++++++++++++---------- 1 file changed, 117 insertions(+), 64 deletions(-) (limited to 'modern') diff --git a/modern/src/settings/ComputedAttributePage.js b/modern/src/settings/ComputedAttributePage.js index c9170650..287a4340 100644 --- a/modern/src/settings/ComputedAttributePage.js +++ b/modern/src/settings/ComputedAttributePage.js @@ -11,6 +11,8 @@ import { TextField, createFilterOptions, Autocomplete, + Button, + Snackbar, } from '@mui/material'; import makeStyles from '@mui/styles/makeStyles'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; @@ -18,6 +20,9 @@ import EditItemView from './components/EditItemView'; import { useTranslation } from '../common/components/LocalizationProvider'; import usePositionAttributes from '../common/attributes/usePositionAttributes'; import SettingsMenu from './components/SettingsMenu'; +import SelectField from '../common/components/SelectField'; +import { useCatch } from '../reactHelper'; +import { snackBarDurationLongMs } from '../common/util/duration'; const useStyles = makeStyles((theme) => ({ details: { @@ -35,6 +40,8 @@ const ComputedAttributePage = () => { const positionAttributes = usePositionAttributes(t); const [item, setItem] = useState(); + const [deviceId, setDeviceId] = useState(); + const [result, setResult] = useState(); const options = Object.entries(positionAttributes).filter(([, value]) => !value.property).map(([key, value]) => ({ key, @@ -46,6 +53,21 @@ const ComputedAttributePage = () => { stringify: (option) => option.name, }); + const testAttribute = useCatch(async () => { + const query = new URLSearchParams({ deviceId }); + const url = `/api/attributes/computed/test?${query.toString()}`; + const response = await fetch(url, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(item), + }); + if (response.ok) { + setResult(await response.text()); + } else { + throw Error(await response.text()); + } + }); + const validate = () => item && item.description && item.expression; return ( @@ -58,71 +80,102 @@ const ComputedAttributePage = () => { breadcrumbs={['settingsTitle', 'sharedComputedAttribute']} > {item && ( - - }> - - {t('sharedRequired')} - - - - setItem({ ...item, description: event.target.value })} - label={t('sharedDescription')} - /> - option.key === item.attribute) || item.attribute} - onChange={(_, option) => { - const attribute = option ? option.key || option : null; - if (option && option.type) { - setItem({ ...item, attribute, type: option.type }); - } else { - setItem({ ...item, attribute }); - } - }} - filterOptions={(options, params) => { - const filtered = filter(options, params); - if (params.inputValue) { - filtered.push({ - key: params.inputValue, - name: params.inputValue, - }); - } - return filtered; - }} - options={options} - getOptionLabel={(option) => option.name || option} - renderOption={(props, option) => ( -
  • - {option.name} -
  • - )} - renderInput={(params) => ( - - )} - freeSolo - /> - setItem({ ...item, expression: event.target.value })} - label={t('sharedExpression')} - multiline - rows={4} - /> - - {t('sharedType')} - setItem({ ...item, type: e.target.value })} + > + {t('sharedTypeString')} + {t('sharedTypeNumber')} + {t('sharedTypeBoolean')} + + +
    +
    + + }> + + {t('sharedTest')} + + + + setDeviceId(Number(e.target.value))} + endpoint="/api/devices" + label={t('sharedDevice')} + /> + + setResult(null)} + autoHideDuration={snackBarDurationLongMs} + message={result} + /> + + + )} ); -- cgit v1.2.3