diff --git a/anonymousVoting/clientAnonymousVoting/src/components/CreateProcessPage.js b/anonymousVoting/clientAnonymousVoting/src/components/CreateProcessPage.js index f5e4ed38..facaaf3f 100644 --- a/anonymousVoting/clientAnonymousVoting/src/components/CreateProcessPage.js +++ b/anonymousVoting/clientAnonymousVoting/src/components/CreateProcessPage.js @@ -2,15 +2,11 @@ import Modal from 'react-bootstrap/Modal'; import Collapse from 'react-bootstrap/Collapse'; import Spinner from 'react-bootstrap/Spinner'; import styles from './CreateProcessPage.module.css'; -import {ethers} from 'ethers'; - -//web3 imports -import { deployVotingProcess, deployTestContract, getTestContract } from '../web3/contracts' -import { useState, useEffect, useRef } from 'react'; +import { ethers } from 'ethers'; +import { deployVotingProcess } from '../web3/contracts'; +import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; - - const CreateProcess = () => { const navigate = useNavigate(); @@ -18,107 +14,134 @@ const CreateProcess = () => { const [description, setDescription] = useState(''); const [proposals, setProposals] = useState(''); const [pending, setPending] = useState(false); - const [show, setShow] = useState(false); + const [open, setOpen] = useState(false); + const [transactionResult, setTransactionResult] = useState(null); + const handleClose = () => { setShow(false); navigate('/'); - } - const [open, setOpen] = useState(false); - const [transactionResult, setTransactionResult] = useState(null); + }; + + const isFormValid = () => { + const validProposals = proposals + .split(',') + .map(p => p.trim()) + .filter(p => p.length > 0); + return validProposals.length >= 2; + }; + + const formatProposals = (input) => { + return input + .split(',') + .map(p => p.trim()) + .filter(p => p.length > 0) + .map(p => ethers.utils.toUtf8Bytes(p)); + }; - const refValue = useRef(false); - const createProcess = async (e) => { e.preventDefault(); - console.log("name: ", name); - //format proposals - let proposalArray = formatProposals(proposals); - //check form inputs - if(!isFormValid()){ - window.alert("Form is not valid"); + + if (!isFormValid()) { + window.alert("Form is not valid. Enter at least 2 proposals."); return; } - setPending(true); - refValue.current = true; - //deploy new process contract - const result = await deployVotingProcess(name, description, proposalArray,1000000,1000000); - setTransactionResult(result); - - refValue.current = false; - setPending(false); - - setShow(true); - } - const isFormValid = () => { - if(proposals < 2) - return false; - return true; - } + try { + setPending(true); - const formatProposals = (input) => { - let proposals = input.split(','); - let array = [] - for(let i=0; i < proposals.length; i ++){ - array.push(ethers.utils.toUtf8Bytes(proposals[i].trim())); - } - return array; - } + const _formattedProposals = formatProposals(proposals); + + const startDate = Math.floor(Date.now() / 1000); // current time + const endDate = startDate + 24 * 60 * 60; // 1 day later + // Deploy with proposals + const result = await deployVotingProcess( + name, + description, + startDate, + endDate, + _formattedProposals // <-- send proposals to contract + ); - useEffect(() => { - // setPending(!pending); - }, [pending]) + setTransactionResult(result); + setShow(true); - return ( -
+ } catch (err) { + console.error(err); + window.alert("Transaction failed: " + (err.message || err)); + } finally { + setPending(false); + } + }; + + return ( +

Create new voting process

+
- setName(e.target.value)} />
+
- setDescription(e.target.value)} />
+
- setProposals(e.target.value)} />
+
- +
- {refValue.current == true &&
- -
}
+ Transaction result + -

- Transaction was executed. -

+

Transaction was executed.

+ - { transactionResult && -
-
-

transaction hash:

-

{transactionResult.hash}

-
-
-

nonce:

-

{transactionResult.nonce}

+ + {transactionResult && ( + +
+
+

Transaction hash:

+

{transactionResult.hash}

+
+
+

Nonce:

+

{transactionResult.nonce}

+
-
- } + + )} + - +
); -} - -export default CreateProcess; \ No newline at end of file +}; + +export default CreateProcess;