Skip to content

Commit 0bc91b6

Browse files
committed
Changed react to react-with-addons, made reset no longer close the check, fixed data flow issues
1 parent af60cfa commit 0bc91b6

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

esp/public/media/scripts/program/modules/scheduling_checks.jsx

+38-20
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,33 @@ var SchedulingCheck = React.createClass({
4141
greyed: {},
4242
sort: -1,
4343
reverse: false
44-
}
44+
},
45+
tableKey: 0 // so React knows to rerender the table
4546
};
4647
},
4748

48-
updateTableState: function (state) {
49-
this.state.tableState = state;
49+
sortColumn: function (column) {
50+
if (this.state.tableState.sort === column) {
51+
this.setState( {
52+
tableState: React.addons.update(this.state.tableState,
53+
{ reverse: {$set: !this.state.tableState.reverse} } ),
54+
tableKey: this.state.tableKey + 1
55+
} );
56+
} else {
57+
this.setState( {
58+
tableState: React.addons.update(this.state.tableState, { sort: {$set: column}, reverse: {$set: false} }),
59+
tableKey: this.state.tableKey + 1
60+
} );
61+
}
62+
},
63+
64+
greyRow: function(item ){
65+
newkey = {};
66+
newkey[item] = !this.state.tableState.greyed[item];
67+
this.setState( {
68+
tableState: React.addons.update(this.state.tableState, { greyed: {$merge: newkey} } ),
69+
tableKey: this.state.tableKey + 1
70+
} );
5071
},
5172

5273
resetTable: function () {
@@ -56,7 +77,7 @@ var SchedulingCheck = React.createClass({
5677
sort: -1,
5778
reverse: false
5879
},
59-
open: false
80+
tableKey: this.state.tableKey + 1
6081
});
6182

6283
},
@@ -113,7 +134,10 @@ var SchedulingCheck = React.createClass({
113134
var data = JSON.parse(this.state.data); // Might not work on old browsers
114135
var table;
115136
if (data.headings.length == 0) {
116-
table = <SelectTable rows = {data.body} header = {false} saveState = {this.state.tableState} updateTableState = {this.updateTableState} />;
137+
table = <SelectTable rows = {data.body} header = {false}
138+
saveState = {this.state.tableState}
139+
clickHeader = {this.sortColumn} clickRow = {this.greyRow}
140+
key = {this.state.tableKey} />;
117141
} else {
118142
var columns = [];
119143
for (i = 0; i < data.headings.length; i++) {
@@ -123,7 +147,10 @@ var SchedulingCheck = React.createClass({
123147
columns[i] = {key: String(i), label: "--"};
124148
}
125149
}
126-
table = <SelectTable rows = {data.body} columns = {columns} header = {true} saveState = {this.state.tableState} updateTableState = {this.updateTableState} />;
150+
table = <SelectTable rows = {data.body} columns = {columns}
151+
header = {true} saveState = {this.state.tableState}
152+
clickHeader = {this.sortColumn} clickRow = {this.greyRow}
153+
key = {this.state.tableKey} />;
127154
}
128155
body = <div>
129156
<div className="placeholder">
@@ -188,20 +215,18 @@ var SelectTable = React.createClass({
188215
}).isRequired,
189216
header: React.PropTypes.bool.isRequired,
190217
columns: React.PropTypes.array,
191-
updateTableState: React.PropTypes.func.isRequired
218+
clickHeader: React.PropTypes.func.isRequired,
219+
clickRow: React.PropTypes.func.isRequired
192220
},
193221

194222
getInitialState: function(){
195-
// We will store the sorted column and whether each row is greyed out
196223
return {sort: this.props.saveState.sort, greyed: this.props.saveState.greyed, reverse: this.props.saveState.reverse};
197224
},
198225
render: function(){
199-
this.props.updateTableState(this.state);
200-
var me = this;
201226
// clone the rows
202227
items = this.props.rows.slice();
203228

204-
items = _.sortBy(items, me.state.sort);
229+
items = _.sortBy(items, this.state.sort);
205230

206231
if (this.state.reverse) items.reverse();
207232

@@ -241,17 +266,10 @@ var SelectTable = React.createClass({
241266
},
242267

243268
onClickHeader: function( e, column ){
244-
if (this.state.sort === column) {
245-
this.setState( {reverse: !this.state.reverse} );
246-
} else {
247-
this.setState( {sort: column, reverse: false} );
248-
}
269+
this.props.clickHeader(column);
249270
},
250271

251272
onClickRow: function( e, item ){
252-
// kind of kludgy but I can't figure out how to do this more nicely
253-
newgreyed = jQuery.extend({}, this.state.greyed);
254-
newgreyed[item] = !newgreyed[item];
255-
this.setState( {greyed: newgreyed} );
273+
this.props.clickRow(item);
256274
}
257275
});

esp/templates/elements/html

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<script type="text/javascript" src="/media/scripts/common.js"></script>
4545
{% endblock jquery %}
4646

47-
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
47+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react-with-addons.js"></script>
4848
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
4949
<script type="text/javascript" src="/media/scripts/lodash.compat.min.js"></script>
5050

0 commit comments

Comments
 (0)