From e80a2d9a1d39f0b98c8fc9609835451c2a9b164d Mon Sep 17 00:00:00 2001 From: Linus Gasser Date: Tue, 12 Mar 2024 07:54:18 +0100 Subject: [PATCH] Put throw outside of try/catch --- .../src/components/utils/usePostCall.tsx | 4 +- .../form/components/FomrStatusLoading.tsx | 11 ----- .../form/components/utils/TransactionPoll.ts | 45 ++++++++++--------- 3 files changed, 26 insertions(+), 34 deletions(-) delete mode 100644 web/frontend/src/pages/form/components/FomrStatusLoading.tsx diff --git a/web/frontend/src/components/utils/usePostCall.tsx b/web/frontend/src/components/utils/usePostCall.tsx index 9cc0ef0fb..a5a5c492c 100644 --- a/web/frontend/src/components/utils/usePostCall.tsx +++ b/web/frontend/src/components/utils/usePostCall.tsx @@ -17,7 +17,9 @@ const usePostCall = (setError) => { try { const result = await response.json(); if (result.Token) { - pollTransaction(checkTransaction, result.Token, 1000, 30).then( + // 600s is the time to shuffle 10'000 votes. This should be enough for + // everybody. + pollTransaction(checkTransaction, result.Token, 1000, 600).then( () => { setIsPosting((prev) => !prev); }, diff --git a/web/frontend/src/pages/form/components/FomrStatusLoading.tsx b/web/frontend/src/pages/form/components/FomrStatusLoading.tsx deleted file mode 100644 index 8a3ffe1db..000000000 --- a/web/frontend/src/pages/form/components/FomrStatusLoading.tsx +++ /dev/null @@ -1,11 +0,0 @@ -import IndigoSpinnerIcon from './IndigoSpinnerIcon'; - -const FormStatusLoading = () => { - return ( -
- -
- ); -}; - -export default FormStatusLoading; diff --git a/web/frontend/src/pages/form/components/utils/TransactionPoll.ts b/web/frontend/src/pages/form/components/utils/TransactionPoll.ts index 1aa4c03f8..9b04384dd 100644 --- a/web/frontend/src/pages/form/components/utils/TransactionPoll.ts +++ b/web/frontend/src/pages/form/components/utils/TransactionPoll.ts @@ -7,39 +7,40 @@ const pollTransaction = ( let attempts = 0; const request = { - method: 'GET', - headers: { 'Content-Type': 'application/json' }, + method: "GET", + headers: { "Content-Type": "application/json" } }; const executePoll = async (resolve, reject): Promise => { + let response, result; try { attempts += 1; - const response = await fetch(endpoint(data), request); - const result = await response.json(); - - if (!response.ok) { - throw new Error(JSON.stringify(result)); - } + response = await fetch(endpoint(data), request); + result = await response.json(); + } catch (e) { + return reject(e); + } - data = result.Token; + if (!response.ok) { + throw new Error(JSON.stringify(result)); + } - if (result.Status === 1) { - return resolve(result); - } + data = result.Token; - if (result.Status === 2) { - throw new Error('Transaction Rejected'); - } + if (result.Status === 1) { + return resolve(result); + } - // Add a timeout - if (attempts === maxAttempts) { - throw new Error('Timeout'); - } + if (result.Status === 2) { + throw new Error("Transaction Rejected"); + } - setTimeout(executePoll, interval, resolve, reject); - } catch (e) { - return reject(e); + // Add a timeout + if (attempts === maxAttempts) { + throw new Error("Timeout"); } + + setTimeout(executePoll, interval, resolve, reject); }; return new Promise(executePoll);