-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
123 lines (105 loc) · 2.44 KB
/
main.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
var React = require('react');
var ReactDOM = require('react-dom');
var ListContainer = React.createClass({
handleOrderChange: function(order){
if(order == 'popular'){
this.getQuotesFromServer('http://www.zaphod.xyz/'+ api_key +'/search/quotes?popularity=desc');
} else if (order == 'newest') {
this.getQuotesFromServer('http://www.zaphod.xyz/'+ api_key +'/search/quotes');
}
},
getQuotesFromServer: function(url){
$.ajax({
url: url,
dataType: 'json',
cache: false,
success: function(results) {
this.setState({quotes: results});
}.bind(this),
error: function(xhr, status, err){
console.log(err.toString());
}.bind(this)
});
},
getInitialState: function(){
return {
quotes: []
};
},
componentDidMount: function(){
this.getQuotesFromServer(this.props.source);
},
componentWillUnmount: function(){
this.quoteRequest.abort();
},
render: function(){
return(
<div>
<OrderByPopularityButton onOrderChange={this.handleOrderChange} />
<OrderByNewestButton onOrderChange={this.handleOrderChange} />
<ListOfQuotes quotes={this.state.quotes} />
</div>
);
}
});
var OrderByNewestButton = React.createClass({
handleClick: function(){
this.props.onOrderChange('newest');
},
render: function() {
return (
<button onClick={this.handleClick}>Order by Newest</button>
);
}
});
var OrderByPopularityButton = React.createClass({
handleClick: function(){
this.props.onOrderChange('popular');
},
render: function() {
return (
<button onClick={this.handleClick}>Order by Popularity</button>
);
}
});
var ListOfQuotes = React.createClass({
render: function(){
var quoteNodes = this.props.quotes.map(function(quote, index, quote_arr){
return (
<ListItem quote={quote.quotation} professor={quote.professor} key={quote.id}/>
);
});
return (
<div>{quoteNodes}</div>
);
}
});
var ListItem = React.createClass({
render: function() {
return (
<div>
<QuoteText quote={this.props.quote} />
<ProfessorName professor={this.props.professor} />
</div>
);
}
});
var QuoteText = React.createClass({
render: function() {
return (
<p>"{this.props.quote}"</p>
);
}
});
var ProfessorName = React.createClass({
render: function() {
return (
<p>--{this.props.professor}</p>
);
}
});
var source_url = 'http://www.zaphod.xyz/'+ api_key +'/search/quotes';
ReactDOM.render(
<ListContainer source={source_url} />,
document.getElementById('content')
);