-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
119 lines (95 loc) · 2.58 KB
/
App.js
File metadata and controls
119 lines (95 loc) · 2.58 KB
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
import React, { Component } from 'react';
// import logo from './logo.svg';
import './App.css';
import Launch from './Launch';
const perPage = 16;
class App extends Component {
constructor() {
super();
this.state = {
offset: 0,
launches: [],
query: '',
};
this.handleQueryChange = this.handleQueryChange.bind(this);
this.search = this.search.bind(this);
this.resetSearch = this.resetSearch.bind(this);
this.loadNextPage = this.loadNextPage.bind(this);
this.loadPreviousPage = this.loadPreviousPage.bind(this);
}
componentDidMount() {
const { offset } = this.state;
this.fetchLaunches(offset);
}
fetchLaunches(offset, query) {
const queryParam = query ? `&name=${query}` : '';
let url = `https://launchlibrary.net/1.3/launch/next/${perPage}?offset=${offset}${queryParam}`;
fetch(url)
.then(response => response.json())
.then(response => {
this.setState({
offset,
query,
launches: response.launches,
})
})
};
search() {
const { query } = this.state;
this.fetchLaunches(0, query);
}
resetSearch() {
this.fetchLaunches(0, '');
}
handleQueryChange(event) {
this.setState({
query: event.target.value,
})
}
loadNextPage() {
const { offset, query } = this.state;
const newOffset = offset + perPage;
this.fetchLaunches(newOffset, query);
}
loadPreviousPage() {
const { offset, query } = this.state;
const newOffset = offset - perPage;
this.fetchLaunches(newOffset, query);
}
renderLaunches() {
const { launches } = this.state;
return launches.map(launch => (
<Launch
id={launch.id}
name={launch.name}
locationName={launch.location.name}
rocketName={launch.rocket.name}
imgUrl={launch.rocket.imageURL}
/>
))
}
render() {
const { offset } = this.state;
return (
<div>
<h1>Launches</h1>
<div className="search-controls">
<input value={this.state.query}
onChange={this.handleQueryChange}
placeholder="Search by rocket name"
/>
<button onClick={this.search}>Search</button>
<button onClick={this.resetSearch}>Reset search</button>
</div>
<div className="launches">
{this.renderLaunches()}
</div>
<div className="controls">
<button onClick={this.loadPreviousPage} disabled={offset === 0}>prev</button>
<button onClick={this.loadNextPage}>next</button>
</div>
</div>
)
}
}
export default App;