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 { body {
background-color: #4a4a4a; background-color: #4a4a4a;
font-size: 160% font-size: 160%;
color: #fff
} }

View file

@ -1,27 +1,24 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import ChannelService from '../../services/chanel'; import ChannelService from '../../services/ChannelService';
import './ChannelList.scss'; import './ChannelList.scss';
export default class Channellist extends Component<any, any> { export default class Channellist extends Component<any, any> {
service: ChannelService;
constructor(props: any) { constructor(props: any) {
super(props); super(props);
this.state = { this.state = {
loaded: false, loaded: false,
channels: [], channels: [],
}; };
this.service = new ChannelService();
} }
componentDidMount() { componentDidMount() {
ChannelService.getAllChannels() ChannelService.fetchAllChannels().then(data => {
.then(data => this.setState({ channels: data, loaded: true })) this.setState({ channels: data, loaded: true });
.catch(e => console.log(e)); });
} }
public render() { public render() {
const { loaded, channels } = this.state; const { loaded, channels } = this.state;
console.log(this.state);
if (!loaded) { if (!loaded) {
return <div className='loading'>Loading...</div>; return <div className='loading'>Loading...</div>;
} else { } 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()
);
}
}