-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSidebarChat.js
51 lines (43 loc) · 1.51 KB
/
SidebarChat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React, {useEffect, useState} from 'react';
import {Avatar} from "@material-ui/core";
import './SidebarChat.css';
import db from './firebase';
import {Link} from 'react-router-dom';
function SidebarChat({id,name,addNewChat}) {
const [seed, setSeed] = useState("");
const [messages, setMessages] = useState("");
useEffect(() => {
if(id){
db.collection('rooms').doc(id).collection('messages').orderBy('timestamp','desc').onSnapshot(snapshot => {
setMessages(snapshot.docs.map((doc) => doc.data()))
})
}
}, [id]);
useEffect(() => {
setSeed(Math.floor(Math.random() * 5000));
}, []);
const createChat = () => {
const roomName = prompt("Please Enter Name for Chat");
if(roomName){
db.collection("rooms").add({
name: roomName
})
}
};
return !addNewChat ? (
<Link to={`/rooms/${id}`} key={id}>
<div className="sidebarChat">
<Avatar src={`https://avatars.dicebear.com/api/human/${seed}.svg`}/>
<div className="sidebarChat_info">
<h2>{name}</h2>
<p>{messages[0]?.message}</p>
</div>
</div>
</Link>
) : (
<div onClick={createChat} className="sidebarChat">
<h3 className="add-new-chat-title">Add New Chat</h3>
</div>
)
}
export default SidebarChat