diff --git a/src/components/Room/Room.tsx b/src/components/Room/Room.tsx index db934a9..0ebe0a0 100644 --- a/src/components/Room/Room.tsx +++ b/src/components/Room/Room.tsx @@ -5,38 +5,46 @@ import FormControl from '@mui/material/FormControl' import Typography from '@mui/material/Typography' import TextField from '@mui/material/TextField' -import { usePeerRoom, usePeerRoomAction, PeerActions } from 'hooks/usePeerRoom' +import { usePeerRoom, usePeerRoomAction } from 'hooks/usePeerRoom' +import { PeerActions } from 'models/network' +import { UnsentMessage, ReceivedMessage } from 'models/chat' export function Room() { const { roomId = '' } = useParams() - const [message, setMessage] = useState('') + const [textMessage, setTextMessage] = useState('') + const [receivedMessages, setReceivedMessages] = useState( + [] + ) const peerRoom = usePeerRoom({ appId: `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`, roomId, }) - const [sendMessage, receiveMessage] = usePeerRoomAction( + const [sendMessage, receiveMessage] = usePeerRoomAction( peerRoom, PeerActions.MESSAGE ) const handleMessageChange = (event: React.ChangeEvent) => { const { value } = event.target - setMessage(value) + setTextMessage(value) } const handleMessageSubmit = ( event: React.SyntheticEvent ) => { event.preventDefault() - sendMessage(message) - setMessage('') + sendMessage({ text: textMessage, timeSent: Date.now() }) + setTextMessage('') } receiveMessage(message => { - console.log(message) + setReceivedMessages([ + ...receivedMessages, + { ...message, timeReceived: Date.now() }, + ]) }) return ( @@ -50,7 +58,7 @@ export function Room() { @@ -58,7 +66,7 @@ export function Room() {