|
|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
import { IonButton, IonCard, IonCardContent, IonCol, IonRow, IonTextarea } from '@ionic/react';
|
|
|
|
|
import { IonButton, IonCard, IonCol, IonFooter, IonInput, IonRow } from '@ionic/react';
|
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
import { db, timestamp } from '../services/firebase';
|
|
|
|
|
import './Chatbox.css';
|
|
|
|
|
@ -14,40 +14,47 @@ const Chat: React.FC<ChatboxProps> = ({ roomId, userId }) => {
|
|
|
|
|
|
|
|
|
|
// Send message to database
|
|
|
|
|
const sendMessage = async () => {
|
|
|
|
|
await db.collection('rooms').doc(roomId).collection('messages').add({
|
|
|
|
|
createdAt: timestamp,
|
|
|
|
|
senderId: userId,
|
|
|
|
|
content: message,
|
|
|
|
|
type: 'user',
|
|
|
|
|
});
|
|
|
|
|
if (message !== '') {
|
|
|
|
|
await db.collection('rooms').doc(roomId).collection('messages').add({
|
|
|
|
|
createdAt: timestamp,
|
|
|
|
|
senderId: userId,
|
|
|
|
|
content: message,
|
|
|
|
|
type: 'user',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset textarea field
|
|
|
|
|
setMessage('');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onEnter = (e: React.KeyboardEvent<HTMLIonInputElement>) => {
|
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
|
sendMessage();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<IonCard class="chat-card">
|
|
|
|
|
<Messages roomId={roomId} userId={userId}></Messages>
|
|
|
|
|
<IonRow class="input-card-row">
|
|
|
|
|
<IonCol size="12" sizeLg="3" class="input-card-col">
|
|
|
|
|
<IonCardContent class="input-card-content">
|
|
|
|
|
<IonRow>
|
|
|
|
|
<IonCol size="9">
|
|
|
|
|
<IonTextarea
|
|
|
|
|
onIonChange={(e) => setMessage(e.detail.value!)}
|
|
|
|
|
value={message}
|
|
|
|
|
class="textarea"
|
|
|
|
|
></IonTextarea>
|
|
|
|
|
</IonCol>
|
|
|
|
|
<IonCol size="3" class="send-msg">
|
|
|
|
|
<IonButton expand="block" color="primary" onClick={sendMessage} class="send-button">
|
|
|
|
|
Send
|
|
|
|
|
</IonButton>
|
|
|
|
|
</IonCol>
|
|
|
|
|
</IonRow>
|
|
|
|
|
</IonCardContent>
|
|
|
|
|
</IonCol>
|
|
|
|
|
</IonRow>
|
|
|
|
|
<IonFooter>
|
|
|
|
|
<IonRow>
|
|
|
|
|
<IonCol size="12" sizeSm="9" class="message-col">
|
|
|
|
|
<IonInput
|
|
|
|
|
onIonChange={(e) => setMessage(e.detail.value!)}
|
|
|
|
|
onKeyDown={(e) => onEnter(e)}
|
|
|
|
|
value={message}
|
|
|
|
|
placeholder="Send message"
|
|
|
|
|
enterkeyhint="send"
|
|
|
|
|
class="message-input"
|
|
|
|
|
></IonInput>
|
|
|
|
|
</IonCol>
|
|
|
|
|
<IonCol size="3" class="send-msg">
|
|
|
|
|
<IonButton expand="block" color="primary" onClick={sendMessage} class="send-button">
|
|
|
|
|
Send
|
|
|
|
|
</IonButton>
|
|
|
|
|
</IonCol>
|
|
|
|
|
</IonRow>
|
|
|
|
|
</IonFooter>
|
|
|
|
|
</IonCard>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|