Chat.js 4.88 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
Soo Hyun Kim committed
12
  const [roomName, setRoomName] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
13
  const [inner, setInner] = useState([''])
Soo Hyun Kim's avatar
Soo Hyun Kim committed
14
  const [chat, setChat] = useState([])   //object로 key는 보낸사람 value는 메세지
JeongYeonwoo's avatar
JeongYeonwoo committed
15
16
17

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

Choi Ga Young's avatar
Choi Ga Young committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  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
32

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

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

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

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

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

Choi Ga Young's avatar
Choi Ga Young committed
61
  function handleClick() {
Choi Ga Young's avatar
Choi Ga Young committed
62
63
64
65
    setChat([])
    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
74
    setChat([...chat, props.singleChat])
  }, [props.singleChat])
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
75

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

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

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

Choi Ga Young's avatar
Choi Ga Young committed
133
        <Button variant="light" onClick={handleClick} >{`<`}</Button>
JeongYeonwoo's avatar
JeongYeonwoo committed
134
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