From 1f6902d9ae4a2c093bfb9eb98a43eba7face8b19 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Guhur Date: Wed, 18 Jan 2023 07:30:53 +0100 Subject: [PATCH] fix: ballot & resutls bugs --- components/MeritProfile.tsx | 120 +++++++++++++++------------- components/ballot/BallotDesktop.tsx | 14 ++-- components/ballot/BallotMobile.tsx | 2 +- components/ballot/CandidateCard.tsx | 3 +- components/ballot/GradeInput.tsx | 2 +- 5 files changed, 76 insertions(+), 65 deletions(-) diff --git a/components/MeritProfile.tsx b/components/MeritProfile.tsx index ba0a6cf..d9c33ae 100644 --- a/components/MeritProfile.tsx +++ b/components/MeritProfile.tsx @@ -1,6 +1,6 @@ -import { useEffect, useLayoutEffect, useRef, useState } from 'react'; -import { GradeResultInterface, MeritProfileInterface } from '@services/type'; -import { getMajorityGrade } from '@services/majorityJudgment'; +import {useEffect, useLayoutEffect, useRef, useState} from 'react'; +import {GradeResultInterface, MeritProfileInterface} from '@services/type'; +import {getMajorityGrade} from '@services/majorityJudgment'; interface ParamsInterface { numVotes: number; @@ -14,7 +14,7 @@ interface GradeBarInterface { params: ParamsInterface; } -const GradeBar = ({ index, grade, size, params }: GradeBarInterface) => { +const GradeBar = ({index, grade, size, params}: GradeBarInterface) => { const width = `${size * 100}%`; const textWidth = Math.floor(100 * size); @@ -59,14 +59,14 @@ const DashedMedian = () => { return (
); }; -const MajorityGrade = ({ grade, left }) => { +const MajorityGrade = ({grade, left}) => { // const spanRef = useRef(); const spanRef = useRef(); @@ -81,7 +81,7 @@ const MajorityGrade = ({ grade, left }) => { return (
; } -const MeritProfileBar = ({ profile, grades }: MeritProfileBarInterface) => { - const gradesByValue: { [key: number]: GradeResultInterface } = {}; +const MeritProfileBar = ({profile, grades}: MeritProfileBarInterface) => { + const gradesByValue: {[key: number]: GradeResultInterface} = {}; grades.forEach((g) => (gradesByValue[g.value] = g)); const numVotes = Object.values(profile).reduce((a, b) => a + b, 0); @@ -167,32 +167,37 @@ const MeritProfileBar = ({ profile, grades }: MeritProfileBarInterface) => { left={proponentWidth + normalized[majorityValue] / 2} />
-
- {values - .filter((v) => v > majorityGrade.value) - .map((v) => { - const index = values.indexOf(v); - const size = - proponentWidth < 1e-3 ? 0 : normalized[index] / proponentWidth; - return ( - - ); - })} -
+ {proponentWidth > 1e-5 ? ( +
+ {values + .filter((v) => v > majorityGrade.value) + .map((v) => { + const index = values.indexOf(v); + const size = + proponentWidth < 1e-3 ? 0 : normalized[index] / proponentWidth; + return ( + + ); + })} +
) : ( +
+
)}
{values .filter((v) => v === majorityGrade.value) @@ -209,29 +214,32 @@ const MeritProfileBar = ({ profile, grades }: MeritProfileBarInterface) => { ); })}
-
- {values - .filter((v) => v < majorityGrade.value) - .map((v) => { - const index = values.indexOf(v); - const size = - opponentWidth < 1e-3 ? 0 : normalized[index] / opponentWidth; - return ( - - ); - })} -
+ {opponentWidth > 1e-5 ? ( +
+ {values + .filter((v) => v < majorityGrade.value) + .map((v) => { + const index = values.indexOf(v); + const size = + opponentWidth < 1e-3 ? 0 : normalized[index] / opponentWidth; + return ( + + ); + })} +
) : ( +
+
+ )}
{/*
*/} diff --git a/components/ballot/BallotDesktop.tsx b/components/ballot/BallotDesktop.tsx index 33de545..64b6f32 100644 --- a/components/ballot/BallotDesktop.tsx +++ b/components/ballot/BallotDesktop.tsx @@ -1,10 +1,10 @@ -import { useState } from 'react'; -import { Container } from 'reactstrap'; -import { useBallot } from '@services/BallotContext'; +import {useState} from 'react'; +import {Container} from 'reactstrap'; +import {useBallot} from '@services/BallotContext'; import CandidateCard from '@components/ballot/CandidateCard'; import TitleBar from '@components/ballot/TitleBar'; import GradeInput from '@components/ballot/GradeInput'; -import { CandidatePayload } from '@services/api'; +import {CandidatePayload} from '@services/api'; import CandidateModal from '@components/CandidateModalGet'; const BallotDesktop = () => { @@ -19,7 +19,7 @@ const BallotDesktop = () => {

{ballot.election.name}

{ballot.election.candidates.map((candidate, candidateId) => { @@ -33,7 +33,9 @@ const BallotDesktop = () => { candidate={candidate} />
- {ballot.election.grades.map((_, gradeId) => { + {ballot.election.grades.sort( + (a, b) => b.value - a.value + ).map((_, gradeId) => { console.assert(gradeId < numGrades); return ( {
moveRight(true)}>
: null}
- {ballot.election.grades.map((_, gradeId) => { + {ballot.election.grades.sort((a, b) => b.value - a.value).map((_, gradeId) => { console.assert(gradeId < numGrades); return ( {
{candidate.name}
- {t("vote.more-details")} + { // {t("vote.more-details")} + }
) } diff --git a/components/ballot/GradeInput.tsx b/components/ballot/GradeInput.tsx index d160125..d8d81f0 100644 --- a/components/ballot/GradeInput.tsx +++ b/components/ballot/GradeInput.tsx @@ -32,7 +32,7 @@ const GradeInput = ({gradeId, candidateId}: GradeInputInterface) => { }; const active = ballot.votes.some(b => b.gradeId === gradeId && b.candidateId === candidateId) - const color = active ? getGradeColor(gradeId, numGrades) : '#C3BFD8'; + const color = active ? getGradeColor(grade.value, numGrades) : '#C3BFD8'; return (<>