Chat.js 5.05 KB
Newer Older
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
1
import axios from 'axios';
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
2
import React, { useState, useEffect } from 'react';
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
3
4
5
import { Form, Button, Row, Image, Col, Container } from 'react-bootstrap';
import { isAuthenticated } from '../utils/auth';
import catchErrors from '../utils/catchErrors';
우지원's avatar
ul    
우지원 committed
6
7

function Chat(props) {
Choi Ga Young's avatar
Choi Ga Young committed
8
  // let defaultname = sessionStorage.getItem('name');
JeongYeonwoo's avatar
JeongYeonwoo committed
9

JeongYeonwoo's avatar
JeongYeonwoo committed
10
11
  const [sender, setSender] = useState([])

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
12
  const [inner, setInner] = useState([''])
Soo Hyun Kim's avatar
Soo Hyun Kim committed
13
  const [chat, setChat] = useState([])   //object로 key는 보낸사람 value는 메세지
JeongYeonwoo's avatar
JeongYeonwoo committed
14
15
16

  const [img, setImg] = useState([])

Choi Ga Young's avatar
Choi Ga Young committed
17
18
19
20
  const [disabled, setDisabled] = useState(true)
  const [user, setUser] = useState('')
  const [error, setError] = useState('');

Soo Hyun Kim's avatar
Soo Hyun Kim committed
21
  const [exit, setExit] = useState(false);
Choi Ga Young's avatar
Choi Ga Young committed
22
23

  const userId = isAuthenticated()
Soo Hyun Kim's avatar
Soo Hyun Kim committed
24

Choi Ga Young's avatar
Choi Ga Young committed
25
26
27
28
29
30
31
32
  async function getProfile(userId) {
    try {
      const response = await axios.get(`/users/${userId}`)
      setUser(response.data)
    } catch (error) {
      catchErrors(error, setError)
    }
  }
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
33

34
35
  function handleChange(e) {
    e.preventDefault()
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
36
    setInner(e.target.value)
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
37
    setDisabled(false)
38
39
40
  }

  function sendMsgCH(e) {
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
41
    e.preventDefault()
JeongYeonwoo's avatar
JeongYeonwoo committed
42

JeongYeonwoo's avatar
JeongYeonwoo committed
43
44
    props.setSingleImg(user.profileimg)
    props.setSingleUser(user.nickname)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
45
    props.setSingleChat(inner)
JeongYeonwoo's avatar
JeongYeonwoo committed
46

47
    props.sendMsg(e)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
48
    setInner('')
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
49
    setDisabled(true)
50
  }
Choi Ga Young's avatar
Choi Ga Young committed
51

Choi Ga Young's avatar
Choi Ga Young committed
52
  function handleClick() {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
53
54
55
    props.closeChatRoom(props.roomCode)
    props.setRecievedMsg('')
    console.log(chat)
Choi Ga Young's avatar
Choi Ga Young committed
56
57
58
    props.handleChatc()
  }

Soo Hyun Kim's avatar
Soo Hyun Kim committed
59
60
61
62
63
64
65
  async function exitAndCloseRoom(){
    props.exitRoom(props.roomCode)
    setExit(false)
    props.handleChatc()
  }
  

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
66
67
68
  useEffect(() => {
    getProfile(userId)
  }, [userId])
69

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
70
  useEffect(() => {
JeongYeonwoo's avatar
JeongYeonwoo committed
71
    setImg([...img, props.singleImg])
JeongYeonwoo's avatar
JeongYeonwoo committed
72
    setSender([...sender, props.singleUser])
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
73
    setChat([...chat, props.singleChat])
Soo Hyun Kim's avatar
Soo Hyun Kim committed
74
75
    console.log('chat', chat)
    console.log('sender', sender)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
76
  }, [props.singleChat])
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
77

Soo Hyun Kim's avatar
Soo Hyun Kim committed
78
  useEffect(() => {
JeongYeonwoo's avatar
JeongYeonwoo committed
79
    setImg([...img, props.recievedImg])
JeongYeonwoo's avatar
JeongYeonwoo committed
80
    setSender([...sender, props.recievedUser])
Soo Hyun Kim's avatar
Soo Hyun Kim committed
81
82
83
    setChat([...chat, props.recievedMsg])
  }, [props.recievedMsg])

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
84
  const time = new Date().toLocaleTimeString()
우지원's avatar
ul    
우지원 committed
85
  return (
JeongYeonwoo's avatar
JeongYeonwoo committed
86
87
    <>
      <Container id="chat" style={{ overflow: 'auto', padding: '20px', border: "2px solid", height: "500px", margin: "1%", borderColor: "#BDBDBD", background: '' }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
88
        <Row className="justify-content-center" style={{ border: "2px solid", borderWidth: "medium", borderColor: "#FFD75F", height: "80px" }}>
89
          <Col md="auto">
Choi Ga Young's avatar
dmdk    
Choi Ga Young committed
90
            <Button variant="light" onClick={handleClick} >{`<`}</Button>
91
92
          </Col>
          <Col>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
93
            <Row style={{ fontWeight: "bold", fontSize: "x-large" }}> {props.roomName} </Row>
94
95
            <Row > {props.roomCode} </Row>
          </Col>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
96
97
98
          <Col md="auto">
            <Button variant="light" onClick={() => setExit(true)}>{'='}</Button>
          </Col>
99
        </Row>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
100
101
102
103
104
105
        {exit ?
          <Row>
            <Col>퇴장하시겠습니까?</Col>
            <Col md="auto"><Button variant="light" onClick={exitAndCloseRoom}>{"나가기"}</Button></Col>
          </Row>
          : null}
106
        <p>{props.newUser}님이 입장하셨습니다.</p>
JeongYeonwoo's avatar
JeongYeonwoo committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        {chat.map((value, index) => {
          if (!(value == '')) {
            if (!(sender[index] === user.nickname)) {
              return (
                <Row key={index} className='m-1' >
                  <Col xs={2}>
                    <Image src={img && `/images/${img[index]}`} style={{ width: "50px", height: "50px" }} roundedCircle />
                  </Col>
                  <Col xs={8}>
                    <Row><strong>{sender[index]}</strong></Row>
                    <Row className='d-flex flex-wrap-nowrap'>
                      <Row className='border border-dark' style={{ width: 'max-content', maxWidth: '300px', height: 'auto', paddingLeft: '15px', paddingRight: '15px', background: 'white', borderRadius: '5px', fontSize: 'x-large' }}>{value}</Row>
                      <Col className='ml-1'>{time}</Col>
                    </Row>
                  </Col>
122
                </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
123
124
125
126
127
128
129
              )
            } else {
              return (
                <Row key={index} className='m-1 justify-content-end'>
                  <Row className='d-flex flex-wrap-nowrap' >
                    <Col className='mr-1'>{time}</Col>
                    <Row className='mr-2' style={{ width: 'max-content', maxWidth: '300px', height: 'auto', paddingLeft: '15px', paddingRight: '15px', background: 'yellow', borderRadius: '3px', fontSize: 'x-large' }}>{value}</Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
130
                  </Row>
131
                </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
132
133
134
135
              )
            }
          } else {
            return null
JeongYeonwoo's avatar
JeongYeonwoo committed
136
          }
JeongYeonwoo's avatar
JeongYeonwoo committed
137
        })
138
        }
Choi Ga Young's avatar
Choi Ga Young committed
139

JeongYeonwoo's avatar
JeongYeonwoo committed
140
      </Container >
141
142
      <Form onSubmit={sendMsgCH} fluid>
        <Form.Group className='d-flex flex-wrap-nowrap justify-content-center m-3'>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
143
144
145
          <Form.Control className='border border-warning' name='chat' type="text" value={inner} onChange={handleChange} style={{ width: '85%' }} />
          <Button variant="warning" type="submit" disabled={disabled} style={{ width: '10%' }}>
            전송
JeongYeonwoo's avatar
JeongYeonwoo committed
146
          </Button>
우지원's avatar
ul    
우지원 committed
147
        </Form.Group>
148
      </Form>
JeongYeonwoo's avatar
JeongYeonwoo committed
149
    </>
우지원's avatar
ul    
우지원 committed
150
151
152
153
154


  );
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
155

우지원's avatar
ul    
우지원 committed
156
export default Chat;
JeongYeonwoo's avatar
JeongYeonwoo committed
157