/
new.jsx
211 lines (199 loc) · 5.29 KB
/
new.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict'
import React from 'react';
import { Component } from 'react';
import { browserHistory } from 'react-router';
import Spning from './components/spning';
import Footer from './components/footer';
class New extends Component {
constructor(props) {
super(props);
this.state = {
options: [],
isSubmitting: false
}
}
componentDidMount() {
$('#globalTransition').css('display', 'none');
if(!userInfo.name){
// 不支持未登陆 landing, 滚去首页!
location.href = location.origin;
}
}
addOptions(e){
var _temp = this.state.options;
if(!$('#poll-option').val()) return
_temp.push({
'option': $('#poll-option').val(),
'count': 0,
'index': this.state.options.length
});
this.setState({
'options': _temp
});
$('#poll-option').val('');
}
deleteOptions(e){
var _index = $(e.target).attr('data-index');
var _temp = this.state.options;
_temp = _temp.filter(function(item, index){
return index !== ~~_index;
});
_temp.map(function(item, index){
item.index = index;
});
this.setState({
'options': _temp
});
}
submitPollData(){
if(this.state.options.length < 2 ){
alert('At least two options');
return
}
if(!$('#poll-title').val()){
alert('Input your poll title!');
return
}
if(!$('#poll-description').val()){
alert('Input your poll description!');
return
}
var _request = {
'title': $('#poll-title').val() || 'Default title',
'description': $('#poll-description').val() || 'Default description',
'options': this.state.options,
'ownerName': userInfo.name,
'voterList': []
}
this.setState({
'isSubmitting': true
});
$.ajax({
type: "POST",
url: '/api/insertPoll',
async: true,
contentType: "application/json;charset=utf-8",
data: JSON.stringify(_request),
dataType: 'json',
success: function (data) {
if(data && data.result) {
console.log(this.props);
browserHistory.push('/list/' + userInfo.name);
this.setState({
'isSubmitting': false
});
}
}.bind(this)
});
}
render() {
return (
<div className = "newpage">
<h2 className = "left">Create a new poll.</h2>
<span
className="input-group-addon"
style={{"text-align":"left"}}
>
Poll title:
</span>
<div
className="input-group"
style={{"width":"100%"}}
>
<input
type="text"
name="title"
id="poll-title"
className="form-control"
placeholder="Please input a title here ..."
aria-describedby="sizing-addon2"
/>
</div> <br />
<span
className="input-group-addon"
style={{"text-align":"left"}}
>
Description:
</span>
<div
className="input-group"
style={{"width":"100%"}}
>
<textarea
type="text"
rows="3"
name="description"
id="poll-description"
className="form-control"
placeholder="Descripte your polls ..."
aria-describedby="sizing-addon2"
>
</textarea>
</div> <br />
<span
className="input-group-addon"
style={{"text-align":"left"}}
>
Options:
</span>
<ul
className="list-group"
style={{"line-height": "34px"}}
>
{this.state.options.map(
(item, index)=>(
<li
className="list-group-item"
>
<div className="input-group">
{item.option}
<span className="input-group-btn">
<button
className="btn btn-danger"
type="button"
onClick={this.deleteOptions.bind(this)}
data-index={index}
>
Delete
</button>
</span>
</div>
</li>
)
)}
{this.state.options.length === 0?<li className="list-group-item">To be added ...</li>:''}
</ul>
<div className="row">
<div className="col-lg-12">
<div className="input-group">
<input
type = "text"
className="form-control"
id = "poll-option"
placeholder="Write your options and click the add button..."
name = "option"
/>
<span className="input-group-btn">
<button
className="btn btn-default"
type="button"
onClick={this.addOptions.bind(this)}
>
Add
</button>
</span>
</div>
</div>
</div><hr />
<button
className="btn btn-primary"
onClick={this.submitPollData.bind(this)}
>
{this.state.isSubmitting?<Spning />:'Submit'}
</button>
<Footer />
</div>
);
}
}
export default New;