-
Notifications
You must be signed in to change notification settings - Fork 0
/
17-application-pager-3.html
108 lines (98 loc) · 3.07 KB
/
17-application-pager-3.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React Workshop</title>
<link href="https://rawgit.com/kouphax/static/master/react-workshop/stylesheets/styles.css" rel="stylesheet">
</head>
<body>
<!-- NOT REQUIRED FOR REACT BUT MAY BE HANDY LATER -->
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<!-- REACT CORE LIBRARY (DEVELOPMENT VERSION) -->
<script src="http://fb.me/react-0.11.1.js"></script>
<!-- ONLY NEEDED FOR JSX FOR DEVELOPMENT AND PROTOTYPING -->
<script src="http://fb.me/JSXTransformer-0.11.1.js"></script>
<div id="container"></div>
<script>
var BuyButton = React.createClass({
render: function(){
if(this.props.quantity == 0) {
return React.DOM.div({className: 'buy'},
React.DOM.button({disabled: true}, "OUT OF STOCK"))
} else {
return React.DOM.div({className: 'buy'},
"£" + this.props.price.toFixed(2) + " ", React.DOM.button(null, 'Buy'))
}
}
})
var Paint = React.createClass({
render: function() {
return React.DOM.div({className: 'paint'},
React.DOM.div({
className: 'preview',
style: {
backgroundColor: this.props.code
}
}),
React.DOM.p(null, this.props.title),
BuyButton({quantity: this.props.quantity, price: this.props.price}))
}
})
var Pager = React.createClass({
render: function(){
if(this.props.pages == 0) {
return null;
} else {
var pagerItems = [];
for(var i = 1; i <= this.props.pages; i++) {
pagerItems.push(React.DOM.button({
disabled: this.props.page == i,
key: i,
onClick: this.props.onPageChangeClick.bind(null, i)
}, i))
}
return React.DOM.div(null, pagerItems)
}
}
})
var PaintList = React.createClass({
getInitialState: function(){
return {
paints: [],
pages: 0
}
},
componentDidMount: function(){
this.changePage(1);
},
changePage: function(pageNumber) {
jQuery.getJSON(this.props.url + "?page=" + pageNumber).done(function(result){
this.setState({
paints: result.data,
page: result.page,
pages: result.pages
})
}.bind(this))
},
render: function(){
var paints = this.state.paints.map(function(paint){
paint['key'] = paint['id'];
return Paint(paint);
})
return React.DOM.div(null, paints, Pager({
pages: this.state.pages,
page: this.state.page,
onPageChangeClick: this.changePage
}));
}
})
var Application = React.createClass({
render: function() {
return React.DOM.div(null, PaintList({ url: this.props.url }));
}
})
React.renderComponent(Application({url: "http://paints.herokuapp.com"}),
document.getElementById('container'))
</script>
</body>
</html>