Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/frontend/src/language/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"alreadyVoted2": "on this form.",
"changeVote": "You can change your vote by simply casting a new vote.",
"pickCandidate": "Pick a candidate:",
"voteSuccess": "Your vote was successfully submitted!",
"voteSuccess": "Your vote was successfully submitted! VoteID: ",
"voteSuccessful": "Vote successful",
"errorTitle": "Error",
"actionChange": "Action Change",
Expand Down
3 changes: 3 additions & 0 deletions web/frontend/src/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,12 @@ export const handlers = [
Voters,
});

const BallotID = uid();

return res(
ctx.status(200),
ctx.json({
BallotID: BallotID,
Ballot: Ballot,
})
);
Expand Down
39 changes: 21 additions & 18 deletions web/frontend/src/pages/ballot/Show.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useEffect, useState } from 'react';
import { FC, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router-dom';
import kyber from '@dedis/kyber';
Expand All @@ -10,7 +10,6 @@ import { MailIcon } from '@heroicons/react/outline';
import { useNavigate } from 'react-router';

import useForm from 'components/utils/useForm';
import usePostCall from 'components/utils/usePostCall';
import * as endpoints from 'components/utils/Endpoints';
import { encryptVote } from './components/VoteEncrypt';
import { voteEncode } from './components/VoteEncode';
Expand Down Expand Up @@ -38,24 +37,9 @@ const Ballot: FC = () => {
const [modalText, setModalText] = useState(t('voteSuccess') as string);
const [modalTitle, setModalTitle] = useState('');
const [castVoteLoading, setCastVoteLoading] = useState(false);
const sendFetchRequest = usePostCall(setPostError);

const navigate = useNavigate();

useEffect(() => {
if (postError !== null) {
if (postError.includes('ECONNREFUSED')) {
setModalText(t('errorServerDown'));
} else {
setModalText(t('voteFailure'));
}
setModalTitle(t('errorTitle'));
} else {
setModalText(t('voteSuccess'));
setModalTitle(t('voteSuccessful'));
}
}, [postError, t]);

const hexToBytes = (hex: string) => {
const bytes: number[] = [];
for (let c = 0; c < hex.length; c += 2) {
Expand Down Expand Up @@ -89,7 +73,26 @@ const Ballot: FC = () => {
'Content-Type': 'Application/json',
},
};
await sendFetchRequest(endpoints.newFormVote(formID.toString()), newRequest, setShowModal);
try {
const response = await fetch(endpoints.newFormVote(formID.toString()), newRequest);
if (!response.ok) {
const txt = await response.text();
throw new Error(txt);
}
const res = await response.json();
setModalText(`${t('voteSuccess')}${res.BallotID}`);
setModalTitle(t('voteSuccessful'));
} catch (error) {
setPostError(error.message);
if (postError.includes('ECONNREFUSED')) {
setModalText(t('errorServerDown'));
} else {
setModalText(t('voteFailure'));
}
setModalTitle(t('errorTitle'));
}

setShowModal((prev) => !prev);
} catch (e) {
console.log(e);
setModalText(t('ballotFailure'));
Expand Down