import CancelIcon from '@mui/icons-material/Cancel'; import WarningIcon from '@mui/icons-material/Warning'; import { Button, Dialog, DialogTitle, DialogContent, DialogActions, InputAdornment, MenuItem, TextField, FormHelperText, Grid, Box, Typography } from '@mui/material'; import { useState, useEffect } from 'react'; import { DeviceValueUOM, DeviceValueUOM_s } from './types'; import type { DeviceValue } from './types'; import type Schema from 'async-validator'; import type { ValidateFieldsError } from 'async-validator'; import { ValidatedTextField } from 'components'; import { useI18nContext } from 'i18n/i18n-react'; import { updateValue } from 'utils'; import { validate } from 'validators'; type DashboardDevicesDialogProps = { open: boolean; onClose: () => void; onSave: (as: DeviceValue) => void; selectedItem: DeviceValue; validator: Schema; }; const DashboarDevicesDialog = ({ open, onClose, onSave, selectedItem, validator }: DashboardDevicesDialogProps) => { const { LL } = useI18nContext(); const [editItem, setEditItem] = useState(selectedItem); const [fieldErrors, setFieldErrors] = useState(); const updateFormValue = updateValue(setEditItem); useEffect(() => { if (open) { setFieldErrors(undefined); setEditItem(selectedItem); } }, [open, selectedItem]); const close = () => { onClose(); }; const save = async () => { try { setFieldErrors(undefined); await validate(validator, editItem); onSave(editItem); } catch (errors: any) { setFieldErrors(errors); } }; const setUom = (uom: number) => { switch (uom) { case DeviceValueUOM.HOURS: return LL.HOURS(); case DeviceValueUOM.MINUTES: return LL.MINUTES(); case DeviceValueUOM.SECONDS: return LL.SECONDS(); default: return DeviceValueUOM_s[uom]; } }; const showHelperText = (dv: DeviceValue) => { if (dv.h) { return dv.h; } if (dv.l) { return '[ ' + dv.l.join(' | ') + ' ]'; } let helperText = '<'; if (dv.u !== DeviceValueUOM.NONE) { helperText += 'n'; if (dv.m && dv.x) { helperText += ' between ' + dv.m + ' and ' + dv.x; } if (dv.s) { helperText += ' , step ' + dv.s; } } else { helperText += 'text'; } return helperText + '>'; }; return ( {selectedItem.v === '' && selectedItem.c ? LL.RUN_COMMAND() : LL.CHANGE_VALUE()} {editItem.id.slice(2)} {editItem.l ? ( {editItem.l.map((val) => ( {val} ))} ) : editItem.u !== DeviceValueUOM.NONE ? ( {setUom(editItem.u)} }} /> ) : ( )} format: {showHelperText(editItem)} ); }; export default DashboarDevicesDialog;