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 Header from './components/Header/Header';
|
||||||
|
|
||||||
import './App.scss';
|
import './App.scss';
|
||||||
import Channellist from './components/Channel/ChannelList';
|
|
||||||
import Home from './components/Home';
|
import Home from './components/Home';
|
||||||
|
|
||||||
class App extends Component {
|
class App extends Component {
|
||||||
|
|
|
@ -1,21 +1,30 @@
|
||||||
import React, { Component } from 'react';
|
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<div>{name}</div>
|
<div>{channel.name}</div>
|
||||||
{subchannels !== undefined && (
|
{joined.length > 0 && (
|
||||||
<ul>
|
<ul>
|
||||||
{subchannels.map(e => {
|
{joined.map(e => (
|
||||||
return <li key={e.id}>{e.name}</li>;
|
<li>{e.nickname}</li>
|
||||||
})}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
)}
|
)}
|
||||||
|
{channel.subchannels !== undefined && (
|
||||||
|
<ChannelList channels={channel.subchannels} clients={clients} />
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
export default ChannelEntry;
|
export default ChannelEntry;
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
.channelList li {
|
.channelList {
|
||||||
list-style: none
|
list-style: none
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,39 +1,21 @@
|
||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import ChannelService from '../../services/ChannelService';
|
import ApiService from '../../services/ApiService';
|
||||||
import ChannelEntry from './ChannelEntry';
|
import ChannelEntry from './ChannelEntry';
|
||||||
|
|
||||||
import './ChannelList.scss';
|
import './ChannelList.scss';
|
||||||
|
|
||||||
export default class Channellist extends Component<any, any> {
|
const ChannelList = ({ channels, clients }: ChannelListProps) => {
|
||||||
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 {
|
|
||||||
return (
|
return (
|
||||||
<ul className='channelList'>
|
<ul className='channelList'>
|
||||||
{channels.map((e: Channel) => {
|
{channels.map(channel => {
|
||||||
return (
|
return (
|
||||||
<li key={e.id}>
|
<li key={channel.id}>
|
||||||
<ChannelEntry channel={e} />
|
<ChannelEntry channel={channel} clients={clients} />
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</ul>
|
</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 React, { Component } from 'react';
|
||||||
import Channellist from './Channel/ChannelList';
|
import Channels from './Channel/Channels';
|
||||||
|
|
||||||
export default class Home extends Component {
|
export default class Home extends Component {
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Channellist />
|
<Channels />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
15
src/interfaces.d.ts
vendored
15
src/interfaces.d.ts
vendored
|
@ -6,8 +6,23 @@ interface Channel {
|
||||||
subchannels: 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 {
|
interface ChannelEntryProps {
|
||||||
channel: Channel;
|
channel: Channel;
|
||||||
|
clients: Client[];
|
||||||
}
|
}
|
||||||
interface ChannelViewProps {
|
interface ChannelViewProps {
|
||||||
channel: Channel;
|
channel: Channel;
|
||||||
|
|
Loading…
Reference in a new issue