Add error message if loading fails

This commit is contained in:
2020-06-01 15:51:55 +02:00
parent 053eb38cd2
commit be3a400512
3 changed files with 24 additions and 7 deletions

View File

@@ -40,6 +40,11 @@
color: #88c9db; color: #88c9db;
} }
.error-message {
color: red;
}
footer { footer {
padding: 2rem; padding: 2rem;
} }

View File

@@ -27,7 +27,10 @@ export default class App extends React.Component {
componentDidMount() { componentDidMount() {
this.setState({isLoaded: false}) this.setState({isLoaded: false})
this.mockService.getStats() this.mockService.getStats()
.then(data => this.setState({mock: data})); .then(data => this.setState({
data: data,
mock: data
}));
this.apiService.getStats() this.apiService.getStats()
.then(data => this.setState({ .then(data => this.setState({
isLoaded: true, isLoaded: true,
@@ -86,13 +89,14 @@ export default class App extends React.Component {
return ( return (
<div className="App"> <div className="App">
<p className="title">Humenius' TeamSpeak 3-Ranking</p> <p className="title">Humenius' TeamSpeak 3-Ranking</p>
{ this.state.error != null ? <p className="error-message">Data could not be loaded. Please try again later!</p> : null}
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Placement</th> <th>Placement</th>
<th>Name</th> <th>Name</th>
<th>Online time</th> <th>Online time</th>
</tr> </tr>
</thead> </thead>
<tbody className={this.state.error != null ? "blurred" : undefined}> <tbody className={this.state.error != null ? "blurred" : undefined}>
{this.renderTableData()} {this.renderTableData()}

View File

@@ -13,6 +13,14 @@ export default class UserStatsService {
async getStats(): Promise<TableEntry[]> { async getStats(): Promise<TableEntry[]> {
return fetch(this.apiURL, this.requestInit) return fetch(this.apiURL, this.requestInit)
.then(res => { console.log(res); return res; }) .then(res => { console.log(res); return res; })
.then(res => res.json()) .then(res => this.checkResponse(res))
.then(data => data.json())
}
private checkResponse(response: any): any {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
} }
} }