import env from '@beam-australia/react-env'; import { joiResolver } from '@hookform/resolvers/joi'; import { Google, Login, Visibility, VisibilityOff } from '@mui/icons-material'; import { Button, IconButton, InputAdornment, TextField } from '@mui/material'; import Joi from 'joi'; import { isEmpty } from 'lodash'; import { Trans, useTranslation } from 'next-i18next'; import { useMemo, useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; import toast from 'react-hot-toast'; import { useIsMutating, useMutation } from 'react-query'; import BaseModal from '@/components/shared/BaseModal'; import { FLAG_DISABLE_SIGNUPS } from '@/constants/flags'; import { login, LoginParams, loginWithGoogle, LoginWithGoogleParams } from '@/services/auth'; import { ServerError } from '@/services/axios'; import { useAppDispatch, useAppSelector } from '@/store/hooks'; import { setModalState } from '@/store/modal/modalSlice'; declare const google: any; type FormData = { identifier: string; password: string; }; const defaultState: FormData = { identifier: '', password: '', }; const schema = Joi.object({ identifier: Joi.string().required(), password: Joi.string().min(6).required(), }); const LoginModal: React.FC = () => { const { t } = useTranslation(); const dispatch = useAppDispatch(); const [showPassword, setShowPassword] = useState(false); const isMutating = useIsMutating(); const isLoading = useMemo(() => isMutating > 0, [isMutating]); const { open: isOpen } = useAppSelector((state) => state.modal['auth.login']); const { reset, control, handleSubmit } = useForm({ defaultValues: defaultState, resolver: joiResolver(schema), }); const { mutateAsync: loginMutation } = useMutation(login); const { mutateAsync: loginWithGoogleMutation } = useMutation( loginWithGoogle ); const handleClose = () => { dispatch(setModalState({ modal: 'auth.login', state: { open: false } })); reset(); }; const onSubmit = async ({ identifier, password }: FormData) => { await loginMutation( { identifier, password }, { onError: (error) => { toast.error(error.message); }, } ); handleClose(); }; const handleCreateAccount = () => { handleClose(); dispatch(setModalState({ modal: 'auth.register', state: { open: true } })); }; const handleRecoverAccount = () => { handleClose(); dispatch(setModalState({ modal: 'auth.forgot', state: { open: true } })); }; const handleLoginWithGoogle = async () => { google.accounts.id.initialize({ client_id: env('GOOGLE_CLIENT_ID'), callback: async (response: any) => { await loginWithGoogleMutation({ credential: response.credential }); handleClose(); }, auto_select: false, }); google.accounts.id.prompt(); }; const PasswordVisibility = (): React.ReactElement => { const handleToggle = () => setShowPassword((showPassword) => !showPassword); return ( {showPassword ? : } ); }; return ( } isOpen={isOpen} heading={t('modals.auth.login.heading')} handleClose={handleClose} footerChildren={
{!isEmpty(env('GOOGLE_CLIENT_ID')) && ( )}
} >

{t('modals.auth.login.body')}

( ('modals.auth.login.form.username.label')} error={!!fieldState.error} helperText={fieldState.error?.message || t('modals.auth.login.form.username.help-text')} {...field} /> )} /> ( ('modals.auth.login.form.password.label')} error={!!fieldState.error} helperText={fieldState.error?.message} InputProps={{ endAdornment: }} {...field} /> )} /> {!FLAG_DISABLE_SIGNUPS && (

If you don't have one, you can create an account here.

)}

In case you have forgotten your password, you can recover your account here.

); }; export default LoginModal;