fix results

pull/73/head
Pierre-Louis Guhur 4 years ago
parent 39347a6012
commit 9f837f676e

@ -1,33 +1,33 @@
import i18n from './i18n.jsx';
const colors = [
"#015411",
"#019812",
"#6bca24",
"#ffb200",
"#ff5d00",
"#b20616",
"#6f0214"
'#6f0214',
'#b20616',
'#ff5d00',
'#ffb200',
'#6bca24',
'#019812',
'#015411',
];
const gradeNames = [
"Excellent",
"Very good",
"Good",
"Fair",
"Passable",
"Insufficient",
"To reject",
'To reject',
'Insufficient',
'Passable',
'Fair',
'Good',
'Very good',
'Excellent',
];
export const grades = gradeNames.map((name, i) => ({
label: name,
color: colors[i]
color: colors[i],
}));
export const i18nGrades = () => {
return gradeNames.map((name, i) => ({
label: i18n.t(name),
color: colors[i]
}));
color: colors[i],
}));
};

@ -1,4 +1,4 @@
import React, { Component } from "react";
import React, { Component, Fragment } from "react";
import {withTranslation} from 'react-i18next';
import { Link } from "react-router-dom";
@ -36,13 +36,13 @@ class Footer extends Component {
<a href="https://mieuxvoter.fr/" style={linkStyle} >{t("Who are we")}</a>
<span className="m-2">-</span>
{
countries.map(({l, flag}) => (
<>
countries.map(({l, flag}, i) => (
<Fragment key={i} >
<button style={buttonStyle} onClick={() => i18n.changeLanguage(l)}>
<FlagIcon code={flag} />
</button>
{" "}
</>
</Fragment>
))
}
<div className="mt-3">

@ -16,6 +16,13 @@ import { i18nGrades } from "../../Util";
import { AppContext } from "../../AppContext";
import { errorMessage, Error } from "../../Errors";
const meritProfileFromVotes = votes => {
const numGrades = Math.max( ...votes) - Math.min( ...votes );
const profile = Array(numGrades).fill(0);
votes.forEach(vote => {profile[vote] += 1 });
return profile;
}
class Result extends Component {
static contextType = AppContext;
@ -56,7 +63,6 @@ class Result extends Component {
name: c.name,
profile: c.profile,
grade: c.grade,
score: c.score
}));
this.setState(state => ({ candidates: candidates }));
return response;
@ -124,7 +130,6 @@ class Result extends Component {
}
];
this.setState({ candidates: dataTest });
console.log(this.state.candidates);
} else {
const detailsEndpoint = resolve(
this.context.urlServer,
@ -174,14 +179,10 @@ class Result extends Component {
return <Error value={errorMessage} />;
}
let totalOfVote = 0;
//based on the first candidate
if (candidates.length > 0) {
candidates[0].profile.map((value, i) => (totalOfVote += value));
} else {
totalOfVote = 1;
}
const sum = seq => Object.values(seq).reduce((a,b) => a + b, 0)
const numVotes = candidates && candidates.length > 0 ? sum(candidates[0].profile) : 1 ;
const gradeIds = candidates && candidates.length > 0 ? Object.keys(candidates[0].profile) : [];
console.log(gradeIds);
return (
<Container>
@ -196,7 +197,6 @@ class Result extends Component {
<h1>{t("Results of the election:")}</h1>
<ol>
{candidates.map((candidate, i) => {
console.log(candidate);
return (
<li key={i} className="mt-2">
<span className="mt-2 ml-2">{candidate.name}</span>
@ -209,9 +209,9 @@ class Result extends Component {
>
{grades[candidate.grade].label}
</span>
<span className="badge badge-dark mt-2 ml-2">
{ /* <span className="badge badge-dark mt-2 ml-2">
{(100 * candidate.score).toFixed(1)}%
</span>
</span> */ }
</li>
);
})}
@ -250,12 +250,10 @@ class Result extends Component {
<table style={{ width: "100%" }}>
<tbody>
<tr>
{candidate.profile.map((value, i) => {
{gradeIds.map((id, i) => {
const value = candidate.profile[id];
if (value > 0) {
let percent =
Math.round(
(value * 100) / totalOfVote
) + "%";
let percent = (value * 100) / numVotes + "%";
if (i === 0) {
percent = "auto";
}
@ -362,13 +360,10 @@ class Result extends Component {
return (
<tr key={i}>
<td>{i + 1}</td>
{/*candidate.label*/}
{candidate.profile.map((value, i) => {
let percent =
Math.round(
((value * 100) / totalOfVote) * 100
) / 100;
return <td key={i}>{percent}%</td>;
{gradeIds.map((id, i) => {
const value = candidate.profile[id];
const percent = (value / numVotes * 100).toFixed(1);
return <td key={i}>{percent} %</td>;
})}
</tr>
);

@ -190,13 +190,13 @@ class Vote extends Component {
lg={this.state.colSizeCandidateLg}>
<h5>&nbsp;</h5>
</Col>
{electionGrades.map((grade, j) => {
return j < this.state.numGrades ? (
{electionGrades.map((grade, gradeId) => {
return gradeId < this.state.numGrades ? (
<Col
xs={this.state.colSizeGradeXs}
md={this.state.colSizeGradeMd}
lg={this.state.colSizeGradeLg}
key={j}
key={gradeId}
className="text-center p-0"
style={{lineHeight: 2}}>
<small
@ -209,9 +209,9 @@ class Vote extends Component {
})}
</Row>
{candidates.map((candidate, i) => {
{candidates.map((candidate, candidateId) => {
return (
<Row key={i} className="cardVote">
<Row key={candidateId} className="cardVote">
<Col
xs={this.state.colSizeCandidateXs}
md={this.state.colSizeCandidateMd}
@ -219,16 +219,17 @@ class Vote extends Component {
<h5 className="m-0">{candidate.label}</h5>
<hr className="d-lg-none" />
</Col>
{this.state.electionGrades.map((grade, j) => {
return j < this.state.numGrades ? (
{this.state.electionGrades.map((grade, gradeId) => {
console.assert(gradeId < this.state.numGrades)
return (
<Col
xs={this.state.colSizeGradeXs}
md={this.state.colSizeGradeMd}
lg={this.state.colSizeGradeLg}
key={j}
key={gradeId}
className="text-lg-center">
<label
htmlFor={'candidateGrade' + i + '-' + j}
htmlFor={'candidateGrade' + candidateId + '-' + gradeId}
className="check">
<small
className="nowrap d-lg-none ml-2 bold badge"
@ -238,7 +239,7 @@ class Vote extends Component {
) {
return (
JSON.stringify(ratedCandidat) ===
JSON.stringify({id: candidate.id, value: j})
JSON.stringify({id: candidate.id, value: gradeId})
);
})
? {backgroundColor: grade.color, color: '#fff'}
@ -251,17 +252,17 @@ class Vote extends Component {
</small>
<input
type="radio"
name={'candidate' + i}
id={'candidateGrade' + i + '-' + j}
data-index={i}
name={'candidate' + candidateId}
id={'candidateGrade' + candidateId + '-' + gradeId}
data-index={candidateId}
data-id={candidate.id}
value={j}
value={gradeId}
onClick={this.handleGradeClick}
defaultChecked={this.state.ratedCandidates.find(
function(element) {
return (
JSON.stringify(element) ===
JSON.stringify({id: candidate.id, value: j})
JSON.stringify({id: candidate.id, value: gradeId})
);
},
)}
@ -274,7 +275,7 @@ class Vote extends Component {
) {
return (
JSON.stringify(ratedCandidat) ===
JSON.stringify({id: candidate.id, value: j})
JSON.stringify({id: candidate.id, value: gradeId})
);
})
? {backgroundColor: grade.color, color: '#fff'}
@ -286,7 +287,8 @@ class Vote extends Component {
/>
</label>
</Col>
) : null;
)
})}
</Row>
);

Loading…
Cancel
Save