|
| 1 | +// require the module |
| 2 | +const React = require('react'); |
| 3 | +const url = 'http://128.31.24.189:8001/api/survey/start/'; |
| 4 | +const axios = require('axios'); |
| 5 | +const _ = require('underscore'); |
| 6 | + |
| 7 | +class TestAPI extends React.Component { |
| 8 | + |
| 9 | + constructor(props) { |
| 10 | + super(props); |
| 11 | + this.state = { |
| 12 | + questions: [], |
| 13 | + responses: [], |
| 14 | + selectedOption: [], |
| 15 | + score: 0 |
| 16 | + // surveyID: '', |
| 17 | + // userID: '' |
| 18 | + }; |
| 19 | + //React components using ES6 classes no longer autobind this to non React methods. |
| 20 | + this.handleFormReset = this.handleFormReset.bind(this); |
| 21 | + this.handleFormSubmit = this.handleFormSubmit.bind(this); |
| 22 | + } |
| 23 | + |
| 24 | + componentDidMount() { |
| 25 | + // this is where you make the axios call |
| 26 | + |
| 27 | + axios.get(url) |
| 28 | + .then(({ data })=> { |
| 29 | + console.log(data); // entire json array |
| 30 | + this.setState( |
| 31 | + {questions: data.questions} |
| 32 | + ); |
| 33 | + |
| 34 | + _.map(this.state.questions, question => { |
| 35 | + this.setState({responses: question.responseOptions }); |
| 36 | + }); |
| 37 | + |
| 38 | + _.map(this.state.responses, response => { |
| 39 | + this.setState({selectedOption: response.value}); |
| 40 | + }); |
| 41 | + |
| 42 | + |
| 43 | + }) |
| 44 | + .catch(function(error) { |
| 45 | + if (error.response) { |
| 46 | + console.log(error.response.data); |
| 47 | + console.log(error.response.status); |
| 48 | + console.log(error.response.headers); |
| 49 | + } else if (error.request) { |
| 50 | + console.log(error.request); |
| 51 | + } else { |
| 52 | + console.log('Error', error.message); |
| 53 | + } |
| 54 | + console.log(error.config); |
| 55 | + }); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + render () { |
| 60 | + |
| 61 | + return ( |
| 62 | + <div className ="container1"> |
| 63 | + <div className="container2"> |
| 64 | + <form onSubmit={this.handleFormSubmit} onReset={this.handleFormReset}> |
| 65 | + {this.renderQuestions()} |
| 66 | + <button className="btn btn-default" type="submit">Submit Responses</button> |
| 67 | + <button className="btn btn-default" type="reset">Reset Responses</button> |
| 68 | + </form> |
| 69 | + |
| 70 | + {/*{this.renderRadioButtons()}*/} |
| 71 | + |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + ); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + // handleOptionChange(changeEvent) { |
| 79 | + // this.setState({ |
| 80 | + // selectedOption: changeEvent.target.value |
| 81 | + // }); |
| 82 | + // } |
| 83 | + |
| 84 | + handleFormSubmit(formSubmitEvent) { |
| 85 | + formSubmitEvent.preventDefault(); |
| 86 | + console.log('You have selected to submit'); |
| 87 | + this.setState({ |
| 88 | + score: 100 |
| 89 | + }); |
| 90 | + console.log(this.state.score); |
| 91 | + } |
| 92 | + |
| 93 | + handleFormReset(formSubmitEvent) { |
| 94 | + formSubmitEvent.preventDefault(); |
| 95 | + console.log('You have selected to reset'); |
| 96 | + this.setState({ |
| 97 | + selectedOption: [] |
| 98 | + }); |
| 99 | + console.log(this.state.selectedOption); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + renderQuestions() { |
| 104 | + |
| 105 | + console.log(this.state.questions); |
| 106 | + |
| 107 | + console.log(this.state.responses); // the state of responses at a given time |
| 108 | + |
| 109 | + return _.map(this.state.questions, question => { |
| 110 | + |
| 111 | + var question_text= question.questionText; |
| 112 | + |
| 113 | + //var question_id = question.tabindex; |
| 114 | + |
| 115 | + return ( _.map(question.responseOptions, response => { |
| 116 | + |
| 117 | + var response_text = response.displayText; |
| 118 | + var response_id = response.id; |
| 119 | + |
| 120 | + // currently returning the question multiple times |
| 121 | + // how do i get it to show the question once, and then a list of responses for every question? |
| 122 | + return ( |
| 123 | + |
| 124 | + <ul><label>{question_text}</label> |
| 125 | + <ul key ={response_id}></ul> |
| 126 | + |
| 127 | + {/*// <input type='radio' value ="response option">{response_text}</input>*/} |
| 128 | + <ul> |
| 129 | + <button type = "button"ç>{response_text}</button> |
| 130 | + </ul> |
| 131 | + </ul> |
| 132 | + |
| 133 | + ); |
| 134 | + })); |
| 135 | + } |
| 136 | + |
| 137 | + ); |
| 138 | + |
| 139 | + |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | +module.exports = TestAPI; |
| 145 | + |
0 commit comments