import env from '@beam-australia/react-env'; import { joiResolver } from '@hookform/resolvers/joi'; import { Google, HowToReg } from '@mui/icons-material'; import { Button, TextField } from '@mui/material'; import Joi from 'joi'; import { isEmpty } from 'lodash'; import { Trans, useTranslation } from 'next-i18next'; import { Controller, useForm } from 'react-hook-form'; import { useMutation } from 'react-query'; import BaseModal from '@/components/shared/BaseModal'; import { loginWithGoogle, LoginWithGoogleParams, register as registerUser, RegisterParams } 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 = { name: string; username: string; email: string; password: string; confirmPassword: string; }; const defaultState: FormData = { name: '', username: '', email: '', password: '', confirmPassword: '', }; const schema = Joi.object({ name: Joi.string().required(), username: Joi.string() .lowercase() .min(3) .regex(/^[a-z0-9-]+$/, 'only lowercase characters, numbers and hyphens') .required(), email: Joi.string() .email({ tlds: { allow: false } }) .required(), password: Joi.string().min(6).required(), confirmPassword: Joi.string().min(6).required().valid(Joi.ref('password')), }); const RegisterModal: React.FC = () => { const { t } = useTranslation(); const dispatch = useAppDispatch(); const { open: isOpen } = useAppSelector((state) => state.modal['auth.register']); const { reset, control, handleSubmit } = useForm({ defaultValues: defaultState, resolver: joiResolver(schema), }); const { mutateAsync, isLoading } = useMutation(registerUser); const { mutateAsync: loginWithGoogleMutation } = useMutation( loginWithGoogle ); const handleClose = () => { dispatch(setModalState({ modal: 'auth.register', state: { open: false } })); reset(); }; const onSubmit = async ({ name, username, email, password }: FormData) => { await mutateAsync({ name, username, email, password }); handleClose(); }; const handleLogin = () => { handleClose(); dispatch(setModalState({ modal: 'auth.login', 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(); }; return ( } isOpen={isOpen} heading={t('modals.auth.register.heading')} handleClose={handleClose} footerChildren={
{!isEmpty(env('GOOGLE_CLIENT_ID')) && ( )}
} >

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

( ('modals.auth.register.form.name.label')} error={!!fieldState.error} helperText={fieldState.error?.message} {...field} /> )} /> ( ('modals.auth.register.form.username.label')} error={!!fieldState.error} helperText={fieldState.error?.message} {...field} /> )} /> ( ('modals.auth.register.form.email.label')} className="col-span-2" error={!!fieldState.error} helperText={fieldState.error?.message} {...field} /> )} /> ( ('modals.auth.register.form.password.label')} error={!!fieldState.error} helperText={fieldState.error?.message} {...field} /> )} /> ( ('modals.auth.register.form.confirm-password.label')} error={!!fieldState.error} helperText={fieldState.error?.message} {...field} /> )} />

If you already have an account, you can login here.

); }; export default RegisterModal;