import { h, type FunctionComponent, type TargetedSubmitEvent } from 'preact' import type { FetchError } from 'rek' import { useCallback } from 'preact/hooks' import { useCurrentUser } from '../contexts/current_user.ts' import rek from 'rek' import useRequestState from '../../shared/hooks/use_request_state.ts' import Button from './button.tsx' import Input from './input.tsx' import Message from './message.tsx' import s from './login_form.module.scss' const LoginForm: FunctionComponent = () => { const [{ error, pending }, actions] = useRequestState() const { setUser } = useCurrentUser() const login = useCallback(async (e: TargetedSubmitEvent) => { e.preventDefault() actions.pending() const form = e.currentTarget try { const result = await rek.post('/auth/login', { email: form.email.value, password: form.password.value, }) actions.success() setUser(result) } catch (err) { actions.error(err as FetchError) } }, []) return (
{error && Error: {error.body?.message || error.message}}
) } export default LoginForm