fix major bug: votes were not corrected transmitted when more than 10 candidates

pull/37/head
Pierre-Louis Guhur 4 years ago
parent ae0e235637
commit 86cdcef5ba

@ -0,0 +1,35 @@
import React from 'react';
export const UNKNOWN_ELECTION_ERROR = 'E1';
export const ONGOING_ELECTION_ERROR = 'E2';
export const NO_VOTE_ERROR = 'E3';
export const ELECTION_NOT_STARTED_ERROR = 'E4';
export const ELECTION_FINISHED_ERROR = 'E5';
export const INVITATION_ONLY_ERROR = 'E6';
export const UNKNOWN_TOKEN_ERROR = 'E7';
export const USED_TOKEN_ERROR = 'E8';
export const WRONG_ELECTION_ERROR = 'E9';
export const redirectError = (errorMsg, history) => {};
export const errorMessage = (error, t) => {
if (error.startsWith(UNKNOWN_ELECTION_ERROR)) {
return t('Oops... The election is unknown.');
} else if (error.startsWith(ONGOING_ELECTION_ERROR)) {
return t(
"The election is still going on. You can't access now to the results.",
);
} else if (error.startsWith(NO_VOTE_ERROR)) {
return t('No votes have been recorded yet. Come back later.');
} else if (error.startsWith(ELECTION_NOT_STARTED_ERROR)) {
return t('The election has not started yet.');
} else if (error.startsWith(ELECTION_FINISHED_ERROR)) {
return t('The election is over. You can\'t vote anymore');
} else if (error.startsWith(INVITATION_ONLY_ERROR)) {
return t('You need a token to vote in this election');
} else if (error.startsWith(USED_TOKEN_ERROR)) {
return t('You seem to have already voted.');
} else if (error.startsWith(WRONG_ELECTION_ERROR)) {
return t('The parameters of the election are incorrect.');
}
};

@ -20,6 +20,8 @@ function Routes() {
<Route path="/vote/:slug" component={Vote} />
<Route path="/result/:slug" component={Result} />
<Route path="/create-success/:slug" component={CreateSuccess} />
<Route path="/link/:slug" component={CreateSuccess} closed="true" />
<Route path="/links/:slug" component={CreateSuccess} closed="false" />
<Route path="/vote-success/:slug" component={VoteSuccess} />
<Route path="/unknown-election/:slug" component={UnknownElection} />
<Route component={UnknownView} />

@ -8,6 +8,11 @@ import { faCheck } from "@fortawesome/free-solid-svg-icons";
import { resolve } from "url";
import { i18nGrades } from "../../Util";
import { AppContext } from "../../AppContext";
import { errorMessage } from "../../Errors";
const shuffle = array => array.sort(() => Math.random() - 0.5);
class Vote extends Component {
static contextType = AppContext;
@ -33,9 +38,11 @@ class Vote extends Component {
if (!response.ok) {
response.json().then(response => {
console.log(response);
this.setState(state => ({
redirectTo: "/unknown-election/" + encodeURIComponent(response)
}));
const {t} = this.props;
toast.error(errorMessage(response, t));
// this.setState(state => ({
// redirectTo: "/unknown-election/" + encodeURIComponent(response)
// }));
});
throw Error(response);
}
@ -48,14 +55,8 @@ class Vote extends Component {
id: i,
label: c
}));
//shuffle candidates
let i, j, temp;
for (i = candidates.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
temp = candidates[i];
candidates[i] = candidates[j];
candidates[j] = temp;
}
shuffle(candidates);
const colSizeGradeLg = Math.floor(
(12 - this.state.colSizeCandidateLg) / numGrades
);
@ -144,7 +145,6 @@ class Vote extends Component {
});
const gradesByCandidate = [];
Object.keys(gradesById)
.sort()
.forEach(id => {
gradesByCandidate.push(gradesById[id]);
});

@ -0,0 +1,35 @@
import React from 'react';
export const UNKNOWN_ELECTION_ERROR = 'E1';
export const ONGOING_ELECTION_ERROR = 'E2';
export const NO_VOTE_ERROR = 'E3';
export const ELECTION_NOT_STARTED_ERROR = 'E4';
export const ELECTION_FINISHED_ERROR = 'E5';
export const INVITATION_ONLY_ERROR = 'E6';
export const UNKNOWN_TOKEN_ERROR = 'E7';
export const USED_TOKEN_ERROR = 'E8';
export const WRONG_ELECTION_ERROR = 'E9';
export const redirectError = (errorMsg, history) => {};
export const errorMessage = (error, t) => {
if (error.startsWith(UNKNOWN_ELECTION_ERROR)) {
return t('Oops... The election is unknown.');
} else if (error.startsWith(ONGOING_ELECTION_ERROR)) {
return t(
"The election is still going on. You can't access now to the results.",
);
} else if (error.startsWith(NO_VOTE_ERROR)) {
return t('No votes have been recorded yet. Come back later.');
} else if (error.startsWith(ELECTION_NOT_STARTED_ERROR)) {
return t('The election has not started yet.');
} else if (error.startsWith(ELECTION_FINISHED_ERROR)) {
return t('The election is over. You can\'t vote anymore');
} else if (error.startsWith(INVITATION_ONLY_ERROR)) {
return t('You need a token to vote in this election');
} else if (error.startsWith(USED_TOKEN_ERROR)) {
return t('You seem to have already voted.');
} else if (error.startsWith(WRONG_ELECTION_ERROR)) {
return t('The parameters of the election are incorrect.');
}
};
Loading…
Cancel
Save