From f215570a1e4e8b1f284e2ed49562fd291fc11526 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Sat, 16 Mar 2024 21:42:22 -0500 Subject: [PATCH] separated work requests component --- client/src/App.jsx | 119 ++++++----------------------------- client/src/WorkRequests.jsx | 121 ++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 99 deletions(-) create mode 100644 client/src/WorkRequests.jsx diff --git a/client/src/App.jsx b/client/src/App.jsx index 45f8b99..adc9425 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -16,6 +16,7 @@ import contracts from './contracts'; import DAOArtifact from './assets/DAO.json'; import work1Artifact from './assets/Work1.json'; import AvailabilityStakes from './AvailabilityStakes'; +import WorkRequests from './WorkRequests'; const updateList = (list, action) => { switch (action.type) { @@ -48,8 +49,23 @@ function App() { const [availabilityStakes, dispatchAvailabilityStake] = useList(); const [workRequests, dispatchWorkRequest] = useList(); + const web3ProviderValue = useMemo(() => ({ + provider, + DAO, + work1, + work1Price, + reputation, + setReputation, + account, + chainId, + availabilityStakes, + workRequests, + }), [ + provider, DAO, work1, work1Price, reputation, setReputation, account, chainId, + availabilityStakes, workRequests, + ]); + // In this effect, we initialize everything and add contract event listeners. - // TODO: Refactor -- make separate, functional components? useEffect(() => { if (!provider || !chainId || !account || balance === undefined) return; if (!contracts[chainId]) return; @@ -328,47 +344,10 @@ function App() { }); }, [DAO, account]); - const requestWork = useCallback(async () => { - const web3 = new Web3(provider); - const priceWei = BigInt(web3.utils.toWei(work1Price, 'ether')); - await work1.methods.requestWork().send({ - from: account, - gas: 1000000, - value: priceWei, - }); - }, [provider, work1, account, work1Price]); - - const submitWorkEvidence = useCallback(async (requestIndex) => { - await work1.methods.submitWorkEvidence(requestIndex).send({ - from: account, - gas: 1000000, - }); - }, [work1, account]); - - const submitWorkApproval = useCallback(async (requestIndex) => { - await work1.methods.submitWorkApproval(requestIndex, true).send({ - from: account, - gas: 1000000, - }); - }, [work1, account]); - - const submitWorkDisapproval = useCallback(async (requestIndex) => { - await work1.methods.submitWorkApproval(requestIndex, false).send({ - from: account, - gas: 1000000, - }); - }, [work1, account]); - /* -------------------------------------------------------------------------------- */ /* --------------------------- END UI ACTIONS ------------------------------------- */ /* -------------------------------------------------------------------------------- */ - const web3ProviderValue = useMemo(() => ({ - DAO, work1, availabilityStakes, reputation, setReputation, account, chainId, - }), [ - DAO, work1, availabilityStakes, reputation, setReputation, account, chainId, - ]); - return ( {!connected && } @@ -517,67 +496,7 @@ function App() { {`Price: ${work1Price} ETH`} -
- -
-
- Work Request Count: - {' '} - {workRequests.length} -
-
- - - - - - - - - - - - - - - {workRequests.filter((x) => !!x).map((request) => ( - - - - - - - - - - - ))} - -
IDCustomerFeeStatusStake IDApprovalPool IDActions
{request.id.toString()}{request.customer.toString()} - {request.feeEther} - {' '} - ETH - {request.statusString}{request.stakeIndex.toString()}{request.approval.toString()}{request.poolIndex.toString()} - {availabilityStakes.length > 0 - && availabilityStakes[Number(request.stakeIndex)]?.currentUserIsWorker() - && Number(request.status) === 0 && ( - - )} - {request.currentUserIsCustomer() - && Number(request.status) === 1 && ( - <> - - - - )} -
-
+ @@ -586,6 +505,7 @@ function App() { {`Price: ${work1Price} ETH`} +

Work Contract 1

@@ -593,6 +513,7 @@ function App() { {`Price: ${work1Price} ETH`} +
diff --git a/client/src/WorkRequests.jsx b/client/src/WorkRequests.jsx new file mode 100644 index 0000000..fc92e24 --- /dev/null +++ b/client/src/WorkRequests.jsx @@ -0,0 +1,121 @@ +import { useCallback, useContext } from 'react'; +import { PropTypes } from 'prop-types'; +import Button from 'react-bootstrap/Button'; + +import Web3 from 'web3'; +import Web3Context from './Web3Context'; + +function WorkRequests({ showRequestWork }) { + const { + provider, work1, work1Price, workRequests, availabilityStakes, account, + } = useContext(Web3Context); + + const requestWork = useCallback(async () => { + const web3 = new Web3(provider); + const priceWei = BigInt(web3.utils.toWei(work1Price, 'ether')); + await work1.methods.requestWork().send({ + from: account, + gas: 1000000, + value: priceWei, + }); + }, [provider, work1, account, work1Price]); + + const submitWorkEvidence = useCallback(async (requestIndex) => { + await work1.methods.submitWorkEvidence(requestIndex).send({ + from: account, + gas: 1000000, + }); + }, [work1, account]); + + const submitWorkApproval = useCallback(async (requestIndex) => { + await work1.methods.submitWorkApproval(requestIndex, true).send({ + from: account, + gas: 1000000, + }); + }, [work1, account]); + + const submitWorkDisapproval = useCallback(async (requestIndex) => { + await work1.methods.submitWorkApproval(requestIndex, false).send({ + from: account, + gas: 1000000, + }); + }, [work1, account]); + + return ( + <> + {showRequestWork && ( +
+ +
+ )} +
+ Work Request Count: + {' '} + {workRequests.length} +
+
+ + + + + + + + + + + + + + + {workRequests.filter((x) => !!x).map((request) => ( + + + + + + + + + + + ))} + +
IDCustomerFeeStatusStake IDApprovalPool IDActions
{request.id.toString()}{request.customer.toString()} + {request.feeEther} + {' '} + ETH + {request.statusString}{request.stakeIndex.toString()}{request.approval.toString()}{request.poolIndex.toString()} + {availabilityStakes.length > 0 + && availabilityStakes[Number(request.stakeIndex)]?.currentUserIsWorker() + && Number(request.status) === 0 && ( + + )} + {request.currentUserIsCustomer() + && Number(request.status) === 1 && ( + <> + + + + )} +
+
+ + ); +} + +WorkRequests.propTypes = { + showRequestWork: PropTypes.bool, +}; + +WorkRequests.defaultProps = { + showRequestWork: false, +}; + +export default WorkRequests;