From b8d10d914f22b8fa6551a664f04e572249c91ab9 Mon Sep 17 00:00:00 2001 From: 123niel Date: Thu, 14 Feb 2019 11:07:07 +0100 Subject: [PATCH] refactored service to async/await --- src/App.scss | 3 ++- src/components/Channel/ChannelList.tsx | 11 ++++------- src/services/ChannelService.ts | 9 +++++++++ src/services/chanel.ts | 9 --------- 4 files changed, 15 insertions(+), 17 deletions(-) create mode 100644 src/services/ChannelService.ts delete mode 100644 src/services/chanel.ts diff --git a/src/App.scss b/src/App.scss index ab3fb24..74ebcac 100644 --- a/src/App.scss +++ b/src/App.scss @@ -1,4 +1,5 @@ body { background-color: #4a4a4a; - font-size: 160% + font-size: 160%; + color: #fff } diff --git a/src/components/Channel/ChannelList.tsx b/src/components/Channel/ChannelList.tsx index ffb697a..3e97e96 100644 --- a/src/components/Channel/ChannelList.tsx +++ b/src/components/Channel/ChannelList.tsx @@ -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 { - 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
Loading...
; } else { diff --git a/src/services/ChannelService.ts b/src/services/ChannelService.ts new file mode 100644 index 0000000..15db882 --- /dev/null +++ b/src/services/ChannelService.ts @@ -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; + } +} diff --git a/src/services/chanel.ts b/src/services/chanel.ts deleted file mode 100644 index a2bd16b..0000000 --- a/src/services/chanel.ts +++ /dev/null @@ -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() - ); - } -}