From 39347a6012de8719567353ad4c7acfa44d755313 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Guhur Date: Sun, 19 Apr 2020 09:35:20 +0000 Subject: [PATCH] [CREATE ELECTION] enfore error checkings --- public/locale/i18n/de/common.json | 1 + public/locale/i18n/de/resource.json | 23 +++++++++++- src/components/views/CreateElection.jsx | 47 +++++++++++++++++++++---- 3 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 public/locale/i18n/de/common.json diff --git a/public/locale/i18n/de/common.json b/public/locale/i18n/de/common.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/public/locale/i18n/de/common.json @@ -0,0 +1 @@ +{} diff --git a/public/locale/i18n/de/resource.json b/public/locale/i18n/de/resource.json index c2e6159..63e5dd7 100644 --- a/public/locale/i18n/de/resource.json +++ b/public/locale/i18n/de/resource.json @@ -49,5 +49,26 @@ "Go back to homepage": "Zurück zur Hompage", "You have to judge every candidate/proposal!": "Sie müssen jeden Kandidaten/Abstimmungsvorschlag bewerten!", "Your participation was recorded with success!": " Ihre Teilnahme wurde gespeichert!", - "Thanks for your participation.": " Vielen Dank für Ihre Teilnahme." + "Thanks for your participation.": " Vielen Dank für Ihre Teilnahme.", + "Unknown error. Try again please.": "__STRING_NOT_TRANSLATED__", + "Ending date:": "__STRING_NOT_TRANSLATED__", + "If you list voters' emails, only them will be able to access the election": "__STRING_NOT_TRANSLATED__", + "Dates": "__STRING_NOT_TRANSLATED__", + "The election will take place from": "__STRING_NOT_TRANSLATED__", + "at": "__STRING_NOT_TRANSLATED__", + "to": "__STRING_NOT_TRANSLATED__", + "Grades": "__STRING_NOT_TRANSLATED__", + "Voters' list": "__STRING_NOT_TRANSLATED__", + "Voters received a link to vote by email. Each link can be used only once!": "__STRING_NOT_TRANSLATED__", + "Results of the election:": "__STRING_NOT_TRANSLATED__", + "Graph": "__STRING_NOT_TRANSLATED__", + "Preference profile": "__STRING_NOT_TRANSLATED__", + "Oops... The election is unknown.": "__STRING_NOT_TRANSLATED__", + "The election is still going on. You can't access now to the results.": "__STRING_NOT_TRANSLATED__", + "No votes have been recorded yet. Come back later.": "__STRING_NOT_TRANSLATED__", + "The election has not started yet.": "__STRING_NOT_TRANSLATED__", + "The election is over. You can't vote anymore": "__STRING_NOT_TRANSLATED__", + "You need a token to vote in this election": "__STRING_NOT_TRANSLATED__", + "You seem to have already voted.": "__STRING_NOT_TRANSLATED__", + "The parameters of the election are incorrect.": "__STRING_NOT_TRANSLATED__" } diff --git a/src/components/views/CreateElection.jsx b/src/components/views/CreateElection.jsx index 7635a8d..59db056 100644 --- a/src/components/views/CreateElection.jsx +++ b/src/components/views/CreateElection.jsx @@ -39,6 +39,11 @@ import HelpButton from '../form/HelpButton'; import ButtonWithConfirm from '../form/ButtonWithConfirm'; import Loader from '../wait'; + +// Error messages +const AT_LEAST_2_CANDIDATES_ERROR = 'Please add at least 2 candidates.' +const NO_TITLE_ERROR = 'Please add a title.' + // Convert a Date object into YYYY-MM-DD const dateToISO = date => date.toISOString().substring(0, 10); @@ -156,7 +161,6 @@ class CreateElection extends Component { this.state = { candidates: [{label: ''}, {label: ''}], - numCandidatesWithLabel: 0, title: title || '', isVisibleTipsDragAndDropCandidate: true, numGrades: 7, @@ -212,7 +216,6 @@ class CreateElection extends Component { }); this.setState({ candidates: candidates, - numCandidatesWithLabel: numLabels, }); }; @@ -241,6 +244,28 @@ class CreateElection extends Component { this.setState({isAdvancedOptionsOpen: !this.state.isAdvancedOptionsOpen}); }; + checkFields() { + const { candidates, title } = this.state; + + if (!candidates) { + return {ok: false, msg: AT_LEAST_2_CANDIDATES_ERROR}; + } + + let numCandidates = 0; + candidates.forEach(c => { + if (c !== "") numCandidates += 1; + }) + if (numCandidates < 2) { + return {ok: false, msg: AT_LEAST_2_CANDIDATES_ERROR}; + } + + if (!title || title === "") { + return {ok: false, msg: NO_TITLE_ERROR}; + } + + return {ok: true, msg: "OK"}; + } + handleSubmit() { const { candidates, @@ -258,6 +283,14 @@ class CreateElection extends Component { const {t} = this.props; + const check = this.checkFields(); + if (!check.ok) { + toast.error(t(check.msg), { + position: toast.POSITION.TOP_CENTER, + }); + return + } + this.setState({waiting: true}); fetch(endpoint, { @@ -298,9 +331,9 @@ class CreateElection extends Component { .catch(error => error); } - handleSendWithoutCandidate = () => { + handleSendNotReady = (msg) => { const {t} = this.props; - toast.error(t('Please add at least 2 candidates.'), { + toast.error(t(msg), { position: toast.POSITION.TOP_CENTER, }); }; @@ -316,12 +349,12 @@ class CreateElection extends Component { candidates, numGrades, isAdvancedOptionsOpen, - numCandidatesWithLabel, electorEmails, } = this.state; const {t} = this.props; const grades = i18nGrades(); + const check = this.checkFields(); if (successCreate) return ; @@ -576,7 +609,7 @@ class CreateElection extends Component { - {numCandidatesWithLabel >= 2 ? ( + {check.ok ? ( @@ -675,7 +708,7 @@ class CreateElection extends Component {