-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredux-leaderboard.jsx
151 lines (131 loc) · 3.77 KB
/
redux-leaderboard.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
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { connect, Provider } from 'react-redux';
import thunk from 'redux-thunk';
import React, { PropTypes, Component } from 'react';
import { render } from 'react-dom';
import classNames from 'classnames';
import { _ } from 'meteor/underscore';
import { collectionReducer, syncCollectionToStore/*, simulateUpdate*/ } from './lib/ddp-redux';
const initialAppState = {
selectedPlayerId: '',
};
const appReducer = (state = initialAppState, { type, payload }) => {
switch (type) {
case 'SELECT_PLAYER':
return {
...state,
selectedPlayerId: payload,
};
default:
return state;
}
};
const rootReducer = combineReducers({
app: appReducer,
collections: collectionReducer,
});
const store = applyMiddleware(thunk)(createStore)(rootReducer);
store.subscribe(() => {
console.log(store.getState());
});
syncCollectionToStore('players', store);
Meteor.subscribe('players');
class Player extends Component {
selectedClass() {
const { selectedPlayer } = this.props;
return classNames('player', { selected: selectedPlayer && selectedPlayer._id === this.props._id});
}
handleSelect(player, e) {
this.props.onSelect(player, e);
}
render() {
return (
<li onClick={this.handleSelect.bind(this, this.props._id)} className={this.selectedClass()}>
<span className="name">{this.props.name}</span>
<span className="score">{this.props.score}</span>
</li>
);
}
}
class Leaderboard extends Component {
handleSelectPlayer(playerId, e) {
const { selectedPlayer } = this.props;
if (selectedPlayer && selectedPlayer._id === playerId) return;
this.props.dispatch({
type: 'SELECT_PLAYER',
payload: playerId,
});
}
handleIncreasePoints(e) {
this.props.dispatch((dispatch, getState) => {
const { app: { selectedPlayerId } } = getState();
/*dispatch(simulateUpdate(
'players',
selectedPlayerId,
{ score: this.props.selectedPlayer.score + 5 }
));*/
Meteor.call('players.update-score', selectedPlayerId, (error, result) => {
error && console.error(error);
result && console.log(result);
});
});
}
renderBottom() {
if (this.props.selectedPlayer) {
return (
<div className="details">
<div className="name">{this.props.selectedPlayer.name}</div>
<button onClick={this.handleIncreasePoints.bind(this)} className="inc">Add 5 points</button>
</div>
);
} else {
return (
<div className="message">Click a player to select</div>
);
}
}
renderPlayers() {
return this.props.players.map(player => (
<Player
key={player._id}
{...player}
selectedPlayer={this.props.selectedPlayer}
onSelect={this.handleSelectPlayer.bind(this)} />
));
}
render() {
return (
<div>
<ol className="leaderboard">
{this.renderPlayers()}
</ol>
{this.renderBottom()}
</div>
);
}
}
const LeaderboardContainer = connect(
({ collections, app }) => ({
players: _.sortBy(collections.players, 'score').reverse(),
selectedPlayer: collections.players && collections.players[app.selectedPlayerId],
})
)(Leaderboard);
const App = () => (
<div className="outer">
<div className="logo"></div>
<h1 className="title">Leaderboard</h1>
<div className="subtitle">Select a scientist to give them points</div>
<LeaderboardContainer />
</div>
);
Meteor.startup(() => {
window.addEventListener('DOMContentLoaded', () => {
let root = document.createElement('root');
document.body.appendChild(root);
render((
<Provider store={store}>
<App />
</Provider>
), root);
});
});