refactored service to async/await

This commit is contained in:
Niel 2019-02-14 11:07:07 +01:00
parent 83b51e0bdd
commit b8d10d914f
4 changed files with 15 additions and 17 deletions

View file

@ -1,4 +1,5 @@
body {
background-color: #4a4a4a;
font-size: 160%
font-size: 160%;
color: #fff
}

View file

@ -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 {

View 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;
}
}

View file

@ -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()
);
}
}