import { FormControl, FormHelperText, Input, 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 CreateChildUserModal({ isOpen, onClose, onSuccess }) {
const { t } = useTranslation('settings')
const { ResponsiveModal } = useResponsiveModal()
const [childName, setChildName] = useState('')
const [displayName, setDisplayName] = useState('')
const [password, setPassword] = useState('')
const [confirmPassword, setConfirmPassword] = useState('')
const [errors, setErrors] = useState({})
const [touched, setTouched] = useState({})
const [isSubmitting, setIsSubmitting] = useState(false)
useEffect(() => {
const newErrors = {}
if (touched.childName) {
if (!childName.trim()) {
newErrors.childName = t('childUsers.errNameRequired')
} else if (childName.length < 2) {
newErrors.childName = t('childUsers.errNameMin')
} else if (childName.length > 20) {
newErrors.childName = t('childUsers.errNameMax')
} else if (!/^[a-z.-]+$/.test(childName)) {
newErrors.childName = t('childUsers.errNameChars')
}
}
if (touched.password) {
if (!password) {
newErrors.password = t('childUsers.errPasswordRequired')
} else if (password.length < 8) {
newErrors.password = t('childUsers.errPasswordLength')
} else if (password.length > 64) {
newErrors.password = t('childUsers.errPasswordLength')
}
}
if (touched.confirmPassword) {
if (password !== confirmPassword) {
newErrors.confirmPassword = t('childUsers.errPasswordMatch')
}
}
if (touched.displayName && displayName.length > 50) {
newErrors.displayName = t('childUsers.errDisplayNameMax')
}
setErrors(newErrors)
}, [childName, displayName, password, confirmPassword, touched, t])
const handleSubmit = async () => {
setTouched({
childName: true,
password: true,
confirmPassword: true,
displayName: true,
})
if (Object.keys(errors).length > 0) {
return
}
setIsSubmitting(true)
try {
await onSuccess({
childName: childName.trim(),
displayName: displayName.trim() || childName.trim(),
password,
})
handleClose()
} catch (error) {
console.error('Failed to create child user:', error)
} finally {
setIsSubmitting(false)
}
}
const handleClose = () => {
setChildName('')
setDisplayName('')
setPassword('')
setConfirmPassword('')
setErrors({})
setTouched({})
setIsSubmitting(false)
onClose()
}
const isValid =
Object.keys(errors).length === 0 &&
childName.trim() &&
password &&
password === confirmPassword
return (
}
>
{t('childUsers.nameLabel')}
{
setChildName(e.target.value)
setTouched(prev => ({ ...prev, childName: true }))
}}
/>
{errors.childName && (
{errors.childName}
)}
{t('childUsers.displayNameLabel')}
{
setDisplayName(e.target.value)
setTouched(prev => ({ ...prev, displayName: true }))
}}
/>
{errors.displayName && (
{errors.displayName}
)}
{t('childUsers.passwordLabel')}
{
setPassword(e.target.value)
setTouched(prev => ({ ...prev, password: true }))
}}
/>
{errors.password && {errors.password}}
{t('childUsers.confirmPasswordLabel')}
{
setConfirmPassword(e.target.value)
setTouched(prev => ({ ...prev, confirmPassword: true }))
}}
/>
{errors.confirmPassword && (
{errors.confirmPassword}
)}
)
}
export default CreateChildUserModal