import React, { Component } from "react"; import { Redirect } from "react-router-dom"; import { withTranslation } from "react-i18next"; import { resolve } from "url"; import { Container, Row, Col, Collapse, Card, CardHeader, CardBody, Table } from "reactstrap"; import { i18nGrades } from "../../Util"; import { AppContext } from "../../AppContext"; class Result extends Component { static contextType = AppContext; constructor(props) { super(props); this.state = { candidates: [], title: null, numGrades: 0, colSizeCandidateLg: 4, colSizeCandidateMd: 6, colSizeCandidateXs: 12, colSizeGradeLg: 1, colSizeGradeMd: 1, colSizeGradeXs: 1, collapseGraphics: false, collapseProfiles: false, redirectLost: false, electionGrades: i18nGrades() }; } handleErrors = response => { if (!response.ok) { response.json().then(response => { this.setState(state => ({ redirectLost: "/unknown-election/" + encodeURIComponent(response) })); }); throw Error(response); } return response; }; resultsToState = response => { const candidates = response.map(c => ({ id: c.id, name: c.name, profile: c.profile, grade: c.grade, score: c.score })); this.setState(state => ({ candidates: candidates })); return response; }; detailsToState = response => { const numGrades = response.num_grades; const colSizeGradeLg = Math.floor( (12 - this.state.colSizeCandidateLg) / numGrades ); const colSizeGradeMd = Math.floor( (12 - this.state.colSizeCandidateMd) / numGrades ); const colSizeGradeXs = Math.floor( (12 - this.state.colSizeCandidateXs) / numGrades ); this.setState(state => ({ title: response.title, numGrades: numGrades, colSizeGradeLg: colSizeGradeLg, colSizeGradeMd: colSizeGradeMd, colSizeGradeXs: colSizeGradeXs, colSizeCandidateLg: 12 - colSizeGradeLg * numGrades > 0 ? 12 - colSizeGradeLg * numGrades : 12, colSizeCandidateMd: 12 - colSizeGradeMd * numGrades > 0 ? 12 - colSizeGradeMd * numGrades : 12, colSizeCandidateXs: 12 - colSizeGradeXs * numGrades > 0 ? 12 - colSizeGradeXs * numGrades : 12, electionGrades: i18nGrades().slice(0, numGrades) })); return response; }; componentDidMount() { // get details of the election const electionSlug = this.props.match.params.slug; if (electionSlug === "dev") { const dataTest = [ { name: "BB", id: 1, score: 1.0, profile: [1, 1, 0, 0, 0, 0, 0], grade: 1 }, { name: "CC", id: 2, score: 1.0, profile: [0, 0, 2, 0, 0, 0, 0], grade: 2 }, { name: "AA", id: 0, score: 1.0, profile: [1, 1, 0, 0, 0, 0, 0], grade: 1 } ]; this.setState({ candidates: dataTest }); console.log(this.state.candidates); } else { const detailsEndpoint = resolve( this.context.urlServer, this.context.routesServer.getElection.replace( new RegExp(":slug", "g"), electionSlug ) ); fetch(detailsEndpoint) .then(this.handleErrors) .then(response => response.json()) .then(this.detailsToState) .catch(error => console.log(error)); // get results of the election const resultsEndpoint = resolve( this.context.urlServer, this.context.routesServer.getResultsElection.replace( new RegExp(":slug", "g"), electionSlug ) ); fetch(resultsEndpoint) .then(this.handleErrors) .then(response => response.json()) .then(this.resultsToState) .catch(error => console.log(error)); } } toggleGraphics = () => { this.setState(state => ({ collapseGraphics: !state.collapseGraphics })); }; toggleProfiles = () => { this.setState(state => ({ collapseProfiles: !state.collapseProfiles })); }; render() { const { redirectLost, candidates, electionGrades } = this.state; const { t } = this.props; const grades = i18nGrades(); if (redirectLost) { return ; } let totalOfVote = 0; //based on the first candidate if (candidates.length > 0) { candidates[0].profile.map((value, i) => (totalOfVote += value)); } else { totalOfVote = 1; } return (

{this.state.title}

{t("Results of the election:")}

    {candidates.map((candidate, i) => { console.log(candidate); return (
  1. {candidate.name} {grades[candidate.grade].label} {(100 * candidate.score).toFixed(1)}%
  2. ); })}

{t("Graph")}

{candidates.map((candidate, i) => { return ( {/*candidate.label*/} ); })}
{i + 1} {candidate.profile.map((value, i) => { if (value > 0) { let percent = Math.round( (value * 100) / totalOfVote ) + "%"; if (i === 0) { percent = "auto"; } return ( ); } else { return null; } })}
 
{candidates.map((candidate, i) => { return ( {i > 0 ? ", " : ""} {i + 1}: {candidate.name} ); })}
{electionGrades.map((grade, i) => { return ( {grade.label} ); })}

{t("Preference profile")}

{electionGrades.map((grade, i) => { return ( ); })} {candidates.map((candidate, i) => { return ( {/*candidate.label*/} {candidate.profile.map((value, i) => { let percent = Math.round( ((value * 100) / totalOfVote) * 100 ) / 100; return ; })} ); })}
# {grade.label}{" "}
{i + 1}{percent}%
{candidates.map((candidate, i) => { return ( {i > 0 ? ", " : ""} {i + 1}: {candidate.name} ); })}
); } } export default withTranslation()(Result);