Skip to content

Commit

Permalink
form update and election status
Browse files Browse the repository at this point in the history
  • Loading branch information
Aayushshah196 committed Jul 7, 2022
1 parent 266535b commit 5126207
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 27 deletions.
3 changes: 2 additions & 1 deletion client/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Routes, Route } from 'react-router-dom';

import Admin from './screens/Admin';
import CandidateForm from './screens/CandidateForm';
import Home from './screens/Home';
import Navbar from './components/Navbar';
import Login from './screens/Login';
Expand All @@ -19,6 +19,7 @@ function App() {
<Routes>
<Route path="/login" element={<Login />} />
<Route path="/home" element={<Home />} />
<Route path="/add" element={<CandidateForm />} />
</Routes>
</div>

Expand Down
49 changes: 31 additions & 18 deletions client/src/screens/Admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogTitle from '@mui/material/DialogTitle';

import Candidate from '../components/CandidateCard';

Expand All @@ -20,6 +19,7 @@ export default function Admin({role, contract, web3, currentAccount}) {
// const [web3, setWeb3] = useState(null);
// const [currentAccount, setCurrentAccount] = useState(null);
// const [contract, setContract] = useState(null);
const [electionState, setElectionState] = useState("NotStarted");
const [loading, setLoading] = useState(true);
const [candidates, setCandidates] = useState([]);

Expand All @@ -44,25 +44,34 @@ export default function Admin({role, contract, web3, currentAccount}) {
// }
// };

const getCandidates = async () => {
if (contract) {
const count = await contract.methods.candidatesCount().call();
const temp = [];
for (let i = 0; i < count; i++) {
const candidate = await contract.methods.getCandidateDetails(i).call();
temp.push({ name: candidate[0], votes: candidate[1] });
}
setCandidates(temp);
setLoading(false);
console.log(temp)
}
};
const getCandidates = async () => {
if (contract) {
console.log(contract)
const count = await contract.methods.candidatesCount().call();
const temp = [];
for (let i = 0; i < count; i++) {
const candidate = await contract.methods.getCandidateDetails(i).call();
temp.push({ name: candidate[0], votes: candidate[1] });
}
setCandidates(temp);
setLoading(false);
console.log(temp)
}
};

const getElectionState = async () => {
if (contract) {
const state = await contract.methods.electionState().call();
setElectionState(state);
}
}

// useEffect(() => {
// loadWeb3();
// }, []);

useEffect(() => {
getElectionState();
getCandidates();
}, [contract]);

Expand All @@ -75,8 +84,10 @@ export default function Admin({role, contract, web3, currentAccount}) {
};

const handleAgree = async () => {
console.log("Ending Election");
await contract.methods.endElection().call();
if (electionState==="NotStarted")
await contract.methods.startElection().call();
else if (electionState==="InProgress")
await contract.methods.endElection().call();
}

return (
Expand All @@ -93,7 +104,8 @@ export default function Admin({role, contract, web3, currentAccount}) {
sx={{width: "100%"}}
onClick={handleEnd}
>
End Election
{ electionState && electionState==="NotStarted" && "Start Election"}
{ electionState && electionState==="InProgress" && "End Election"}
</Button>
</div>
</Grid>
Expand Down Expand Up @@ -124,7 +136,8 @@ export default function Admin({role, contract, web3, currentAccount}) {
>
<DialogContent>
<DialogContentText id="alert-dialog-description">
Do you want to end the election?
{ electionState && electionState==="NotStarted" && "Do you want to start the election?"}
{ electionState && electionState==="InProgress" && "Do you want to end the election?"}
</DialogContentText>
</DialogContent>
<DialogActions>
Expand Down
64 changes: 64 additions & 0 deletions client/src/screens/CandidateForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { useState } from 'react';
import { styled } from '@mui/material/styles';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import Grid from '@mui/material/Grid';

const Input = styled('input')({
display: 'none',
});

export default function CandidateForm(){//({role, contract, web3, currentAccount}) {
const [name, setName] = useState('');
const [file, setFile] = useState(null);

const handleForm = async () => {
console.log("handleForm");
await contract.methods.addCandidate(name).call();
}

const handleNameChange = (event) => {
setName(event.target.value);
};

const handleFileChange = (event) => {
setFile(event.target.files[0]);
};

return(
<Box>
<Grid container spacing={3}>

<Grid item xs={12}>
Candidate Form
</Grid>

<Grid item xs={12}>
<label htmlFor="contained-button-file">
<Input accept="image/*" id="contained-button-file" onChange={handleFileChange}/>
<Button variant="contained" component="span">
Upload Candidate List
</Button>
</label>
</Grid>

<Grid item xs={12}>
<form onSubmit={handleForm}>
<TextField
id="outlined-basic"
label="Candidate Name"
variant="outlined"
value={name}
onChange={handleNameChange}
/>
<Button variant="contained" type="submit"> Submit </Button>
</form>
</Grid>

</Grid>

</Box>

)
}
6 changes: 3 additions & 3 deletions client/src/screens/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function Home() {
const getRole = async () => {
if (contract) {
// const role = await contract.methods.getRole().call();
const role = 2;
const role = 1;
setRole(role);
setLoading(false);
}
Expand All @@ -55,13 +55,13 @@ export default function Home() {
<h1>Loading the page !!!</h1>
) : (
<Box>
{ role===1 && <Vote role={role}
{ role===1 && <Admin role={role}
contract={contract}
web3={web3}
currentAccount={currentAccount}/>
}

{ role===2 && <Admin role={role}
{ role===2 && <Vote role={role}
contract={contract}
web3={web3}
currentAccount={currentAccount}/>
Expand Down
5 changes: 0 additions & 5 deletions client/src/screens/Vote.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import Grid from '@mui/material/Grid';
import Button from '@mui/material/Button';
import Typography from '@mui/material/Typography';
import Divider from '@mui/material/Divider';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogTitle from '@mui/material/DialogTitle';

import Radio from '@mui/material/Radio';
import RadioGroup from '@mui/material/RadioGroup';
Expand Down

0 comments on commit 5126207

Please sign in to comment.