forked from clankford/react-voting-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (134 loc) · 4.19 KB
/
app.js
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
const Product = React.createClass({
handleUpVote: function() {
this.props.onUpVote(this.props.id);
},
handleDownVote: function() {
this.props.onDownVote(this.props.id);
},
render: function() {
return (
<div className='item'>
<div className='image'>
<img src={this.props.product_image_url} />
</div>
<div class='middle aligned content'>
<div className='header'>
<a onClick={this.handleUpVote}>
<i className='large caret up icon'></i>
</a>
{this.props.votes}
</div>
<div className='header'>
<a onClick={this.handleDownVote}>
<i className='large caret down icon'></i>
</a>
</div>
<div className='description'>
<a href={this.props.url}>
{this.props.title}
</a>
</div>
<div className='extra'>
<span>Submitted by:</span>
<img
className='ui avatar image'
src={this.props.submitter_avatar_url}
/>
</div>
</div>
</div>
);
}
});
const Sort = React.createClass({
handleSortOrder: function() {
this.props.onSort();
},
render: function() {
return (
<div className='header'>
<a onClick={this.handleSortOrder}>
Sort Toggle: {this.props.sortBy}
</a>
</div>
)
}
});
const ProductList = React.createClass({
getInitialState: function() {
return {
products: [],
sort: 'descending'
};
},
componentDidMount: function() {
},
sortDescending: function() {
return (
Data.sort((a, b) => {
return b.votes - a.votes;
})
)
},
sortAscending: function() {
return (
Data.sort((a, b) => {
return a.votes - b.votes;
})
)
},
handleProductUpVote: function(productId) {
var products = Data.forEach((el) => {
if (el.id === productId) {
el.votes = el.votes + 1;
return;
}
});
this.setState({products: products});
},
handleProductDownVote: function(productId) {
var products = Data.forEach((el) => {
if (el.id === productId) {
el.votes = el.votes - 1;
return;
}
});
this.setState({products: products});
},
toggleSort: function() {
var newSort = this.state.sort == 'descending' ? 'ascending' : 'descending';
this.setState({sort: newSort})
},
render: function() {
var sortedProducts = this.state.sort == 'descending' ? this.sortDescending() : this.sortAscending();
const products = sortedProducts.map((product) => {
return (
<Product
key={'product-' + product.id}
id={product.id}
title={product.title}
description={product.description}
url={product.url}
votes={product.votes}
submitter_avatar_url={product.submitter_avatar_url}
product_image_url={product.product_image_url}
onUpVote={this.handleProductUpVote}
onDownVote={this.handleProductDownVote}
/>
);
});
return (
<div className='ui items'>
<Sort
onSort={this.toggleSort}
sortBy={this.state.sort}
/>
{products}
</div>
)
}
});
ReactDOM.render(
<ProductList />,
document.getElementById('content')
);