Chat.js 4.14 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
  const [disabled, setDisabled] = useState(true)
  const [user, setUser] = useState('')
  const [error, setError] = useState('');
Choi Ga Young's avatar
Choi Ga Young committed
20
  
Choi Ga Young's avatar
Choi Ga Young 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
soo0115    
Soo Hyun Kim committed
31

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

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

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

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

Choi Ga Young's avatar
Choi Ga Young committed
50
  function handleClick() {
Choi Ga Young's avatar
Choi Ga Young committed
51
52
53
54
    setChat([])
    props.handleChatc()
  }

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

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
59
  useEffect(() => {
JeongYeonwoo's avatar
JeongYeonwoo committed
60
    setImg([...img, props.singleImg])
JeongYeonwoo's avatar
JeongYeonwoo committed
61
    setSender([...sender, props.singleUser])
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
62
63
    setChat([...chat, props.singleChat])
  }, [props.singleChat])
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
64

Soo Hyun Kim's avatar
Soo Hyun Kim committed
65
  useEffect(() => {
JeongYeonwoo's avatar
JeongYeonwoo committed
66
    setImg([...img, props.recievedImg])
JeongYeonwoo's avatar
JeongYeonwoo committed
67
    setSender([...sender, props.recievedUser])
Soo Hyun Kim's avatar
Soo Hyun Kim committed
68
69
70
    setChat([...chat, props.recievedMsg])
  }, [props.recievedMsg])

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
71
  const time = new Date().toLocaleTimeString()
우지원's avatar
ul    
우지원 committed
72
  return (
JeongYeonwoo's avatar
JeongYeonwoo committed
73
74
75
    <>
      <Container id="chat" style={{ overflow: 'auto', padding: '20px', border: "2px solid", height: "500px", margin: "1%", borderColor: "#BDBDBD", background: '' }}>
        <h2>해당 방에 대한 참여코드는 {props.roomCode} 입니다.</h2>
76
        <p>{props.newUser}님이 입장하셨습니다.</p>
JeongYeonwoo's avatar
JeongYeonwoo committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
        {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>
92
                </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
93
94
95
96
97
98
99
              )
            } 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
100
                  </Row>
101
                </Row>
JeongYeonwoo's avatar
JeongYeonwoo committed
102
103
104
105
              )
            }
          } else {
            return null
JeongYeonwoo's avatar
JeongYeonwoo committed
106
          }
JeongYeonwoo's avatar
JeongYeonwoo committed
107
        })
108
        }
Choi Ga Young's avatar
Choi Ga Young committed
109

Choi Ga Young's avatar
Choi Ga Young committed
110
        <Button variant="light" onClick={handleClick} >{`<`}</Button>
JeongYeonwoo's avatar
JeongYeonwoo committed
111
112
113
114
115
116
117
118
119
120
121

      </Container >
        <Form onSubmit={sendMsgCH} fluid>
          <Form.Group className='d-flex flex-wrap-nowrap justify-content-center m-3'>
            <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>
          </Form.Group>
        </Form>
    </>
우지원's avatar
ul    
우지원 committed
122
123
124
125
126


  );
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
127

우지원's avatar
ul    
우지원 committed
128
export default Chat;
JeongYeonwoo's avatar
JeongYeonwoo committed
129