Chat.js 1.59 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
9
10
  const [inner, setInner] = useState([''])
  const [chat, setChat] = useState([inner])   //object로 key는 보낸사람 value는 메세지

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
17
    console.log(e.target.value)
  }

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

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

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

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


  );
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
61

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