Skip to content

Commit

Permalink
Add participation and turnout properties (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
elboletaire authored Jul 1, 2024
1 parent c341f34 commit 87c1644
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/react-providers/src/election/use-election-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ export interface ElectionReducerState {
password: string | undefined
signature: string | undefined
}
participation: number
turnout: number
}

const participation = (election?: PublishedElection | InvalidElection | ArchivedElection) => {
if (!election || election instanceof InvalidElection) {
return 0
}

return Math.round((election.voteCount / (election.census.size || election.maxCensusSize)) * 100) / 100
}

const turnout = (election?: PublishedElection | InvalidElection | ArchivedElection) => {
if (!election || election instanceof InvalidElection || !election.results) {
return 0
}
const total = election.results.reduce((acc, q) => acc + Number(q), 0)

return Math.round((total / (election.census.size || election.maxCensusSize)) * 100) / 100
}

export const electionStateEmpty = ({
Expand Down Expand Up @@ -164,6 +183,8 @@ export const electionStateEmpty = ({
password: undefined,
signature: undefined,
},
participation: participation(election),
turnout: turnout(election),
})

const isAbleToVote = (state: ElectionReducerState, payload?: boolean) =>
Expand Down Expand Up @@ -281,6 +302,8 @@ const electionReducer: Reducer<ElectionReducerState, ElectionAction> = (
election: null,
},
election,
participation: participation(election),
turnout: turnout(election),
}
}

Expand Down

0 comments on commit 87c1644

Please sign in to comment.