Chat.js 4.84 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

Choi Ga Young's avatar
Choi Ga Young committed
7

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

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

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

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

Choi Ga Young's avatar
Choi Ga Young committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  const [disabled, setDisabled] = useState(true)
  const [user, setUser] = useState('')
  const [error, setError] = useState('');


  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
soo0115    
Soo Hyun Kim committed
33

Soo Hyun Kim's avatar
Soo Hyun Kim committed
34
35
36
  async function getRoomName(roomCode) {
    try {
      let res = await axios.get('/room/getRoomName', { params: { 'roomCode': roomCode } })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
37
      console.log(res.data)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
38
39
40
41
42
43
      setRoomName(res.data)
    } catch (error) {
      catchErrors(error, setError)
    }
  }

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

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

JeongYeonwoo's avatar
JeongYeonwoo committed
53
54
    props.setSingleImg(user.profileimg)
    props.setSingleUser(user.nickname)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
55
    props.setSingleChat(inner)
JeongYeonwoo's avatar
JeongYeonwoo committed
56

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

Choi Ga Young's avatar
Choi Ga Young committed
62
  function handleClick() {
Choi Ga Young's avatar
Choi Ga Young committed
63
    props.setRecievedMsg('')
Choi Ga Young's avatar
Choi Ga Young committed
64
65
66
67
    setChat([])
    props.handleChatc()
  }

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

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
72
  useEffect(() => {
JeongYeonwoo's avatar
JeongYeonwoo committed
73
    setImg([...img, props.singleImg])
JeongYeonwoo's avatar
JeongYeonwoo committed
74
    setSender([...sender, props.singleUser])
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
75
76
    setChat([...chat, props.singleChat])
  }, [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])

Soo Hyun Kim's avatar
Soo Hyun Kim committed
84
85
86
87
  useEffect(() => {
    getRoomName(props.roomCode)
  }, [props.roomCode])

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
88
  const time = new Date().toLocaleTimeString()
우지원's avatar
ul    
우지원 committed
89
  return (
JeongYeonwoo's avatar
JeongYeonwoo committed
90
91
    <>
      <Container id="chat" style={{ overflow: 'auto', padding: '20px', border: "2px solid", height: "500px", margin: "1%", borderColor: "#BDBDBD", background: '' }}>
92
        <Row className="d-flex justify-content-center" style={{ border: "2px solid", borderWidth: "medium", borderColor: "#FFD75F", height: "80px", margin: "1%" }}>
93
          <Col md="auto">
Choi Ga Young's avatar
dmdk    
Choi Ga Young committed
94
            <Button variant="light" onClick={handleClick} >{`<`}</Button>
95
96
97
98
99
100
          </Col>
          <Col>
            <Row style={{ fontWeight: "bold", fontSize: "x-large" }}> {roomName} </Row>
            <Row > {props.roomCode} </Row>
          </Col>
        </Row>
101
        <p>{props.newUser}님이 입장하셨습니다.</p>
JeongYeonwoo's avatar
JeongYeonwoo committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        {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>
117
                </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
118
119
120
121
122
123
124
              )
            } 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
125
                  </Row>
126
                </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
127
128
129
130
              )
            }
          } else {
            return null
JeongYeonwoo's avatar
JeongYeonwoo committed
131
          }
JeongYeonwoo's avatar
JeongYeonwoo committed
132
        })
133
        }
Choi Ga Young's avatar
Choi Ga Young committed
134

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


  );
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
150

우지원's avatar
ul    
우지원 committed
151
export default Chat;
JeongYeonwoo's avatar
JeongYeonwoo committed
152