Chat.js 4.82 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) {
JeongYeonwoo's avatar
JeongYeonwoo committed
8
9
  const [sender, setSender] = useState([])

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

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

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

Choi Ga Young's avatar
Choi Ga Young committed
20

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
21
22
23
24
25
26
27
28
29
30
  const userId = isAuthenticated()
  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
Soo Hyun Kim committed
31
32
33
  async function getRoomName(roomCode) {
    try {
      let res = await axios.get('/room/getRoomName', { params: { 'roomCode': roomCode } })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
34
      console.log(res.data)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
35
36
37
38
39
40
      setRoomName(res.data)
    } catch (error) {
      catchErrors(error, setError)
    }
  }

41
42
  function handleChange(e) {
    e.preventDefault()
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
43
    setInner(e.target.value)
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
44
    setDisabled(false)
45
46
47
  }

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

JeongYeonwoo's avatar
JeongYeonwoo committed
50
51
    props.setSingleImg(user.profileimg)
    props.setSingleUser(user.nickname)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
52
    props.setSingleChat(inner)
JeongYeonwoo's avatar
JeongYeonwoo committed
53

54
    props.sendMsg(e)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
55
    setInner('')
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
56
    setDisabled(true)
57
  }
Choi Ga Young's avatar
Choi Ga Young committed
58

Choi Ga Young's avatar
Choi Ga Young committed
59
  function handleClick() {
Choi Ga Young's avatar
Choi Ga Young committed
60
61
62
63
    setChat([])
    props.handleChatc()
  }

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

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

JeongYeonwoo's avatar
JeongYeonwoo committed
74
  useEffect(() => {
JeongYeonwoo's avatar
JeongYeonwoo committed
75
    setImg([...img, props.recievedImg])
JeongYeonwoo's avatar
JeongYeonwoo committed
76
    setSender([...sender, props.recievedUser])
JeongYeonwoo's avatar
JeongYeonwoo committed
77
    setChat([...chat, props.recievedMsg])
JeongYeonwoo's avatar
JeongYeonwoo committed
78
  }, [props.recievedMsg])
Choi Ga Young's avatar
aa    
Choi Ga Young committed
79

Soo Hyun Kim's avatar
Soo Hyun Kim committed
80
81
82
83
  useEffect(() => {
    getRoomName(props.roomCode)
  }, [props.roomCode])

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: '' }}>
88
89
90
91
92
93
94
95
96
        <Row class="d-flex justify-content-center" style={{ border: "2px solid", borderWidth: "medium", borderColor: "#FFD75F", height: "80px", margin: "1%" }}>
          <Col md="auto">
            <Button variant="light" onClick={props.handleChatc} >{`<`}</Button>
          </Col>
          <Col>
            <Row style={{ fontWeight: "bold", fontSize: "x-large" }}> {roomName} </Row>
            <Row > {props.roomCode} </Row>
          </Col>
        </Row>
97
        <p>{props.newUser}님이 입장하셨습니다.</p>
JeongYeonwoo's avatar
JeongYeonwoo committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        {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>
                </Row>
              )
            } 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
121
                  </Row>
122
                </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
123
124
125
126
              )
            }
          } else {
            return null
JeongYeonwoo's avatar
JeongYeonwoo committed
127
          }
JeongYeonwoo's avatar
JeongYeonwoo committed
128
        })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
129
        }
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
130

Choi Ga Young's avatar
Choi Ga Young committed
131
        <Button variant="light" onClick={handleClick} >{`<`}</Button>
JeongYeonwoo's avatar
JeongYeonwoo committed
132
133

      </Container >
134
135
      <Form onSubmit={sendMsgCH} fluid>
        <Form.Group className='d-flex flex-wrap-nowrap justify-content-center m-3'>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
136
137
138
          <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
139
          </Button>
우지원's avatar
ul    
우지원 committed
140
        </Form.Group>
141
      </Form>
JeongYeonwoo's avatar
JeongYeonwoo committed
142
    </>
우지원's avatar
ul    
우지원 committed
143
144
145
146
147


  );
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
148

우지원's avatar
ul    
우지원 committed
149
export default Chat;
JeongYeonwoo's avatar
JeongYeonwoo committed
150