Showing
1 changed file
with
27 additions
and
7 deletions
| 1 | -import React, { useContext, useEffect, useState } from 'react'; | 1 | +import React, { useCallback, useContext, useEffect, useState } from 'react'; |
| 2 | import SocketContext from '../../contexts/SocketContext'; | 2 | import SocketContext from '../../contexts/SocketContext'; |
| 3 | import { MessageType, RawMessage } from '../common/types'; | 3 | import { MessageType, RawMessage } from '../common/types'; |
| 4 | +import { ChatLine } from './ChatLine'; | ||
| 4 | import { ChatData } from './types'; | 5 | import { ChatData } from './types'; |
| 5 | 6 | ||
| 6 | export const Chat: React.FC = () => { | 7 | export const Chat: React.FC = () => { |
| ... | @@ -18,13 +19,32 @@ export const Chat: React.FC = () => { | ... | @@ -18,13 +19,32 @@ export const Chat: React.FC = () => { |
| 18 | } | 19 | } |
| 19 | }, []); | 20 | }, []); |
| 20 | 21 | ||
| 22 | + const handleEnter = useCallback((e: React.KeyboardEvent) => { | ||
| 23 | + if (e.key === 'Enter') { | ||
| 24 | + setChatLines([...chatLines, { sender: 'me', message: input }]); | ||
| 25 | + | ||
| 26 | + const rawMessage: RawMessage = { | ||
| 27 | + type: MessageType.ROOM_CHAT, | ||
| 28 | + message: { message: input } | ||
| 29 | + } | ||
| 30 | + socket.emit('msg', rawMessage, () => {}); | ||
| 31 | + | ||
| 32 | + setInput(''); | ||
| 33 | + } | ||
| 34 | + }, [input]); | ||
| 35 | + | ||
| 21 | return ( | 36 | return ( |
| 22 | - <div className='w-2/12 h-60 rounded shadow'> | 37 | + <div className='w-5/12'> |
| 23 | - {chatLines.map((line) => (<div/>))} | 38 | + <div className='w-full h-80 rounded shadow flex flex-col items-center end'> |
| 24 | - <input className='w-5/6 px-3 py-2 bg-white | 39 | + {chatLines.map((line, i) => (<ChatLine key={16383+i} chatData={line}/>))} |
| 25 | - placeholder-gray-400 text-gray-700 text-sm | 40 | + </div> |
| 26 | - rounded shadow outline-none focus:outline-none' | 41 | + <input className='w-full px-3 py-2 bg-white |
| 27 | - onChange={e => setInput(e.target.value)}></input> | 42 | + placeholder-gray-400 text-gray-700 text-sm |
| 43 | + rounded shadow outline-none focus:outline-none' | ||
| 44 | + placeholder='Enter the answer' | ||
| 45 | + onChange={e => setInput(e.target.value)} | ||
| 46 | + value={input} | ||
| 47 | + onKeyPress={handleEnter}></input> | ||
| 28 | </div> | 48 | </div> |
| 29 | ); | 49 | ); |
| 30 | } | 50 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment