import {
FormControl,
FormHelperText,
Input,
Option,
Select,
Textarea,
Typography,
} from '@mui/joy'
import { useEffect, useState } from 'react'
import ModalActions from '../../../components/common/ModalActions'
import { useResponsiveModal } from '../../../hooks/useResponsiveModal'
import { useTranslation } from 'react-i18next'
function CreateThingModal({ isOpen, onClose, onSave, currentThing }) {
const { t } = useTranslation('things')
const { ResponsiveModal } = useResponsiveModal()
const [name, setName] = useState(currentThing?.name || '')
const [type, setType] = useState(currentThing?.type || 'number')
const [state, setState] = useState(currentThing?.state || '')
const [errors, setErrors] = useState({})
useEffect(() => {
if (type === 'boolean') {
if (state !== 'true' && state !== 'false') {
setState('false')
}
} else if (type === 'number') {
if (isNaN(state)) {
setState(0)
}
}
}, [type, state])
const isValid = () => {
const newErrors = {}
if (!name || name.trim() === '') {
newErrors.name = t('errName')
}
if (type === 'number' && isNaN(state)) {
newErrors.state = t('errStateNumber')
}
if (type === 'boolean' && !['true', 'false'].includes(state)) {
newErrors.state = t('errStateBool')
}
if ((type === 'text' && !state) || state.trim() === '') {
newErrors.state = t('errStateRequired')
}
setErrors(newErrors)
return Object.keys(newErrors).length === 0
}
const handleSave = () => {
if (!isValid()) {
return
}
onSave({ name, type, id: currentThing?.id, state: state || null })
onClose()
}
return (
}
>
{t('name')}
Type
{errors.type}
{type === 'text' && (
{t('value')}
setState(e.target.value)}
sx={{ minWidth: 300 }}
/>
{errors.state}
)}
{type === 'number' && (
{t('value')}
{
setState(e.target.value)
}}
sx={{ minWidth: 300 }}
/>
)}
{type === 'boolean' && (
Value
)}
)
}
export default CreateThingModal