recursive channellist with clientlist

This commit is contained in:
Niel 2019-04-18 01:08:45 +02:00
parent fba41717ad
commit b81350fac1
7 changed files with 91 additions and 53 deletions

View file

@ -4,7 +4,6 @@ import { BrowserRouter as Router, Route } from 'react-router-dom';
import Header from './components/Header/Header';
import './App.scss';
import Channellist from './components/Channel/ChannelList';
import Home from './components/Home';
class App extends Component {

View file

@ -1,21 +1,30 @@
import React, { Component } from 'react';
import ChannelList from './ChannelList';
class ChannelEntry extends Component<ChannelEntryProps> {
render() {
const { name, subchannels } = this.props.channel;
return (
<>
<div>{name}</div>
{subchannels !== undefined && (
<ul>
{subchannels.map(e => {
return <li key={e.id}>{e.name}</li>;
})}
</ul>
)}
</>
);
const ChannelEntry = (props: ChannelEntryProps) => {
const { clients, channel } = props;
const joined: Client[] = [];
if (channel.totalClients > 0) {
clients.forEach(client => {
if (client.channelId === channel.id) joined.push(client);
});
}
}
return (
<>
<div>{channel.name}</div>
{joined.length > 0 && (
<ul>
{joined.map(e => (
<li>{e.nickname}</li>
))}
</ul>
)}
{channel.subchannels !== undefined && (
<ChannelList channels={channel.subchannels} clients={clients} />
)}
</>
);
};
export default ChannelEntry;

View file

@ -1,3 +1,3 @@
.channelList li {
.channelList {
list-style: none
}

View file

@ -1,39 +1,21 @@
import React, { Component } from 'react';
import ChannelService from '../../services/ChannelService';
import ApiService from '../../services/ApiService';
import ChannelEntry from './ChannelEntry';
import './ChannelList.scss';
export default class Channellist extends Component<any, any> {
constructor(props: any) {
super(props);
this.state = {
loaded: false,
channels: [],
};
}
const ChannelList = ({ channels, clients }: ChannelListProps) => {
return (
<ul className='channelList'>
{channels.map(channel => {
return (
<li key={channel.id}>
<ChannelEntry channel={channel} clients={clients} />
</li>
);
})}
</ul>
);
};
componentDidMount() {
ChannelService.fetchAllChannels().then(data => {
this.setState({ channels: data, loaded: true });
});
}
public render() {
const { loaded, channels } = this.state;
if (!loaded) {
return <div className='loading'>Loading...</div>;
} else {
return (
<ul className='channelList'>
{channels.map((e: Channel) => {
return (
<li key={e.id}>
<ChannelEntry channel={e} />
</li>
);
})}
</ul>
);
}
}
}
export default ChannelList;

View file

@ -0,0 +1,33 @@
import React, { Component } from 'react';
import ApiService from '../../services/ApiService';
import ChannelList from './ChannelList';
class Channels extends Component<any, any> {
constructor(props: any) {
super(props);
this.state = {
loaded: false,
channels: [],
users: [],
};
}
componentDidMount() {
ApiService.fetchAllChannels().then(data => {
this.setState({
channels: data.channelJson,
clients: data.clientJson,
loaded: true,
});
});
}
render() {
const { loaded, channels, clients } = this.state;
if (!loaded) return <div className='loading'>Loading...</div>;
else {
return <ChannelList channels={channels} clients={clients} />;
}
}
}
export default Channels;

View file

@ -1,11 +1,11 @@
import React, { Component } from 'react';
import Channellist from './Channel/ChannelList';
import Channels from './Channel/Channels';
export default class Home extends Component {
render() {
return (
<div>
<Channellist />
<Channels />
</div>
);
}

15
src/interfaces.d.ts vendored
View file

@ -6,8 +6,23 @@ interface Channel {
subchannels: Channel[];
}
interface Client {
databaseId: number;
channelId: number;
nickname: string;
type: number;
away: boolean;
awayMessage: string;
}
interface ChannelListProps {
channels: Channel[];
clients: Client[];
}
interface ChannelEntryProps {
channel: Channel;
clients: Client[];
}
interface ChannelViewProps {
channel: Channel;