import { useRef, useState, FormEvent } from 'react' import SingleColumnLayout from '../../../layouts/SingleColumnLayout.tsx' import TextInput from '../../../components/TextInput.tsx' import PrimaryButton from '../../../components/PrimaryButton.tsx' import { AuthService } from '../authService.ts' import { useNavigate } from 'react-router-dom' import SecondaryNavButton from '../../../components/SecondaryNavButton.tsx' interface LoginPageProps { authService: AuthService } export default function LoginPage({ authService }: LoginPageProps) { const [isSubmitting, setIsSubmitting] = useState(false) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState(null) const usernameInputRef = useRef(null) const passwordInputRef = useRef(null) const navigate = useNavigate() const onSubmit = async (e: FormEvent) => { e.preventDefault() if (!username) { setError("you didn't username D:<") return } if (!password) { setError("you didn't password D:<") return } setError(null) setIsSubmitting(true) try { await authService.login(username, password) navigate('/') } catch (error: unknown) { setError(error instanceof Error ? error.message : 'something went terribly wrong') } finally { setIsSubmitting(false) } } return (
{isSubmitting ? 'wait...' : 'make login pls'} register instead? {error}
) }