diff --git a/src/components/shared/auth/Login-form.tsx b/src/components/shared/auth/Login-form.tsx index aa9d6cf..d27cbec 100644 --- a/src/components/shared/auth/Login-form.tsx +++ b/src/components/shared/auth/Login-form.tsx @@ -16,14 +16,14 @@ import { } from '@/components/ui/form'; import { FormError } from '../Form-error'; import AuthContext from '@/context/AuthContext'; +import { toast } from 'sonner'; export function LoginForm() { const [error, setError] = React.useState(''); - const [success, setSuccess] = React.useState(''); const [isPending, startTransition] = React.useTransition(); const auth = React.useContext(AuthContext); - const { dispatch } = auth; + const { state, dispatch } = auth; const form = useForm>({ resolver: zodResolver(LoginSchema), @@ -34,10 +34,32 @@ export function LoginForm() { const onSubmit = (values: z.infer) => { setError(''); - setSuccess(''); startTransition(async () => { - console.log('values', values); - console.log('dispatch', dispatch); + try { + if (state.is_admin) { + toast.error('Admin cannot register a voter!!'); + return; + } + + if (state.instance !== null) { + console.log('state.instance', state.instance); + await state.instance.methods.addVoter(state.account, values.name).send({ + from: state.account, + gas: 1000000, + }); + + dispatch({ + type: 'REGISTER', + }); + + toast.message('you are registered as voter', { + description: 'You can vote once the admin verifies your request', + }); + } + } catch (error) { + console.error(`Error: ${error}`); + toast.error('You can register only once'); + } }); }; diff --git a/src/schemas/index.ts b/src/schemas/index.ts index e69de29..f79aee9 100644 --- a/src/schemas/index.ts +++ b/src/schemas/index.ts @@ -0,0 +1,5 @@ +import * as z from 'zod'; + +export const LoginSchema = z.object({ + name: z.string().nonempty('Name is required'), +});