From fd95bde103243c245018ab38da95ba58381b7f5a Mon Sep 17 00:00:00 2001 From: Pierre-Louis Guhur Date: Fri, 12 Jul 2019 00:05:02 +0200 Subject: [PATCH] connect to API on CreateElection (untested) --- src/components/views/CreateElection.js | 38 ++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/components/views/CreateElection.js b/src/components/views/CreateElection.js index 64c2377..0b270e3 100644 --- a/src/components/views/CreateElection.js +++ b/src/components/views/CreateElection.js @@ -21,7 +21,6 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import {faPlus, faTrashAlt, faCheck } from '@fortawesome/free-solid-svg-icons'; - //TODO : variable de config dans un fichier à part (avec les mentions, le min/max de mentions, le nombre max de candidats, les maxlength,l'url api, etc ...) const mentions = [ {label:"Excellent", color:"#015411"}, @@ -33,6 +32,8 @@ const mentions = [ {label:"A Rejeter", color:"#6f0214"}, ]; +const PATH_API = '/api/'; +const PATH_CREATE_ELECTION = 'create'; const DragHandle = sortableHandle(({children}) => {children}); @@ -88,11 +89,10 @@ class CreateElection extends Component { title:null, isVisibleTipsDragAndDropCandidate:true, nbMentions:7 - - }; this.candidateInputs = []; this.focusInput= React.createRef(); + this.handleSubmit = this.handleSubmit.bind(this); } @@ -156,10 +156,6 @@ class CreateElection extends Component { this.setState({candidates: candidates}); }; - handleSubmit= (event) => { - event.preventDefault(); - }; - handleChangeNbMentions= (event) => { this.setState({nbMentions: event.target.value}); }; @@ -170,6 +166,32 @@ class CreateElection extends Component { }; + handleSubmit () { + const { + candidates, + nbCandidatesWithLabel, + title, + isVisibleTipsDragAndDropCandidate, + nbMentions + } = this.state; + fetch(`${PATH_API}${PATH_CREATE_ELECTION}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + title: title, + candidates: candidates, + on_invitation_only: false, + num_grades: nbMentions, + elector_emails: [] + }) + } + ).then(response => response.json()) + .then(result => alert(result)) + .catch(error => error); + }; + handleSendWithoutCandidate = () => { toast.error("Vous devez saisir au moins deux candidats !", { position: toast.POSITION.TOP_CENTER @@ -274,7 +296,7 @@ class CreateElection extends Component { } -
{}}>Lancer le vote
+
Lancer le vote
Annuler
:}