-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaunchesList.js
124 lines (115 loc) · 3.35 KB
/
LaunchesList.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
import React from 'react';
import PropTypes from 'prop-types';
import { MoonLoader } from 'react-spinners';
import LaunchesListHeader from '../components/LaunchesListHeader';
import FilterButtons from '../components/FilterButtons';
import FilteredLaunch from '../components/FilteredLaunch';
class LaunchesList extends React.Component {
static propTypes = {
onLaunchClick: PropTypes.func.isRequired,
};
constructor(props) {
super(props);
this.state = {
availableRocketNames: ['All Rockets', 'Falcon 1', 'Falcon 9', 'Falcon 10', 'Falcon Heavy'],
filteredLaunches: [],
rocketNameFilter: 'All Rockets',
isLoading: false,
isEmpty: false,
isError: false,
};
this.handleFilterChange = this.handleFilterChange.bind(this);
}
componentWillMount() {
this.filteredLaunches();
}
filteredLaunches(filter = 'all') {
this.setState({
isLoading: true,
});
let url = 'https://api.spacexdata.com/v2/launches/all';
if (filter !== 'all') {
url = `https://api.spacexdata.com/v2/launches?rocket_name=${filter}`;
}
fetch(url)
.then((response) => {
if (response.ok) {
return response.json();
}
this.setState({
filteredLaunches: [],
isError: true,
isLoading: false,
});
throw new Error('Network response was not ok!');
}).then((data) => {
if (data && data.length !== 0) {
this.setState({
filteredLaunches: data,
isLoading: false,
isEmpty: false,
isError: false,
});
} else if (data && data.length === 0) {
this.setState({
filteredLaunches: [],
isEmpty: true,
isError: false,
isLoading: false,
});
}
}).catch(e => Promise.reject(e.message));
}
handleFilterChange(value) {
let filter = value;
this.setState({
rocketNameFilter: filter,
});
if (filter === 'All Rockets') {
filter = 'all';
}
this.filteredLaunches(filter);
}
render() {
const { onLaunchClick } = this.props;
const {
rocketNameFilter,
availableRocketNames,
filteredLaunches,
isLoading,
isEmpty,
isError,
} = this.state;
return (
<div className="launchesListWrapper">
<LaunchesListHeader />
<FilterButtons
rockets={availableRocketNames}
handleFilterChange={this.handleFilterChange}
currentFilter={rocketNameFilter}
/>
<div className={`launchesList ${isLoading ? 'launchesList--hidden' : ''}`}>
{filteredLaunches.map((launch, index) => (
<FilteredLaunch
filteredLaunch={launch}
index={index}
key={launch.flight_number}
onLaunchClick={onLaunchClick}
/>
))}
</div>
<div className={`infoContainer ${(isEmpty || isError) && !isLoading ? '' : 'infoContainer--hidden'}`}>
<h3 className="infoContainer__message">{ isEmpty ? 'Sorry, no launches found' : 'Sorry, there is no server connection'}</h3>
</div>
<div className="loaderContainer">
<MoonLoader
color="#ccac5b"
loading={isLoading}
size={50}
/>
</div>
</div>
);
}
}
export default LaunchesList;