-
Notifications
You must be signed in to change notification settings - Fork 444
/
Player.jsx
52 lines (44 loc) · 1.15 KB
/
Player.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
var PropTypes = require('prop-types');
var React = require('react');
var classNames = require('classnames');
class Player extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick(event) {
this.props.onPlayerSelected(this.props.doc.id);
}
componentDidMount() {
var comp = this;
var doc = comp.props.doc;
doc.subscribe();
doc.on('load', update);
doc.on('op', update);
function update() {
// `comp.props.doc.data` is now updated. re-render component.
comp.forceUpdate();
}
}
componentWillUnmount() {
this.doc.unsubscribe();
}
render() {
var classes = {
'player': true,
'selected': this.props.selected
};
return (
<li className={classNames(classes)} onClick={this.handleClick}>
<span className="name">{this.props.doc.data.name}</span>
<span className="score">{this.props.doc.data.score}</span>
</li>
);
}
}
Player.propTypes = {
doc: PropTypes.object.isRequired,
onPlayerSelected: PropTypes.func.isRequired,
selected: PropTypes.bool.isRequired
};
module.exports = Player;