|
| 1 | +/*global require*/ |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +var React = require('react'); |
| 5 | +var ReactDOM = require('react-dom'); |
| 6 | +var immstruct = require('immstruct'); |
| 7 | +var Immutable = require('immutable'); |
| 8 | + |
| 9 | +document.addEventListener("DOMContentLoaded", function(event) { |
| 10 | + ReactDOM.render( |
| 11 | + <App />, |
| 12 | + document.getElementById('app') |
| 13 | + ); |
| 14 | +}); |
| 15 | + |
| 16 | + |
| 17 | +var App = React.createClass({ |
| 18 | + getInitialState: function(){ |
| 19 | + var structure = immstruct('survey-data', { newSurveyName: '', surveys: [] }); |
| 20 | + structure.on('swap', function (newStructure, oldStructure, keyPath) { |
| 21 | + this.setState({ cursor: immstruct('survey-data').cursor() }); |
| 22 | + }.bind(this)); |
| 23 | + return { |
| 24 | + cursor: structure.cursor(), |
| 25 | + newSurveyName: '', |
| 26 | + }; |
| 27 | + }, |
| 28 | + |
| 29 | + updateNewSurveyName: function(e) { |
| 30 | + this.state.cursor.cursor('newSurveyName').update(function(){ |
| 31 | + return e.target.value; |
| 32 | + }.bind(this)); |
| 33 | + }, |
| 34 | + |
| 35 | + createSurvey: function(e) { |
| 36 | + e.preventDefault(); |
| 37 | + var name = this.state.cursor.cursor('newSurveyName').deref().trim(); |
| 38 | + if (name) { |
| 39 | + this.state.cursor.cursor('surveys').update(function(surveys){ |
| 40 | + var s = surveys.push(Immutable.fromJS({ name: name, questions: [] })); |
| 41 | + return s; |
| 42 | + }.bind(this)); |
| 43 | + this.state.cursor.cursor('newSurveyName').update(function() { |
| 44 | + return ''; |
| 45 | + }); |
| 46 | + } |
| 47 | + }, |
| 48 | + |
| 49 | + removeSurvey: function(i) { |
| 50 | + this.state.cursor.cursor('surveys').delete(i); |
| 51 | + }, |
| 52 | + |
| 53 | + render: function(){ |
| 54 | + return <div> |
| 55 | + <h1>Survery Builder</h1> |
| 56 | + <div className='pure-g'> |
| 57 | + <div className='pure-u-1-2'> |
| 58 | + <form className='pure-form' onSubmit={this.createSurvey}> |
| 59 | + <p> |
| 60 | + <button |
| 61 | + type='button' |
| 62 | + className='pure-button' |
| 63 | + placeholder='survey name' |
| 64 | + onClick={this.createSurvey} |
| 65 | + > |
| 66 | + <i className='fa fa-plus-circle'></i> Create Survey |
| 67 | + </button> |
| 68 | + {' '} |
| 69 | + <input |
| 70 | + type='text' |
| 71 | + id='' |
| 72 | + value={this.state.cursor.cursor('newSurveyName').deref()} |
| 73 | + onChange={this.updateNewSurveyName} |
| 74 | + /> |
| 75 | + </p> |
| 76 | + </form> |
| 77 | + |
| 78 | + {this.state.cursor.cursor('surveys').toJS().map(function(s, i){ |
| 79 | + return <Survey key={i} removeSurvey={this.removeSurvey.bind(this, i)} survey={this.state.cursor.cursor(['surveys', i])} />; |
| 80 | + }.bind(this))} |
| 81 | + </div> |
| 82 | + <div className='pure-u-1-2'> |
| 83 | + <pre>{JSON.stringify(this.state.cursor.toJS(), undefined, ' ')}</pre> |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + </div>; |
| 87 | + } |
| 88 | +}); |
| 89 | + |
| 90 | +var Survey = React.createClass({ |
| 91 | + shouldComponentUpdate: function(nextProps, nextState){ |
| 92 | + return nextProps.survey.deref() !== this.props.survey.deref(); |
| 93 | + }, |
| 94 | + |
| 95 | + createQuestion: function(e) { |
| 96 | + e.preventDefault(); |
| 97 | + var name = this.props.survey.cursor('newQuestion').deref(); |
| 98 | + if (name) { |
| 99 | + this.props.survey.cursor('questions').update(function(questions){ |
| 100 | + var q = Immutable.fromJS({name: name, newAnswer: '', answers: [] }); |
| 101 | + return questions.push(q); |
| 102 | + }.bind(this)); |
| 103 | + this.props.survey.cursor('newQuestion').update(function() { |
| 104 | + return ''; |
| 105 | + }); |
| 106 | + } |
| 107 | + }, |
| 108 | + |
| 109 | + removeQuestion: function(i) { |
| 110 | + this.props.survey.cursor('questions').delete(i); |
| 111 | + }, |
| 112 | + |
| 113 | + updateNewQuestion: function(e) { |
| 114 | + this.props.survey.cursor('newQuestion').update(function(){ |
| 115 | + return e.target.value; |
| 116 | + }.bind(this)); |
| 117 | + }, |
| 118 | + |
| 119 | + render: function() { |
| 120 | + var s = this.props.survey; |
| 121 | + console.log('render survey', s.cursor('name').deref()); |
| 122 | + console.log('rendering survey'); |
| 123 | + return <div className='survey'> |
| 124 | + <h3> |
| 125 | + Survey: {s.cursor('name').deref()} |
| 126 | + </h3> |
| 127 | + <p> |
| 128 | + <button type='button' className='pure-button button-error button-xsmall' onClick={this.props.removeSurvey}> |
| 129 | + <i className='fa fa-minus-circle'></i> Remove Survey |
| 130 | + </button> |
| 131 | + </p> |
| 132 | + <form className='pure-form' onSubmit={this.createQuestion}> |
| 133 | + <button className='pure-button' type='button' onClick={this.createQuestion}> |
| 134 | + <i className='fa fa-plus-circle'></i> Create Question |
| 135 | + </button> |
| 136 | + {' '} |
| 137 | + <input |
| 138 | + type='text' |
| 139 | + id='' |
| 140 | + value={s.cursor('newQuestion').deref() || ''} |
| 141 | + onChange={this.updateNewQuestion} |
| 142 | + placeholder='question' |
| 143 | + /> |
| 144 | + </form> |
| 145 | + {this.props.survey.cursor('questions').toJS().map(function(q, i){ |
| 146 | + return <Question key={i} question={s.cursor(['questions', i])} removeQuestion={this.removeQuestion.bind(this, i)} />; |
| 147 | + }.bind(this))} |
| 148 | + </div>; |
| 149 | + } |
| 150 | +}); |
| 151 | + |
| 152 | +var Question = React.createClass({ |
| 153 | + shouldComponentUpdate: function(nextProps, nextState){ |
| 154 | + return nextProps.question.deref() !== this.props.question.deref(); |
| 155 | + }, |
| 156 | + |
| 157 | + updateAnswer: function(e) { |
| 158 | + this.props.question.cursor('newAnswer').update(function(){ |
| 159 | + return e.target.value; |
| 160 | + }); |
| 161 | + }, |
| 162 | + |
| 163 | + createAnswer: function(e) { |
| 164 | + e.preventDefault(); |
| 165 | + |
| 166 | + var q = this.props.question; |
| 167 | + var name = q.cursor('newAnswer').deref().trim(); |
| 168 | + |
| 169 | + if (name) { |
| 170 | + q.cursor('answers').update(function(answers) { |
| 171 | + return answers.push(Immutable.fromJS({name: name })); |
| 172 | + }); |
| 173 | + q.cursor('newAnswer').update(function(){ |
| 174 | + return ''; |
| 175 | + }); |
| 176 | + } |
| 177 | + }, |
| 178 | + |
| 179 | + removeAnswer: function(i, e) { |
| 180 | + this.props.question.cursor('answers').update(function(answers){ |
| 181 | + return answers.delete(i); |
| 182 | + }.bind(this)); |
| 183 | + }, |
| 184 | + |
| 185 | + render: function() { |
| 186 | + var q = this.props.question; |
| 187 | + console.log('render question', q.cursor('name').deref()); |
| 188 | + var answers = q.cursor('answers').toJS() || []; |
| 189 | + return <div className='question'> |
| 190 | + <h4>Question: {q.cursor('name').deref()}</h4> |
| 191 | + <p> |
| 192 | + <button type='button' className='pure-button button-error button-xsmall' onClick={this.props.removeQuestion}> |
| 193 | + <i className='fa fa-minus-circle'></i> Remove Question |
| 194 | + </button> |
| 195 | + </p> |
| 196 | + <form onSubmit={this.createAnswer} className='pure-form'> |
| 197 | + <button type='submit' className='pure-button'> |
| 198 | + <i className='fa fa-plus-circle'></i> Add Answer |
| 199 | + </button> |
| 200 | + {' '} |
| 201 | + <input |
| 202 | + type='text' |
| 203 | + id='' |
| 204 | + value={q.cursor('newAnswer').deref() || ''} |
| 205 | + onChange={this.updateAnswer} |
| 206 | + placeholder='new answer' |
| 207 | + /> |
| 208 | + </form> |
| 209 | + <ul className='answer-list'> |
| 210 | + {answers.map(function(a, i){ |
| 211 | + return <li key={i}> |
| 212 | + <button type='button' className='pure-button button-xsmall button-error' onClick={this.removeAnswer.bind(this, i)}> |
| 213 | + <i className='fa fa-minus-circle'></i> Remove |
| 214 | + </button> |
| 215 | + {' '} |
| 216 | + <Answer answer={q.cursor(['answers', i])} /> |
| 217 | + </li>; |
| 218 | + }.bind(this))} |
| 219 | + </ul> |
| 220 | + </div>; |
| 221 | + } |
| 222 | +}); |
| 223 | + |
| 224 | +var Answer = React.createClass({ |
| 225 | + shouldComponentUpdate: function(nextProps, nextState){ |
| 226 | + return nextProps.answer.deref() !== this.props.answer.deref(); |
| 227 | + }, |
| 228 | + |
| 229 | + render: function() { |
| 230 | + console.log('render answer', this.props.answer.cursor('name').deref()); |
| 231 | + return <span> |
| 232 | + {this.props.answer.cursor('name').deref()} |
| 233 | + </span>; |
| 234 | + } |
| 235 | +}); |
0 commit comments