-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chat.js
195 lines (190 loc) · 5.39 KB
/
Chat.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React, { useEffect, useState } from "react";
import { Avatar, ClickAwayListener, IconButton } from "@material-ui/core";
import "./Chat.css";
import {
AttachFile,
InsertEmoticon,
Mic,
MoreVert,
SearchOutlined,
ArrowBack,
} from "@material-ui/icons";
import { Link, useParams } from "react-router-dom/cjs/react-router-dom.min";
import db from "../firebase";
import { useStateValue } from "../StateProvider";
import firebase from "firebase";
function Chat({ hide, removeRoom }) {
const [{ user }, dispatch] = useStateValue();
const [seed, setSeed] = useState("");
const [input, setInput] = useState("");
const { roomId } = useParams();
const [roomName, setRoomName] = useState("");
const [messages, setMessages] = useState([]);
const [showdropdown, setDropdown] = useState(false);
useEffect(() => {
setDropdown(false);
setSeed(Math.floor(Math.random() * 50000));
if (roomId) {
db.collection("Rooms")
.doc(roomId)
.onSnapshot((snapshot) => {
if (snapshot.data()) {
setRoomName(snapshot.data().name);
}
});
db.collection("Rooms")
.doc(roomId)
.collection("messages")
.orderBy("timestamp", "asc")
.onSnapshot((resultsnap) => {
setMessages(
resultsnap.docs.map((doc) => {
return doc.data();
})
);
});
}
}, [roomId]);
useEffect(() => {
setSeed(Math.floor(Math.random() * 50000));
}, []);
const sendMessage = (e) => {
e.preventDefault();
if (input) {
db.collection("Rooms").doc(roomId).collection("messages").add({
message: input,
name: user.displayName,
email: user.email,
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
});
setInput("");
} else {
alert("Type something first");
}
};
return (
<div className={hide ? "chat Chat" : "chat"}>
<div className="chat__header">
<Link to="/">
<IconButton>
<ArrowBack />
</IconButton>
</Link>
<Avatar src={`https://avatars.dicebear.com/api/human/${seed}.svg`} />
<div className="chat__headerInfo">
<h3>{roomName}</h3>
<p>
{messages.length !== 0
? `Last seen at ` +
new Date(
messages[messages.length - 1]?.timestamp?.toDate()
).toUTCString()
: ""}
</p>
</div>
<div className="chat__headerRight">
<IconButton
onClick={() =>
alert(
"Not added this functionality.\nClick on three dots to delete room."
)
}
>
<SearchOutlined />
</IconButton>
{/*<IconButton>
<AttachFile />s
</IconButton>*/}
<ClickAwayListener onClickAway={() => setDropdown(false)}>
<div className="dropdown">
<IconButton
onClick={() => {
setDropdown(!showdropdown);
}}
>
<MoreVert />
</IconButton>
<div
className={
showdropdown ? "dropdown__list" : "dropdown__list hide"
}
>
<ul>
<Link to="/">
<li
onClick={() => {
removeRoom(roomId);
}}
>
Delete Room
</li>
</Link>
</ul>
</div>
</div>
</ClickAwayListener>
</div>
</div>
<div className="chat__body">
{messages.map((message) => (
<div key={message.timestamp}>
<p
className={`chat__message ${
message.email === user.email && "chat__receiver"
}`}
>
<span className="chat__name">{message.name}</span>
{message.message}
<span className="chat__timestamp">
{new Date(message.timestamp?.toDate()).toUTCString()}
</span>
</p>
</div>
))}
</div>
<div className="chat__footer">
<IconButton
onClick={() =>
alert(
"Not added this functionality.\nClick on three dots on top right to delete room."
)
}
>
<InsertEmoticon />
</IconButton>
<IconButton
className="attach__file"
onClick={() =>
alert(
"Not added this functionality.\nClick on three dots on top right to delete room."
)
}
>
<AttachFile />
</IconButton>
<form>
<input
required={true}
type="text"
onChange={(e) => setInput(e.target.value)}
value={input}
placeholder="Type a message"
/>
<button type="submit" onClick={sendMessage}>
Send a message
</button>
</form>
<IconButton
onClick={() =>
alert(
"Not added this functionality.\nClick on three dots on top right to delete room."
)
}
>
<Mic />
</IconButton>
</div>
</div>
);
}
export default Chat;