-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #681 from csamuele/663-result-page-layout-download
663 result page layout download
- Loading branch information
Showing
9 changed files
with
156 additions
and
41 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
packages/backend/src/Controllers/getAnonymizedBallotsByElectionIDController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import ServiceLocator from "../ServiceLocator"; | ||
import Logger from "../Services/Logging/Logger"; | ||
import { BadRequest } from "@curveball/http-errors"; | ||
import { expectPermission } from "./controllerUtils"; | ||
import { permissions } from '@equal-vote/star-vote-shared/domain_model/permissions'; | ||
import { IElectionRequest } from "../IRequest"; | ||
import { Response, NextFunction } from 'express'; | ||
import { AnonymizedBallot } from "@equal-vote/star-vote-shared/domain_model/Ballot"; | ||
|
||
|
||
const BallotModel = ServiceLocator.ballotsDb(); | ||
|
||
const getAnonymizedBallotsByElectionID = async (req: IElectionRequest, res: Response, next: NextFunction) => { | ||
var electionId = req.election.election_id; | ||
Logger.debug(req, "getBallotsByElectionID: " + electionId); | ||
const election = req.election; | ||
if (!election.settings.public_results) { | ||
expectPermission(req.user_auth.roles, permissions.canViewBallots) | ||
} | ||
|
||
const ballots = await BallotModel.getBallotsByElectionID(String(electionId), req); | ||
if (!ballots) { | ||
const msg = `Ballots not found for Election ${electionId}`; | ||
Logger.info(req, msg); | ||
throw new BadRequest(msg) | ||
} | ||
const anonymizedBallots: AnonymizedBallot[] = ballots.map((ballot) => { | ||
return { | ||
ballot_id: ballot.ballot_id, | ||
votes: ballot.votes | ||
} | ||
}); | ||
Logger.debug(req, "ballots = ", ballots); | ||
res.json({ ballots: anonymizedBallots }) | ||
} | ||
|
||
module.exports = { | ||
getAnonymizedBallotsByElectionID | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/frontend/src/components/Election/Results/DownloadCSV.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { StyledButton } from '~/components/styles'; | ||
import { CSVLink } from 'react-csv'; | ||
import { useState, useEffect } from 'react'; | ||
import { Election } from '@equal-vote/star-vote-shared/domain_model/Election'; | ||
import { AnonymizedBallot, Ballot } from '@equal-vote/star-vote-shared/domain_model/Ballot'; | ||
import { useGetAnonymizedBallots } from '~/hooks/useAPI'; | ||
|
||
interface Props { | ||
election: Election; | ||
} | ||
|
||
export const DownloadCSV = ({ election }: Props) => { | ||
const [csvData, setCsvData] = useState([]); | ||
const [csvHeaders, setCsvHeaders] = useState([]); | ||
const { data, error, isPending, makeRequest } = useGetAnonymizedBallots(election.election_id); | ||
useEffect(() => { makeRequest() }, []) | ||
const ballots: AnonymizedBallot[] = data?.ballots || []; | ||
const buildCsvData = () => { | ||
|
||
let header = [ | ||
{ label: 'ballot_id', key: 'ballot_id' }, | ||
...election.races.map((race) => | ||
race.candidates.map((c) => ({ | ||
label: `${race.title}!!${c.candidate_name}`, | ||
key: `${race.race_id}-${c.candidate_id}`, | ||
})) | ||
), | ||
]; | ||
header = header.flat(); | ||
let tempCsvData = ballots.map((ballot) => { | ||
let row = { ballot_id: ballot.ballot_id }; | ||
ballot.votes.forEach((vote) => | ||
vote.scores.forEach((score) => { | ||
row[`${vote.race_id}-${score.candidate_id}`] = score.score; | ||
}) | ||
); | ||
return row; | ||
}); | ||
setCsvHeaders(header); | ||
setCsvData(tempCsvData); | ||
}; | ||
|
||
const limit = (string = '', limit = 0) => { | ||
if (!string) return ''; | ||
return string.substring(0, limit); | ||
}; | ||
|
||
|
||
return ( | ||
<> | ||
{!isPending && ( | ||
<StyledButton type="button" variant="contained" fullwidth onClick={buildCsvData}> | ||
<CSVLink | ||
id="csv-download-link" | ||
data={csvData} | ||
headers={csvHeaders} | ||
target="_blank" | ||
filename={`Ballot Data - ${limit(election.title, 50)}-${election.election_id}.csv`} | ||
enclosingCharacter={``} | ||
style={{ textDecoration: 'none', color: 'inherit' }} | ||
> | ||
Download CSV | ||
</CSVLink> | ||
</StyledButton> | ||
)} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters