diff --git a/client/src/App.jsx b/client/src/App.jsx index 3f38b69..e1bf051 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -41,10 +41,17 @@ const getRequestStatus = (request) => { } }; -const updateListItem = (list, item) => { - const newList = [...list]; - newList[Number(item.id)] = item; - return newList; +const updateList = (list, action) => { + switch (action.type) { + case 'update': { + const newList = [...list]; + newList[Number(action.item.id)] = action.item; + return newList; + } + case 'refresh': + default: + return []; + } }; function App() { @@ -58,10 +65,10 @@ function App() { const [balanceEther, setBalanceEther] = useState(); const [reputation, setReputation] = useState(); const [totalReputation, setTotalReputation] = useState(); - const [posts, dispatchPost] = useReducer(updateListItem, []); - const [validationPools, dispatchValidationPool] = useReducer(updateListItem, []); - const [availabilityStakes, dispatchAvailabilityStake] = useReducer(updateListItem, []); - const [workRequests, dispatchWorkRequest] = useReducer(updateListItem, []); + const [posts, dispatchPost] = useReducer(updateList, []); + const [validationPools, dispatchValidationPool] = useReducer(updateList, []); + const [availabilityStakes, dispatchAvailabilityStake] = useReducer(updateList, []); + const [workRequests, dispatchWorkRequest] = useReducer(updateList, []); // const watchReputationToken = useCallback(async () => { // await provider.request({ @@ -106,13 +113,14 @@ function App() { const fetchPost = async (postIndex) => { const p = await DAOContract.methods.posts(postIndex).call(); p.id = Number(p.id); - dispatchPost(p); + dispatchPost({ type: 'update', item: p }); return p; }; const fetchPosts = async () => { const count = await DAOContract.methods.postCount().call(); const promises = []; + dispatchPost({ type: 'refresh' }); for (let i = 0; i < count; i += 1) { promises.push(fetchPost(i)); } @@ -120,19 +128,18 @@ function App() { }; const fetchValidationPool = async (poolIndex) => { - console.trace('fetchValidationPool'); const pool = await DAOContract.methods.validationPools(poolIndex).call(); pool.id = Number(pool.id); pool.timeRemaining = new Date(Number(pool.endTime) * 1000) - new Date(); pool.status = getPoolStatus(pool); - dispatchValidationPool(pool); + dispatchValidationPool({ type: 'update', item: 'pool' }); // When remaing time expires, we want to update the status for this pool if (pool.timeRemaining > 0) { setTimeout(() => { pool.timeRemaining = 0; pool.status = getPoolStatus(pool); - dispatchValidationPool(pool); + dispatchValidationPool({ type: 'update', item: pool }); }, pool.timeRemaining); } }; @@ -143,6 +150,7 @@ function App() { // TODO: Caching const count = await DAOContract.methods.validationPoolCount().call(); const promises = []; + dispatchValidationPool({ type: 'refresh' }); for (let i = 0; i < count; i += 1) { // promises.push(DAOContract.methods.validationPools(i).call()); promises.push(fetchValidationPool(i)); @@ -156,13 +164,14 @@ function App() { id: Number(stakeIndex), currentUserIsWorker: () => s.worker.toLowerCase() === account.toString().toLowerCase(), }); - dispatchAvailabilityStake(s); + dispatchAvailabilityStake({ type: 'update', item: s }); return s; }; const fetchAvailabilityStakes = async () => { const count = await work1Contract.methods.stakeCount().call(); const promises = []; + dispatchAvailabilityStake({ type: 'refresh' }); for (let i = 0; i < count; i += 1) { promises.push(fetchAvailabilityStake(i)); } @@ -178,13 +187,14 @@ function App() { currentUserIsCustomer: () => r.customer.toLowerCase() === account.toString().toLowerCase(), }); - dispatchWorkRequest(r); + dispatchWorkRequest({ type: 'update', item: r }); return r; }; const fetchWorkRequests = async () => { const count = await work1Contract.methods.requestCount().call(); const promises = []; + dispatchWorkRequest({ type: 'refresh' }); for (let i = 0; i < count; i += 1) { promises.push(fetchWorkRequest(i)); }