From ae9c70b9c4da0d41974286d12e116e378c0407cf Mon Sep 17 00:00:00 2001 From: Niel Rohling Date: Thu, 25 Apr 2019 00:26:11 +0200 Subject: [PATCH] parse data from API and further styling --- src/components/App.js | 2 +- src/components/Channel/ChannelEntry.js | 28 ---------- src/components/Channel/ChannelView.js | 26 --------- src/components/Channel/Channels.js | 52 ------------------ src/components/ChannelEntry.js | 59 +++++++++++++++++++++ src/components/{Channel => }/ChannelList.js | 4 +- src/components/Channels.js | 54 +++++++++++++++++++ src/components/ClientList.js | 13 +++++ src/parseData.js | 36 +++++++++++++ 9 files changed, 165 insertions(+), 109 deletions(-) delete mode 100644 src/components/Channel/ChannelEntry.js delete mode 100644 src/components/Channel/ChannelView.js delete mode 100644 src/components/Channel/Channels.js create mode 100644 src/components/ChannelEntry.js rename src/components/{Channel => }/ChannelList.js (80%) create mode 100644 src/components/ClientList.js create mode 100644 src/parseData.js diff --git a/src/components/App.js b/src/components/App.js index 20e6345..1800604 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -1,6 +1,6 @@ import React from 'react'; import Header from './Header'; -import Channels from './Channel/Channels'; +import Channels from './Channels'; export default () => { return ( diff --git a/src/components/Channel/ChannelEntry.js b/src/components/Channel/ChannelEntry.js deleted file mode 100644 index e79bc61..0000000 --- a/src/components/Channel/ChannelEntry.js +++ /dev/null @@ -1,28 +0,0 @@ -import React from 'react'; -import ChannelList from './ChannelList'; - -export default props => { - const { clients, channel } = props; - const joined = []; - if (channel.totalClients > 0) { - clients.forEach(client => { - if (client.channelId === channel.id) joined.push(client); - }); - } - - return ( - <> - {channel.name} - {joined.length > 0 && ( - - )} - {channel.subchannels !== undefined && ( - - )} - - ); -}; diff --git a/src/components/Channel/ChannelView.js b/src/components/Channel/ChannelView.js deleted file mode 100644 index f8aff0c..0000000 --- a/src/components/Channel/ChannelView.js +++ /dev/null @@ -1,26 +0,0 @@ -import React from 'react'; - -export default ({ channel }) => { - return ( -
-

{channel.name}

-
    -
  • - {`# ${channel.id}`} - {channel.name} -
  • - {channel.totalClients >= 0 && ( -
  • - Users online - {channel.totalClients} -
  • - )} - {channel.subchannels.length > 0 && ( -
  • - Subchannels: -
  • - )} -
-
- ); -}; diff --git a/src/components/Channel/Channels.js b/src/components/Channel/Channels.js deleted file mode 100644 index 6f06fbe..0000000 --- a/src/components/Channel/Channels.js +++ /dev/null @@ -1,52 +0,0 @@ -import React, { Component } from 'react'; -import { withStyles } from '@material-ui/core/styles'; -import { Paper } from '@material-ui/core'; -import ApiService from '../../services/ApiService'; -import ChannelList from './ChannelList'; - -const styles = theme => ({ - root: { - ...theme.mixins.gutters(), - paddingTop: theme.spacing.unit * 2, - paddingBottom: theme.spacing.unit * 2, - margin: `${theme.spacing.unit}px auto`, - width: '500px', - }, -}); - -class Channels extends Component { - constructor(props) { - super(props); - this.state = { - loaded: false, - channels: [], - clients: [], - }; - } - - componentDidMount() { - ApiService.fetchAllChannels().then(data => { - this.setState({ - channels: data.channelJson, - clients: data.clientJson, - loaded: true, - }); - }); - } - - render() { - const { loaded, channels, clients } = this.state; - const { classes } = this.props; - return ( - - {!loaded ? ( -
Loading...
- ) : ( - - )} -
- ); - } -} - -export default withStyles(styles)(Channels); diff --git a/src/components/ChannelEntry.js b/src/components/ChannelEntry.js new file mode 100644 index 0000000..936a10f --- /dev/null +++ b/src/components/ChannelEntry.js @@ -0,0 +1,59 @@ +import React from 'react'; +import { withStyles } from '@material-ui/core/styles'; +import ChannelList from './ChannelList'; +import ClientList from './ClientList'; + +const styles = () => ({ + centered: { + textAlign: 'center', + }, +}); + +const ChannelEntry = props => { + const { clients, channel, classes } = props; + + // render Channel Title dependent of spacer-type + let ChannelTitle; + if (channel.spacer === 'fill' && channel.name === '_') + ChannelTitle = ( +
+
+
+ ); + else if (channel.spacer === 'centered') + ChannelTitle = ( +
+ {channel.name} +
+ ); + else + ChannelTitle = ( +
+ {channel.name} +
+ ); + + // render connected clients, if existent + const joined = []; + if (channel.totalClients > 0) { + clients.forEach(client => { + if (client.channelId === channel.id) joined.push(client); + }); + } + const ConnectedClients = joined.length > 0 && ; + + // render another ChannelList if channel has subchannels + const Subchannels = channel.subchannels !== undefined && ( + + ); + + return ( + <> + {ChannelTitle} + {ConnectedClients} + {Subchannels} + + ); +}; + +export default withStyles(styles)(ChannelEntry); diff --git a/src/components/Channel/ChannelList.js b/src/components/ChannelList.js similarity index 80% rename from src/components/Channel/ChannelList.js rename to src/components/ChannelList.js index 4b579b7..838c0c3 100644 --- a/src/components/Channel/ChannelList.js +++ b/src/components/ChannelList.js @@ -9,7 +9,7 @@ const styles = () => ({ }, }); -const channelList = ({ channels, clients, classes }) => { +const ChannelList = ({ channels, clients, classes }) => { return (
    @@ -23,4 +23,4 @@ const channelList = ({ channels, clients, classes }) => { ); }; -export default withStyles(styles)(channelList); +export default withStyles(styles)(ChannelList); diff --git a/src/components/Channels.js b/src/components/Channels.js index e69de29..be70fde 100644 --- a/src/components/Channels.js +++ b/src/components/Channels.js @@ -0,0 +1,54 @@ +import React, { Component } from 'react'; +import { withStyles } from '@material-ui/core/styles'; +import { Paper } from '@material-ui/core'; +import ApiService from '../services/ApiService'; +import ChannelList from './ChannelList'; +import parseData from '../parseData'; + +const styles = theme => ({ + root: { + ...theme.mixins.gutters(), + paddingTop: theme.spacing.unit * 2, + paddingBottom: theme.spacing.unit * 2, + margin: `${theme.spacing.unit}px auto`, + width: '500px', + }, +}); + +class Channels extends Component { + constructor(props) { + super(props); + this.state = { + loaded: false, + channels: [], + clients: [], + }; + } + + componentDidMount() { + ApiService.fetchAllChannels().then(data => { + const { channels, clients } = parseData(data); + this.setState({ + channels, + clients, + loaded: true, + }); + }); + } + + render() { + const { loaded, channels, clients } = this.state; + const { classes } = this.props; + return ( + + {!loaded ? ( +
    Loading...
    + ) : ( + + )} +
    + ); + } +} + +export default withStyles(styles)(Channels); diff --git a/src/components/ClientList.js b/src/components/ClientList.js new file mode 100644 index 0000000..92aa29a --- /dev/null +++ b/src/components/ClientList.js @@ -0,0 +1,13 @@ +import React from 'react'; + +const ClientList = ({ clients }) => { + return ( +
      + {clients.map(e => ( +
    • {e.nickname}
    • + ))} +
    + ); +}; + +export default ClientList; diff --git a/src/parseData.js b/src/parseData.js new file mode 100644 index 0000000..6610b0c --- /dev/null +++ b/src/parseData.js @@ -0,0 +1,36 @@ +const parseChannel = channel => { + const { name, id, subchannels, totalClients } = channel; + const parsedChannel = {}; + + parsedChannel.id = id; + + if (name.startsWith('[*spacer')) { + parsedChannel.name = '_'; + parsedChannel.spacer = 'fill'; + } else if (name.startsWith('[cspacer')) { + parsedChannel.name = name.slice(10); + parsedChannel.spacer = 'centered'; + } else { + parsedChannel.name = name; + parsedChannel.spacer = 'none'; + } + + if (subchannels !== undefined) { + parsedChannel.subchannels = subchannels.map(parseChannel); + } + + parsedChannel.totalClients = totalClients; + + return parsedChannel; +}; + +const parseData = data => { + const { channelJson, clientJson } = data; + const clients = clientJson; + + const channels = channelJson.map(parseChannel); + + return { channels, clients }; +}; + +export default parseData;