Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds "combineShares" button #112

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
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
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ SC5:Close │ │
│ │ NS2:Shuffle
│ │
│ ▼
│ DK4:BeginDecryption
│ DK4:ComputePubshares
SC6:CombineShares
Expand Down Expand Up @@ -370,7 +370,7 @@ Return:

```json
{
"Action": "beginDecryption"
"Action": "computePubshares"
}
```

Expand Down
29 changes: 29 additions & 0 deletions web/frontend/src/components/utils/CombineButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ShieldCheckIcon } from '@heroicons/react/outline';
import { AuthContext } from 'index';
import { useContext } from 'react';
import { useTranslation } from 'react-i18next';
import { STATUS } from 'types/election';
import { ROLE } from 'types/userRole';

const CombineButton = ({ status, handleCombine }) => {
const authCtx = useContext(AuthContext);
const { t } = useTranslation();

const isAuthorized = authCtx.role === ROLE.Admin || authCtx.role === ROLE.Operator;

return (
isAuthorized &&
status === STATUS.DecryptedBallots && (
<span>
<button onClick={handleCombine}>
<div className="whitespace-nowrap inline-flex items-center justify-center px-4 py-1 border border-gray-300 text-sm rounded-full font-medium text-gray-700">
<ShieldCheckIcon className="-ml-1 mr-2 h-5 w-5 text-gray-700" aria-hidden="true" />
{t('combine')}
</div>
</button>
</span>
)
);
};

export default CombineButton;
5 changes: 3 additions & 2 deletions web/frontend/src/components/utils/Endpoints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ export const ENDPOINT_REMOVE_ROLE = '/api/remove_role';
export const newElection = '/api/evoting/elections';
export const editElection = (ElectionID: string) => `/api/evoting/elections/${ElectionID}`;
export const newElectionVote = (ElectionID: string) => `/api/evoting/elections/${ElectionID}/vote`;
export const editShuffle = (ElectionID: string) => `/evoting/services/shuffle/${ElectionID}`;
export const editShuffle = (ElectionID: string) => `/api/evoting/services/shuffle/${ElectionID}`;
// Decrypt
export const editDKGActors = (ElectionID: string) => `/evoting/services/dkg/actors/${ElectionID}`;
export const editDKGActors = (ElectionID: string) =>
`/api/evoting/services/dkg/actors/${ElectionID}`;

// public information can be directly fetched from dela nodes
export const election = (ElectionID: string) =>
Expand Down
31 changes: 26 additions & 5 deletions web/frontend/src/components/utils/useChangeAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import OpenButton from './OpenButton';
import DecryptButton from './DecryptButton';
import ResultButton from './ResultButton';
import VoteButton from './VoteButton';
import CombineButton from './CombineButton';

const useChangeAction = (
status: STATUS,
Expand Down Expand Up @@ -159,22 +160,36 @@ const useChangeAction = (

const handleDecrypt = async () => {
const decryptSuccess = await electionUpdate(
'beginDecryption',
'computePubshares',
endpoints.editDKGActors(electionID.toString())
);
if (decryptSuccess && postError === null) {
// TODO : setResultAvailable is undefined when the decryption is clicked
if (setResultAvailable !== null && setResultAvailable !== undefined) {
setResultAvailable(true);
}
setStatus(STATUS.ResultAvailable);
// if (setResultAvailable !== null && setResultAvailable !== undefined) {
// setResultAvailable(true);
// }
setStatus(STATUS.DecryptedBallots);
} else {
setShowModalError(true);
setIsDecrypting(false);
}
setPostError(null);
};

const handleCombine = async () => {
const combineSuccess = await electionUpdate(
'combineShares',
endpoints.editElection(electionID.toString())
);
if (combineSuccess && postError === null) {
setStatus(STATUS.ResultAvailable);
} else {
setShowModalError(true);
setIsOpening(false);
}
setPostError(null);
};

const getAction = () => {
switch (status) {
case STATUS.Initial:
Expand Down Expand Up @@ -212,6 +227,12 @@ const useChangeAction = (
/>
</span>
);
case STATUS.DecryptedBallots:
return (
<span>
<CombineButton status={status} handleCombine={handleCombine} />
</span>
);
case STATUS.ResultAvailable:
return (
<span>
Expand Down
2 changes: 1 addition & 1 deletion web/frontend/src/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const handlers = [
}),

rest.post(endpoints.newElectionVote(':ElectionID'), async (req, res, ctx) => {
const { Ballot }: NewElectionVoteBody = JSON.parse(req.body.toString());
const { Ballot }: NewElectionVoteBody = req.body as NewElectionVoteBody;
await new Promise((r) => setTimeout(r, 1000));

return res(
Expand Down
3 changes: 3 additions & 0 deletions web/frontend/src/pages/ballot/Show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ const Ballot: FC = () => {
const newRequest = {
method: 'POST',
body: JSON.stringify(ballot),
headers: {
'Content-Type': 'Application/json',
},
};
setPostRequest(newRequest);
} catch (e) {
Expand Down