recursive channellist with clientlist
This commit is contained in:
parent
fba41717ad
commit
b81350fac1
7 changed files with 91 additions and 53 deletions
|
@ -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 {
|
||||
|
|
|
@ -1,21 +1,30 @@
|
|||
import React, { Component } from 'react';
|
||||
import ChannelList from './ChannelList';
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
class ChannelEntry extends Component<ChannelEntryProps> {
|
||||
render() {
|
||||
const { name, subchannels } = this.props.channel;
|
||||
return (
|
||||
<>
|
||||
<div>{name}</div>
|
||||
{subchannels !== undefined && (
|
||||
<div>{channel.name}</div>
|
||||
{joined.length > 0 && (
|
||||
<ul>
|
||||
{subchannels.map(e => {
|
||||
return <li key={e.id}>{e.name}</li>;
|
||||
})}
|
||||
{joined.map(e => (
|
||||
<li>{e.nickname}</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
{channel.subchannels !== undefined && (
|
||||
<ChannelList channels={channel.subchannels} clients={clients} />
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default ChannelEntry;
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
.channelList li {
|
||||
.channelList {
|
||||
list-style: none
|
||||
}
|
||||
|
|
|
@ -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: [],
|
||||
};
|
||||
}
|
||||
|
||||
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 {
|
||||
const ChannelList = ({ channels, clients }: ChannelListProps) => {
|
||||
return (
|
||||
<ul className='channelList'>
|
||||
{channels.map((e: Channel) => {
|
||||
{channels.map(channel => {
|
||||
return (
|
||||
<li key={e.id}>
|
||||
<ChannelEntry channel={e} />
|
||||
<li key={channel.id}>
|
||||
<ChannelEntry channel={channel} clients={clients} />
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default ChannelList;
|
||||
|
|
33
src/components/Channel/Channels.tsx
Normal file
33
src/components/Channel/Channels.tsx
Normal 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;
|
|
@ -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
15
src/interfaces.d.ts
vendored
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue