Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/components/shared/auth/Login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined>('');
const [success, setSuccess] = React.useState<string | undefined>('');
const [isPending, startTransition] = React.useTransition();
const auth = React.useContext(AuthContext);

const { dispatch } = auth;
const { state, dispatch } = auth;

const form = useForm<z.infer<typeof LoginSchema>>({
resolver: zodResolver(LoginSchema),
Expand All @@ -34,10 +34,32 @@ export function LoginForm() {

const onSubmit = (values: z.infer<typeof LoginSchema>) => {
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');
}
});
};

Expand Down
5 changes: 5 additions & 0 deletions src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as z from 'zod';

export const LoginSchema = z.object({
name: z.string().nonempty('Name is required'),
});
Loading