From 5c10eb15e415101f4259ef3034c7eb28b638d40d Mon Sep 17 00:00:00 2001 From: ayshiff Date: Tue, 5 Jun 2018 16:57:33 +0200 Subject: [PATCH] Final update logic front --- client/src/App.js | 67 ++++++++++++++++++++++++++--------------------- makefile | 2 +- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/client/src/App.js b/client/src/App.js index a507fc2..e519fbe 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -7,60 +7,66 @@ class App extends Component { constructor(props) { super(props); this.state = { - currentResponses: ["zert","zert","zert","zert"], - currentQuestion: "zefzeezze", - currentResponsesContent: ["zert","zert","zert","zert"], - currentResponsesChild: [0,1,2,3], + currentResponses: [], + currentQuestion: "zfz", pastQuestion: null, pastResponse: null, score: 0, - - } + start: true + }; + this.fetchQuestion = this.fetchQuestion.bind(this); + this.fetchResponses = this.fetchResponses.bind(this); } - _handleClick(event) { - + _handleClick(element) { + this.setState({ + pastQuestion: element.question , + pastResponse: element.text + }); + this.fetchQuestion(element.child); + this.fetchResponses(element.child+"/responses"); } - fetchQuestion(id) { - axios.get('http://localhost:8000/questions/'+ id) + fetchQuestion(url) { + axios.get(url) .then(((data) => { this.setState({ - currentResponses : data.responses, currentQuestion: data.text }) })) + .catch(error => console.log(error)); } - fetchResponses() { - this.state.currentResponses.forEach((element) => { - axios.get(element) - .then(((t) => { - this.setState(prevState => ({ - currentResponsesContent: [...prevState.currentResponsesContent, t.text], - currentResponsesChild: [...prevState.currentResponsesChild, t.child] - })) - })) - }) - - } + fetchResponses(id) { + axios.get('http://localhost:8000/questions/'+ id +'responses') + .then(((data) => { + this.setState({ + currentResponses : data + }) + })) + } componentWillMount(){ - this.fetchQuestion(0); - this.fetchResponses(); + if(this.state.start) { + this.fetchQuestion("http://localhost:8000/questions/0"); + this.fetchResponses(0); + this.setState ({ + start: null + }) + } } render() { - let currentOptions = this.state.currentResponsesContent.map(((element, index) => ( -

{element}

+ let currentOptions = this.state.currentResponses.map(((element, index) => ( +

{element.text}

))); - let header = () => ( + let header = (

{this.state.pastQuestion}
→ {this.state.pastResponse}

- ) + ); return (
@@ -74,10 +80,11 @@ class App extends Component {

{this.state.currentQuestion}

Je fais quoi ?

- {this.state.currentResponsesContent ? currentOptions : null} + {this.state.currentResponses ? currentOptions : null}
+ Votre score : {this.state.score} ); } diff --git a/makefile b/makefile index b75052e..78e4214 100644 --- a/makefile +++ b/makefile @@ -20,4 +20,4 @@ front-build-prod: ./node_modules/.bin/encore production; front-run: - cd client && npm start; \ No newline at end of file + npm start; \ No newline at end of file