import { FormControl, FormHelperText, Input, Typography } from '@mui/joy' import { useEffect, useState } from 'react' import ModalActions from '../../../components/common/ModalActions' import { useResponsiveModal } from '../../../hooks/useResponsiveModal' function CreateChildUserModal({ isOpen, onClose, onSuccess }) { 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 = 'Sub account name is required' } else if (childName.length < 2) { newErrors.childName = 'Sub account name must be at least 2 characters' } else if (childName.length > 20) { newErrors.childName = 'Sub account name must be less than 20 characters' } else if (!/^[a-z.-]+$/.test(childName)) { newErrors.childName = 'Sub account name can only contain lowercase letters, dot and dash' } } if (touched.password) { if (!password) { newErrors.password = 'Password is required' } else if (password.length < 8) { newErrors.password = 'Password must be between 8 and 64 characters' } else if (password.length > 64) { newErrors.password = 'Password must be between 8 and 64 characters' } } if (touched.confirmPassword) { if (password !== confirmPassword) { newErrors.confirmPassword = 'Passwords do not match' } } if (touched.displayName && displayName.length > 50) { newErrors.displayName = 'Display name must be less than 50 characters' } setErrors(newErrors) }, [childName, displayName, password, confirmPassword, touched]) 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 ( } > Sub Account Name * { setChildName(e.target.value) setTouched(prev => ({ ...prev, childName: true })) }} /> {errors.childName && ( {errors.childName} )} Display Name { setDisplayName(e.target.value) setTouched(prev => ({ ...prev, displayName: true })) }} /> {errors.displayName && ( {errors.displayName} )} Password * { setPassword(e.target.value) setTouched(prev => ({ ...prev, password: true })) }} /> {errors.password && {errors.password}} Confirm Password * { setConfirmPassword(e.target.value) setTouched(prev => ({ ...prev, confirmPassword: true })) }} /> {errors.confirmPassword && ( {errors.confirmPassword} )} ) } export default CreateChildUserModal