diff --git a/src/App.tsx b/src/App.tsx
index b74b61c..824f216 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,10 +1,64 @@
import { Route, Routes } from 'react-router';
import LoginPage from './pages/LoginPage';
+import React from 'react';
+import AuthContext from './context/AuthContext';
+import connectToBlockchain from './config';
function App() {
+ const auth = React.useContext(AuthContext);
+
+ const { dispatch } = auth;
+
+ React.useEffect(() => {
+ let isMounted = true;
+
+ const init = async () => {
+ if (isMounted) {
+ const blockchainConnection = await connectToBlockchain();
+ if (!blockchainConnection) return;
+
+ const { contractInstance, signer } = blockchainConnection;
+ const account = await signer.getAddress();
+
+ let is_admin = false;
+ let is_voter = true;
+
+ try {
+ is_admin = await contractInstance.isAdmin(account);
+ } catch (error) {
+ console.error('Error while fetching user data:', error);
+ }
+
+ if (!is_admin) {
+ try {
+ const voter = await contractInstance.getVoterdetails(account);
+ if (voter?.votername) {
+ is_voter = false;
+ }
+ } catch (error) {
+ console.error('Error while fetching user data:', error);
+ }
+ }
+
+ dispatch({
+ type: 'LOGIN',
+ payload: { account, instance: contractInstance, is_admin, flag: is_voter },
+ });
+ }
+ };
+
+ init();
+
+ return () => {
+ isMounted = false;
+ };
+ }, [dispatch]);
+
return (
- } />
+ } />
+ home} />
+ not found return 404} />
);
}
diff --git a/src/components/shared/Form-error.tsx b/src/components/shared/Form-error.tsx
index e69de29..f4358b2 100644
--- a/src/components/shared/Form-error.tsx
+++ b/src/components/shared/Form-error.tsx
@@ -0,0 +1,16 @@
+import { AlertTriangle } from 'lucide-react';
+
+type FormErrorProps = {
+ message?: string;
+};
+
+export const FormError = ({ message }: FormErrorProps) => {
+ if (!message) return null;
+
+ return (
+
+ );
+};
diff --git a/src/components/shared/auth/Login-form.tsx b/src/components/shared/auth/Login-form.tsx
index 414fc39..aa9d6cf 100644
--- a/src/components/shared/auth/Login-form.tsx
+++ b/src/components/shared/auth/Login-form.tsx
@@ -1,119 +1,89 @@
-import { Vote } from 'lucide-react';
import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
-import { Label } from '@/components/ui/label';
-import connectToBlockchain from '@/config';
-import { useContext, useState } from 'react';
+import React from 'react';
+import { useForm } from 'react-hook-form';
+import * as z from 'zod';
+import { LoginSchema } from '@/schemas';
+import { zodResolver } from '@hookform/resolvers/zod';
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from '@/components/ui/form';
+import { FormError } from '../Form-error';
import AuthContext from '@/context/AuthContext';
-import { toast } from 'sonner';
-export function LoginForm({ className, ...props }: React.ComponentPropsWithoutRef<'div'>) {
- const auth = useContext(AuthContext);
- const [name, setName] = useState('');
- const [loading, setLoading] = useState(false);
-
- if (!auth) {
- return Loading...
;
- }
+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 connectWallet = async (e: React.FormEvent) => {
- e.preventDefault(); // Prevent form submission
- setLoading(true);
-
- try {
- const blockchainConnection = await connectToBlockchain();
- console.log('blockchainConnection', blockchainConnection);
- if (!blockchainConnection) {
- setLoading(false);
- return;
- }
-
- const { contractInstance, signer } = blockchainConnection;
- console.log('contractInstacne', contractInstance);
- console.log('signer', signer);
- const account = await signer.getAddress();
- console.log('account', signer);
- console.log('account1', account);
-
- console.log('contractInstance', contractInstance);
-
- toast.success(`Connected to ${account}`);
-
- let isAdmin = false;
- let isVoter = true;
+ const form = useForm>({
+ resolver: zodResolver(LoginSchema),
+ defaultValues: {
+ name: '',
+ },
+ });
- try {
- isAdmin = await contractInstance.isAdmin(account);
- console.log('isAdmin', isAdmin);
- } catch (err) {
- console.error('Error checking admin status:', err);
- }
-
- if (!isAdmin) {
- try {
- const voter = await contractInstance.getVoterdetails(account);
- if (voter?.votername) {
- isVoter = false;
- }
- } catch (error) {
- console.error('Error fetching voter details:', error);
- }
- }
-
- dispatch({
- type: 'login',
- payload: { account, instance: contractInstance, is_admin: isAdmin, flag: isVoter },
- });
-
- toast.success('Wallet connected successfully!');
- } catch (error) {
- console.error('MetaMask connection error:', error);
- toast.error('Failed to connect wallet.');
- } finally {
- setLoading(false);
- }
+ const onSubmit = (values: z.infer) => {
+ setError('');
+ setSuccess('');
+ startTransition(async () => {
+ console.log('values', values);
+ console.log('dispatch', dispatch);
+ });
};
return (
-