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

Soo Hyun Kim's avatar
Soo Hyun Kim committed
11
  const [roomName, setRoomName] = 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는 메세지
Choi Ga Young's avatar
Choi Ga Young committed
14
15
16
17
18
19
20
21
22
23
24
25
26
  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
27

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

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

  function sendMsgCH(e) {
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
46
47
    e.preventDefault()
    props.setSingleChat(inner)
48
    props.sendMsg(e)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
49
    setInner('')
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
50
51
    setDisabled(true)
    console.log(chat)
52
  }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
53

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
54
55
56
  useEffect(() => {
    getProfile(userId)
  }, [userId])
57

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
58
59
  useEffect(() => {
    setChat([...chat, props.singleChat])
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
60
61
    console.log('UseEffect singlechat', chat)
    // check()
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
62
  }, [props.singleChat])
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
63

Soo Hyun Kim's avatar
Soo Hyun Kim committed
64
65
  useEffect(() => {
    setChat([...chat, props.recievedMsg])
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
66
    console.log('UseEffect recievechat', chat)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
67
68
  }, [props.recievedMsg])

Soo Hyun Kim's avatar
Soo Hyun Kim committed
69
70
71
72
  useEffect(() => {
    getRoomName(props.roomCode)
  }, [props.roomCode])

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
73
  const time = new Date().toLocaleTimeString()
우지원's avatar
ul    
우지원 committed
74
  return (
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
75
    <div className="chat" id="chat" style={{ border: "2px solid", height: "300%", margin: "1%", borderColor: "#BDBDBD", background: '' }}>
76
      <Row class="d-flex justify-content-center" style={{ border:"2px solid", borderWidth: "medium", borderColor: "#FFD75F", height: "80px", margin: "1%" }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
77
78
79
80
        <Col md="auto">
          <Button variant="light" onClick={props.handleChatc} >{`<`}</Button>
        </Col>
        <Col>
81
          <Row style={{ fontWeight: "bold", fontSize: "x-large" }}> {roomName} </Row>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
          <Row > {props.roomCode} </Row>
        </Col>
      </Row>
      {
        chat.map((value, index) => {
          if (!(value == '')) {
            return (
              <Row key={index} className='m-1 ml-3' >
                <Col xs={2}>
                  {user.profileimg ? <Image src={user.profileimg && `/images/${user.profileimg}`} style={{ width: "50px", height: "50px" }} roundedCircle /> : <Image src='https://www.flaticon.com/svg/vstatic/svg/149/149071.svg?token=exp=1610922596~hmac=f4b972b9db509d4e3cc2eb40543b0b0f' style={{ width: "50px", height: "50px" }} roundedCircle />}
                </Col>
                <Col xs={8}>
                  <Row><strong>{user.nickname} {index}</strong></Row>
                  <Row className='d-flex flex-wrap-nowrap'>
                    <Col className='border border-dark' style={{ width: 'max-content', maxWidth: '300px', height: 'auto', paddingLeft: '15px', paddingRight: '15px', background: 'white', borderRadius: '5px', fontSize: 'x-large' }}>{value}</Col>
                    <Col className='ml-4'>{new Date().toLocaleTimeString()}</Col>
                  </Row>
                </Col>
              </Row>
            )
          } else {
            return null
          }
        })
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
106
107
      }
      {/* 내가 보낸 메세지 띄우기 */}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
      {
        chat.map((value, index) => {
          if (!(value == '')) {
            return (
              <Row key={index} className='m-1 mr-4 justify-content-end'>
                <div className='d-flex flex-wrap-nowrap' >
                  <Row className='mr-4'>{time}</Row>
                  <Row style={{ width: 'max-content', maxWidth: '300px', height: 'auto', paddingLeft: '15px', paddingRight: '15px', background: 'yellow', borderRadius: '3px', fontSize: 'x-large' }}>{value}</Row>
                </div>
              </Row>
            )
          } else {
            return null
          }
        })
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
123
124
125
126
      }



127
128


Soo Hyun Kim's avatar
Soo Hyun Kim committed
129

130
      <Form onSubmit={sendMsgCH}>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
131
132
133
134
135
        <Form.Group className='d-flex flex-wrap-nowrap justify-content-center ml-2 mr-2'>
          <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%' }}>
            전송
          </Button>
우지원's avatar
ul    
우지원 committed
136
        </Form.Group>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
137

138
      </Form>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
139
    </div >
우지원's avatar
ul    
우지원 committed
140
141
142
143
144


  );
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
145

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