Created
January 30, 2020 13:47
-
-
Save ayoisaiah/d794919894ddf8f645ed502bfd7b389e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/Admin.js | |
import React, { useState, useEffect } from "react"; | |
import { | |
Chat, | |
Channel, | |
ChannelHeader, | |
Window, | |
MessageList, | |
ChannelList, | |
MessageInput, | |
ChannelListMessenger, | |
MessageTeam | |
} from "stream-chat-react"; | |
import { StreamChat } from "stream-chat"; | |
import axios from "axios"; | |
import "stream-chat-react/dist/css/index.css"; | |
let chatClient; | |
function Admin() { | |
document.title = "Admin"; | |
const [channel, setChannel] = useState(null); | |
useEffect(() => { | |
const username = "admin"; | |
async function getToken() { | |
try { | |
const response = await axios.post("http://localhost:7000/join", { | |
username | |
}); | |
const token = response.data.token; | |
chatClient = new StreamChat(response.data.api_key); | |
chatClient.setUser( | |
{ | |
id: username, | |
name: "Admin" | |
}, | |
token | |
); | |
const channel = chatClient.channel("messaging", "livechat"); | |
await channel.watch(); | |
setChannel(channel); | |
} catch (err) { | |
console.log(err); | |
return; | |
} | |
} | |
getToken(); | |
}, []); | |
if (channel) { | |
return ( | |
<Chat client={chatClient} theme="messaging light"> | |
<ChannelList | |
options={{ | |
subscribe: true, | |
state: true | |
}} | |
filters={{ | |
type: "messaging" | |
}} | |
List={ChannelListMessenger} | |
/> | |
<Channel channel={channel}> | |
<Window> | |
<ChannelHeader /> | |
<MessageList Message={MessageTeam} /> | |
<MessageInput focus /> | |
</Window> | |
</Channel> | |
</Chat> | |
); | |
} | |
return <div></div>; | |
} | |
export default Admin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment