import React, { type ChangeEventHandler, Fragment, useState } from 'react' import { useModalStore } from '../store/useModalStore' import { useAuth } from '../contexts/AuthContext' import { Dialog, DialogTitle, TextField, Button, CircularProgress } from '@mui/material' import { type FormData } from '../@types' interface Props {} const AuthModal: React.FC = () => { const { login, register } = useAuth() const { currentModal, setCurrentModal } = useModalStore() const isRegisterMode = currentModal === 'REGISTER' const isOpen = ['AUTH', 'LOGIN', 'REGISTER'].includes(currentModal) const onClose = () => setCurrentModal('') const [formData, setFormData] = useState({ username: '', password: '' }) const [loading, setLoading] = useState(false) const [error, setError] = useState('') const handleChange: ChangeEventHandler = (e) => { const { name, value } = e.target setFormData((prev) => ({ ...prev, [name]: value })) } const clickSubmit = async () => { setLoading(true) setError('') try { isRegisterMode ? await register(formData) : await login(formData) onClose() } catch (error: any) { setError(typeof error === 'string' ? error : JSON.stringify(error)) } setLoading(false) } const isSubmitButtonDisabled = !formData['username'] || !formData['password'] return ( {isRegisterMode ? Create a new account : Login to your account} {error && {error}} {loading ? (
) : isRegisterMode ? ( ) : ( )}
) } export default AuthModal