1
- import { FC , useEffect , useState } from 'react' ;
1
+ import { FC , useState } from 'react' ;
2
2
import { useTranslation } from 'react-i18next' ;
3
3
import { useParams } from 'react-router-dom' ;
4
4
import kyber from '@dedis/kyber' ;
@@ -10,7 +10,6 @@ import { MailIcon } from '@heroicons/react/outline';
10
10
import { useNavigate } from 'react-router' ;
11
11
12
12
import useForm from 'components/utils/useForm' ;
13
- import usePostCall from 'components/utils/usePostCall' ;
14
13
import * as endpoints from 'components/utils/Endpoints' ;
15
14
import { encryptVote } from './components/VoteEncrypt' ;
16
15
import { voteEncode } from './components/VoteEncode' ;
@@ -33,29 +32,13 @@ const Ballot: FC = () => {
33
32
34
33
const [ userErrors , setUserErrors ] = useState ( '' ) ;
35
34
const edCurve = kyber . curve . newCurve ( 'edwards25519' ) ;
36
- const [ postError , setPostError ] = useState ( '' ) ;
37
35
const [ showModal , setShowModal ] = useState ( false ) ;
38
36
const [ modalText , setModalText ] = useState ( t ( 'voteSuccess' ) as string ) ;
39
37
const [ modalTitle , setModalTitle ] = useState ( '' ) ;
40
38
const [ castVoteLoading , setCastVoteLoading ] = useState ( false ) ;
41
- const sendFetchRequest = usePostCall ( setPostError ) ;
42
39
43
40
const navigate = useNavigate ( ) ;
44
41
45
- useEffect ( ( ) => {
46
- if ( postError !== null ) {
47
- if ( postError . includes ( 'ECONNREFUSED' ) ) {
48
- setModalText ( t ( 'errorServerDown' ) ) ;
49
- } else {
50
- setModalText ( t ( 'voteFailure' ) ) ;
51
- }
52
- setModalTitle ( t ( 'errorTitle' ) ) ;
53
- } else {
54
- setModalText ( t ( 'voteSuccess' ) ) ;
55
- setModalTitle ( t ( 'voteSuccessful' ) ) ;
56
- }
57
- } , [ postError , t ] ) ;
58
-
59
42
const hexToBytes = ( hex : string ) => {
60
43
const bytes : number [ ] = [ ] ;
61
44
for ( let c = 0 ; c < hex . length ; c += 2 ) {
@@ -89,7 +72,26 @@ const Ballot: FC = () => {
89
72
'Content-Type' : 'Application/json' ,
90
73
} ,
91
74
} ;
92
- await sendFetchRequest ( endpoints . newFormVote ( formID . toString ( ) ) , newRequest , setShowModal ) ;
75
+ try {
76
+ const response = await fetch ( endpoints . newFormVote ( formID . toString ( ) ) , newRequest ) ;
77
+ if ( ! response . ok ) {
78
+ const txt = await response . text ( ) ;
79
+ throw new Error ( txt ) ;
80
+ }
81
+ const res = await response . json ( ) ;
82
+ const id = res . BallotID || 'Not Implemented Yet, see issue 240' ;
83
+ setModalText ( `${ t ( 'voteSuccess' ) } ${ id } ` ) ;
84
+ setModalTitle ( t ( 'voteSuccessful' ) ) ;
85
+ } catch ( error ) {
86
+ if ( error . message . includes ( 'ECONNREFUSED' ) ) {
87
+ setModalText ( t ( 'errorServerDown' ) ) ;
88
+ } else {
89
+ setModalText ( t ( 'voteFailure' ) ) ;
90
+ }
91
+ setModalTitle ( t ( 'errorTitle' ) ) ;
92
+ }
93
+
94
+ setShowModal ( ( prev ) => ! prev ) ;
93
95
} catch ( e ) {
94
96
console . log ( e ) ;
95
97
setModalText ( t ( 'ballotFailure' ) ) ;
0 commit comments