refactored service to async/await
This commit is contained in:
parent
83b51e0bdd
commit
b8d10d914f
4 changed files with 15 additions and 17 deletions
|
@ -1,4 +1,5 @@
|
|||
body {
|
||||
background-color: #4a4a4a;
|
||||
font-size: 160%
|
||||
font-size: 160%;
|
||||
color: #fff
|
||||
}
|
||||
|
|
|
@ -1,27 +1,24 @@
|
|||
import React, { Component } from 'react';
|
||||
import ChannelService from '../../services/chanel';
|
||||
import ChannelService from '../../services/ChannelService';
|
||||
|
||||
import './ChannelList.scss';
|
||||
|
||||
export default class Channellist extends Component<any, any> {
|
||||
service: ChannelService;
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
this.state = {
|
||||
loaded: false,
|
||||
channels: [],
|
||||
};
|
||||
this.service = new ChannelService();
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
ChannelService.getAllChannels()
|
||||
.then(data => this.setState({ channels: data, loaded: true }))
|
||||
.catch(e => console.log(e));
|
||||
ChannelService.fetchAllChannels().then(data => {
|
||||
this.setState({ channels: data, loaded: true });
|
||||
});
|
||||
}
|
||||
public render() {
|
||||
const { loaded, channels } = this.state;
|
||||
console.log(this.state);
|
||||
if (!loaded) {
|
||||
return <div className='loading'>Loading...</div>;
|
||||
} else {
|
||||
|
|
9
src/services/ChannelService.ts
Normal file
9
src/services/ChannelService.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
const API_URL = 'http://haveachin.de:1888/v1/channels';
|
||||
|
||||
export default class ChannelService {
|
||||
public static async fetchAllChannels() {
|
||||
const response = await fetch('http://haveachin.de:1888/v1/channels');
|
||||
const json = await response.json();
|
||||
return json;
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
const API_URL = 'http://haveachin.de:1888/v1/channels';
|
||||
|
||||
export default class ChannelService {
|
||||
public static getAllChannels() {
|
||||
return fetch('http://haveachin.de:1888/v1/channels').then(res =>
|
||||
res.json()
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue