Chat.js 1.56 KB
Newer Older
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
1
import React, { useState, useEffect } from 'react';
2
import { Form, Button, Row } from 'react-bootstrap';
우지원's avatar
ul    
우지원 committed
3
4
5


function Chat(props) {
Choi Ga Young's avatar
aa    
Choi Ga Young committed
6
  let defaultname = sessionStorage.getItem('name');
JeongYeonwoo's avatar
JeongYeonwoo committed
7

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
8
  const [inner, setInner] = useState([''])
Soo Hyun Kim's avatar
Soo Hyun Kim committed
9
  const [chat, setChat] = useState([])   //object로 key는 보낸사람 value는 메세지
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
10

11
12
  function handleChange(e) {
    e.preventDefault()
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
13
    setInner(e.target.value)
14
15
16
  }

  function sendMsgCH(e) {
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
17
18
    e.preventDefault()
    props.setSingleChat(inner)
19
    props.sendMsg(e)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
20
    setInner('')
21
22
  }

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
23
24
25
  useEffect(() => {
    setChat([...chat, props.singleChat])
  }, [props.singleChat])
JeongYeonwoo's avatar
JeongYeonwoo committed
26
  
Soo Hyun Kim's avatar
Soo Hyun Kim committed
27
28
29
30
  useEffect(() => {
    setChat([...chat, props.recievedMsg])
  }, [props.recievedMsg])

우지원's avatar
ul    
우지원 committed
31
  return (
JeongYeonwoo's avatar
JeongYeonwoo committed
32

우지원's avatar
ul    
우지원 committed
33
    <div className="chat" id="chat" style={{ border: "2px solid", height: "300%", margin: "1%", borderColor: "#BDBDBD" }}>
34
      <h2>현재 {props.roomName} 입니다.</h2>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
35
      {      chat.map((value, index) => {
Choi Ga Young's avatar
Choi Ga Young committed
36
        if (!(value=='')) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
37
38
39
40
41
42
43
44
          // console.log(`value=${value}=`)
          return <Row key={index} className='ml-3'>
            {props.roomName}에서 {defaultname}님이 보낸 메세지 : {value}
          </Row>
        } else {
          return null
        }
      })}
우지원's avatar
ul    
우지원 committed
45
      <Button variant="light" onClick={props.handleChatc} >{`<`}</Button>
46
      <Form onSubmit={sendMsgCH}>
우지원's avatar
ul    
우지원 committed
47
        <Form.Group>
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
48
          <Form.Control name='chat' type="text" value={inner} onChange={handleChange} />
우지원's avatar
ul    
우지원 committed
49
50
51
52
        </Form.Group>
        <Button variant="primary" type="submit">
          전송
        </Button>
53
      </Form>
우지원's avatar
ul    
우지원 committed
54
55
56
57
58
59
    </div>


  );
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
60

우지원's avatar
ul    
우지원 committed
61
export default Chat;