added owner crown to chat

pull/3/head
Simon Huang 5 years ago
parent 0e6cefbda1
commit e7f1115b1d

@ -5,11 +5,12 @@ import './Chatbox.css';
import Messages from './Messages'; import Messages from './Messages';
type ChatboxProps = { type ChatboxProps = {
ownerId: string;
roomId: string; roomId: string;
userId: string; userId: string;
}; };
const Chat: React.FC<ChatboxProps> = ({ roomId, userId }) => { const Chat: React.FC<ChatboxProps> = ({ ownerId, roomId, userId }) => {
const [message, setMessage] = useState(''); // Message to be sent const [message, setMessage] = useState(''); // Message to be sent
// Send message to database // Send message to database
@ -35,7 +36,7 @@ const Chat: React.FC<ChatboxProps> = ({ roomId, userId }) => {
return ( return (
<IonCard class="chat-card"> <IonCard class="chat-card">
<Messages roomId={roomId} userId={userId}></Messages> <Messages ownerId={ownerId} roomId={roomId} userId={userId}></Messages>
<IonFooter> <IonFooter>
<IonRow> <IonRow>
<IonCol size="12" sizeSm="9" class="message-col"> <IonCol size="12" sizeSm="9" class="message-col">

@ -14,6 +14,7 @@ ion-col {
.message-grid { .message-grid {
margin-right: 0; margin-right: 0;
height: 100%; height: 100%;
user-select: auto;
} }
.right-align { .right-align {

@ -4,6 +4,7 @@ import { currTime, db } from '../services/firebase';
import './Messages.css'; import './Messages.css';
type MessagesProps = { type MessagesProps = {
ownerId: string;
roomId: string; roomId: string;
userId: string; userId: string;
}; };
@ -15,8 +16,7 @@ type Message = {
content: string; content: string;
}; };
const Messages: React.FC<MessagesProps> = ({ roomId, userId }) => { const Messages: React.FC<MessagesProps> = ({ ownerId, roomId, userId }) => {
const [room] = useState(roomId);
const [chats, setChats] = useState<Message[]>([ const [chats, setChats] = useState<Message[]>([
{ id: '', senderId: userId, sender: '', content: 'You have joined the room.' }, { id: '', senderId: userId, sender: '', content: 'You have joined the room.' },
]); // All received messages ]); // All received messages
@ -36,7 +36,7 @@ const Messages: React.FC<MessagesProps> = ({ roomId, userId }) => {
useEffect(() => { useEffect(() => {
const chatUnsubscribe = db const chatUnsubscribe = db
.collection('rooms') .collection('rooms')
.doc(room) .doc(roomId)
.collection('messages') .collection('messages')
.orderBy('createdAt') .orderBy('createdAt')
.where('createdAt', '>', currTime) .where('createdAt', '>', currTime)
@ -48,10 +48,15 @@ const Messages: React.FC<MessagesProps> = ({ roomId, userId }) => {
const data = change.doc.data(); const data = change.doc.data();
const user = await db.collection('users').doc(data.senderId).get(); const user = await db.collection('users').doc(data.senderId).get();
if (data.type !== 'updateState') { if (data.type !== 'updateState') {
let displayName = user.data()?.name;
if (data.senderId === ownerId) {
displayName = displayName + ' 👑';
}
newMsgs.push({ newMsgs.push({
id: change.doc.id, id: change.doc.id,
senderId: data.senderId, senderId: data.senderId,
sender: user.data()?.name, sender: displayName,
content: data.content, content: data.content,
}); });
} }
@ -66,11 +71,12 @@ const Messages: React.FC<MessagesProps> = ({ roomId, userId }) => {
return () => { return () => {
chatUnsubscribe(); chatUnsubscribe();
}; };
}, [room]); }, [roomId, ownerId]);
// Always scroll to most recent chat message (bottom) // Always scroll to most recent chat message (bottom)
useEffect(() => { useEffect(() => {
let content = contentRef.current; let content = contentRef.current;
console.log(content);
// Set timeout because DOM doesn't update immediately after 'chats' state is updated // Set timeout because DOM doesn't update immediately after 'chats' state is updated
setTimeout(() => { setTimeout(() => {

@ -154,7 +154,7 @@ const Room: React.FC<RouteComponentProps<{ roomId: string }>> = ({ match }) => {
<VideoPlayer ownerId={ownerId} userId={userId} roomId={roomId} stateId={stateId}></VideoPlayer> <VideoPlayer ownerId={ownerId} userId={userId} roomId={roomId} stateId={stateId}></VideoPlayer>
</IonCol> </IonCol>
<IonCol size="12" sizeLg="3" class="chat-col"> <IonCol size="12" sizeLg="3" class="chat-col">
<Chat roomId={roomId} userId={userId}></Chat> <Chat ownerId={ownerId} roomId={roomId} userId={userId}></Chat>
</IonCol> </IonCol>
</IonRow> </IonRow>
</IonGrid> </IonGrid>

Loading…
Cancel
Save