Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
fix: overflow turnout, results
Browse files Browse the repository at this point in the history
  • Loading branch information
tassiluca committed Jan 31, 2024
1 parent 3552142 commit c07316e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
14 changes: 8 additions & 6 deletions frontend/src/commons/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,21 @@ export function formatTime(date: Date) {
}

/**
* Returns the entry of the given record with the highest value.
* Returns the entries of the given record with the highest value.
* @param data the record to analyze.
*/
export function highestOf(data: Record<string, number>): { key: string, value: number } {
let maxKey: string = '';
let maxValue: number = 0;
export function highestOf(data: Record<string, number>): { key: string, value: number }[] {
let maxEntries: { key: string, value: number }[] = [];
let maxValue = 0;
for (const [key, value] of Object.entries(data)) {
if (value > maxValue) {
maxKey = key;
maxValue = value;
maxEntries = [{ key, value }];
} else if (value === maxValue) {
maxEntries.push({ key, value });
}
}
return { key: maxKey, value: maxValue as number };
return maxEntries;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/stores/voting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const useVotingStore = defineStore('voting', () => {
start: new Date(electionInfos.startDate),
end: new Date(electionInfos.endDate),
choices: electionInfos.choices.map((i: any) => ({ name: i.choice })),
turnout: electionDetails.affluence,
turnout: `${parseFloat(electionDetails.affluence.replace('%', '')).toFixed(2)}%`,
results: electionDetails.results
}
}
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/views/ElectionDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ async function getVotingDetails(id: string) {
<div class="col">
<Tile title="Results">
<template #default>
<p v-if="highestOf(election.results).value === 0 && Date.now() < election.start.getTime()">
<p v-if="Object.keys(election.results).length === 0 && Date.now() < election.start.getTime()">
Voting has not been opened, yet.
</p>
<p v-else-if="Object.keys(election.results).length === 0">
Voting is still open, results will be made available after closing date.
</p>
<p v-else-if="Object.keys(election.results).length !== 0 && highestOf(election.results).value == 0">
<p v-else-if="Object.keys(election.results).length !== 0 && highestOf(election.results)[0].value === 0">
No votes have been casted.
</p>
<p v-else>
The option '<strong>{{ highestOf(election.results).key }}</strong>' has collected the highest number of votes.
<strong>{{ Object.keys(highestOf(election.results)).reduce((p, c) => p + ", " + c) }}</strong> collected the highest number of votes.
</p>
</template>
<template #details v-if="Object.keys(election.results).length !== 0 && highestOf(election.results).value !== 0">
<template #details v-if="Object.keys(election.results).length !== 0 && highestOf(election.results)[0].value !== 0">
<p class="text-black">Results are here presented: </p>
<table class="table table-striped">
<thead>
Expand Down

0 comments on commit c07316e

Please sign in to comment.