-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
170 lines (151 loc) · 4.06 KB
/
index.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React Tutorial with cats</title>
<link rel="stylesheet" href="css/bootstrap.min.css" />
<script src="scripts/react.js"></script>
<script src="scripts/react-dom.js"></script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/babel.min.js"></script>
</head>
<body>
<div id="content"></div>
<script type="text/babel">
/**
* Top component
*
*/
class CatApp extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this); // Add context
this.handleRemove = this.handleRemove.bind(this);
this.state = {items: [], text: ''};
}
/**
* Handle picture removal.
*
* Passed to CatList component through props,
* so called in CatList but executed in this component.
*/
handleRemove(id) {
var items = this.state.items;
// Go through items and delete when found
for (var i in items) {
if (items[i]['id'] == id) {
// Delete from array
delete items[i];
// Set state to a new array, this will trigger a new render
this.setState({
items: items
});
break;
}
}
}
render() {
return (
<div className="container">
<h1>Cat + React = Recat</h1>
<CatList items={this.state.items} handleRemove={this.handleRemove} />
<div className="row">
<div className="cold-md-12 text-center">
<form onSubmit={this.handleSubmit}>
<button className="btn btn-primary">Add cat</button>
</form>
</div>
</div>
</div>
);
}
/**
* Handle adding a new picture.
*
* This will:
* - Add a new record in the list of items
* - Call render() on this component because we update the state
* - Call render() on CatList component because we render its owner
* - React with JSX is smart to know that only one (new) component needs to be rendered in the DOM
*/
handleSubmit(e) {
e.preventDefault();
// Set id with current timestamp
var newItem = {
id: Date.now()
};
// Add newItem to state items, this will re-render the component
this.setState((prevState) => ({
items: prevState.items.concat(newItem)
}));
}
}
/**
* Show all cat pictures by looping through its prop.
*
* Here we just render the list of items provided by top component through props.
*
*/
class CatList extends React.Component {
render() {
return (
<div className='row'>
{this.props.items.map(item => (
<CatPicture key={item.id} handleRemove={this.props.handleRemove.bind(null, item.id)}/>
))}
</div>
);
}
}
/**
* Show a cat picture and its delete button
*
*/
class CatPicture extends React.Component {
constructor(props) {
super(props);
this.state = {
pictureUrl: '',
}
}
componentDidMount() {
// Get URL picture from api
var p = $.ajax({
url: 'http://thecatapi.com/api/images/get?format=xml&size=small'
});
var component = this;
// Once AJAX request completed, we get URL of the picture
p.always = function() {
var url = this.responseText.match(/\<url\>(.*?)\<\/url\>/i)[1];
// Store URL in state, this will re render the component
component.setState({
pictureUrl: url
});
}
}
render() {
if (this.state.pictureUrl !== '') {
return (
<div className="col-md-3 text-center">
<p>
<img src={this.state.pictureUrl}/>
</p>
<button onClick={this.props.handleRemove} className="btn btn-default">Remove</button>
</div>
)
}
// If no URL, show "loading"
return (
<div className="col-md-3 text-center">
<p className="text-center">
Loading...
</p>
<button onClick={this.props.handleRemove} className="btn btn-default">Remove</button>
</div>
)
}
}
ReactDOM.render(<CatApp />, document.getElementById('content'));
</script>
</body>
</html>