Skip to content

Commit f519cc1

Browse files
Voting
1 parent 3de6c45 commit f519cc1

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

app/src/js/components/Feed.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ var React = require('react');
33
var ShowAddButton = require('./ShowAddButton');
44
var FeedForm = require('./FeedForm');
55
var FeedList = require('./FeedList');
6+
var _ = require('lodash');
67

78
var Feed = React.createClass({
89
getInitialState: function () {
910
var FEED_ITEMS = [
10-
{key: 1, title: 'Realtime data!', description: 'Firebase is cool!', voteCount: 49},
11-
{key: 2, title: 'Javascipt is fun', description: 'Javascript feature here!', voteCount: 34},
12-
{key: 3, title: 'Coffee makes you awake', description: 'A coffee pro here!', voteCount: 25}
11+
{key: '1', title: 'Realtime data!', description: 'Firebase is cool!', voteCount: 49},
12+
{key: '2', title: 'Javascipt is fun', description: 'Javascript feature here!', voteCount: 34},
13+
{key: '3', title: 'Coffee makes you awake', description: 'A coffee pro here!', voteCount: 25}
1314
];
1415
return {
1516
items: FEED_ITEMS,
@@ -24,14 +25,27 @@ var Feed = React.createClass({
2425
var newItems = this.state.items.concat([newItem]);
2526
this.setState({
2627
items: newItems,
27-
formDisplayed: false
28+
formDisplayed: false,
29+
key: this.state.items.length
2830
});
2931
},
3032
onToggleForm: function () {
3133
this.setState({
3234
formDisplayed: !this.state.formDisplayed
3335
});
3436
},
37+
onVote: function (item) {
38+
var items = _.uniq(this.state.items);
39+
var index = _.findIndex(items, function (feedItems) {
40+
return feedItems.key === item.key;
41+
});
42+
var oldObject = items[index];
43+
var newItems = _.pull(items, oldObject);
44+
newItems.push(item);
45+
this.setState({
46+
items: newItems
47+
});
48+
},
3549
render: function () {
3650
return (
3751
<div>
@@ -44,7 +58,7 @@ var Feed = React.createClass({
4458
<br/>
4559
<br/>
4660

47-
<FeedList items={this.state.items} />
61+
<FeedList items={this.state.items} onVote={this.onVote}/>
4862
</div>
4963
);
5064
}

app/src/js/components/FeedItem.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
/** @jsx React.DOM */
22
var React = require('react');
33
var FeedItem = React.createClass({
4+
vote: function (newCount) {
5+
this.props.onVote({
6+
key: this.props.key,
7+
title: this.props.title,
8+
description: this.props.description,
9+
voteCount: newCount
10+
})
11+
},
12+
voteUp: function () {
13+
var count = parseInt(this.props.voteCount, 10);
14+
var newCount = count + 1;
15+
this.vote(newCount);
16+
},
17+
voteDown: function () {
18+
var count = parseInt(this.props.voteCount, 10);
19+
var newCount = count - 1;
20+
this.vote(newCount);
21+
},
422
render: function () {
23+
var badgeClassName = this.props.voteCount < 0 ? 'badge badge-danger' : 'badge badge-success';
524
return (
6-
<li className="list-group-item">
7-
<span className="badge badge-success">{this.props.voteCount}</span>
25+
<li key={this.props.key} className="list-group-item">
26+
<span className={badgeClassName}>{this.props.voteCount}</span>
827
<h4>{this.props.title}</h4>
9-
<span>{this.props.desc}</span>
28+
<span>{this.props.description}</span>
1029
<span className="pull-right">
11-
<button id="up" className="btn btn-sm btn-primary">&uarr;</button>
12-
<button id="down" className="btn btn-sm btn-primary">&darr;</button>
30+
<button id="up" className="btn btn-sm btn-primary" onClick={this.voteUp}>&uarr;</button>
31+
<button id="down" className="btn btn-sm btn-primary" onClick={this.voteDown}>&darr;</button>
1332
</span>
1433
</li>
1534
)

app/src/js/components/FeedList.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ var FeedItem = require('./FeedItem');
44
var FeedList = React.createClass({
55
render: function () {
66
var feedItems = this.props.items.map(function (item) {
7-
return <FeedItem title={item.title} desc={item.description} voteCount={item.voteCount} />
8-
});
7+
return <FeedItem key={item.key}
8+
title={item.title}
9+
description={item.description}
10+
voteCount={item.voteCount}
11+
onVote={this.props.onVote} />
12+
}.bind(this));
913

1014
return (
1115
<ul className="list-group container">

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
"author": "Alejandro Nanez <alejonanez@gmail.com>",
1111
"license": "ISC",
1212
"devDependencies": {
13-
"react": "^0.11.1",
14-
"gulp-react": "^1.0.1",
1513
"gulp": "^3.8.8",
16-
"gulp-connect": "^2.0.6",
14+
"gulp-browserify": "^0.5.0",
1715
"gulp-concat": "^2.4.0",
16+
"gulp-connect": "^2.0.6",
1817
"gulp-open": "^0.2.8",
19-
"gulp-browserify": "^0.5.0",
18+
"gulp-react": "^1.0.1",
19+
"lodash": "^2.4.1",
20+
"react": "^0.11.1",
2021
"reactify": "^0.14.0"
2122
}
2223
}

0 commit comments

Comments
 (0)