import { Button, FormHelperText, Stack, TextField, Typography, } from '@mui/material'; import LoginGoogle from '../LoginGoogle'; import {useTranslation} from 'next-i18next'; import {useState} from 'react'; import {useSendMagicLinkMutation} from '../../generated/graphql'; type Props = { error?: string; showGoogleAuth?: boolean; onSend?: () => void; }; const LoginForm = (props: Props) => { const {error, showGoogleAuth, onSend} = props; const {t, i18n} = useTranslation(); const [email, setEmail] = useState(''); const [sent, setSent] = useState(false); const [magicLinkError, setMagicLinkError] = useState(null); const [sendMagicLink] = useSendMagicLinkMutation(); const handleSubmit = async () => { try { setMagicLinkError(null); if (email) await sendMagicLink({variables: {email, lang: i18n.language}}); setSent(true); onSend?.(); } catch (error) { console.error(error); if (error.message === 'GoogleAccount') setMagicLinkError(error.message); } }; return ( {(error || magicLinkError) && ( {t(errorsMap[error || magicLinkError])} )} {!sent && ( <> setEmail(e.target.value)} type="email" /> {showGoogleAuth && ( <> {t('signin.or')} )} )} {sent && ( {t`signin.check_email`} )} ); }; const errorsMap = { CredentialsSignin: 'signin.errors.CredentialsSignin', GoogleAccount: 'signin.errors.GoogleAccount', }; export default LoginForm;