Last active
January 30, 2020 13:37
-
-
Save ayoisaiah/d2b5334a8169159380e1ca92012b6f0a 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/Customer.js | |
import React, { useState, useEffect } from "react"; | |
import { | |
Chat, | |
Channel, | |
Window, | |
TypingIndicator, | |
MessageList, | |
MessageCommerce, | |
MessageInput, | |
MessageInputFlat, | |
ChannelHeader | |
} from "stream-chat-react"; | |
import { StreamChat } from "stream-chat"; | |
import axios from "axios"; | |
import "stream-chat-react/dist/css/index.css"; | |
let chatClient; | |
function Customer() { | |
document.title = "Customer service"; | |
const [channel, setChannel] = useState(null); | |
useEffect(() => { | |
const username = "customer"; | |
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: "Customer" | |
}, | |
token | |
); | |
const channel = chatClient.channel("messaging", "livechat", { | |
name: "Customer support" | |
}); | |
await channel.watch(); | |
setChannel(channel); | |
} catch (err) { | |
console.log(err); | |
return; | |
} | |
} | |
getToken(); | |
}, []); | |
if (channel) { | |
return ( | |
<Chat client={chatClient} theme="commerce light"> | |
<Channel channel={channel}> | |
<Window> | |
<ChannelHeader /> | |
<MessageList | |
typingIndicator={TypingIndicator} | |
Message={MessageCommerce} | |
/> | |
<MessageInput Input={MessageInputFlat} focus /> | |
</Window> | |
</Channel> | |
</Chat> | |
); | |
} | |
return <div></div>; | |
} | |
export default Customer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment