Chat.js 3.94 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) {
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  const [user, setUser] = useState('')
  const [error, setError] = useState('')
  const [disabled, setDisabled] = useState(true)

  const userId = isAuthenticated()
  async function getProfile(userId) {
    try {
      const response = await axios.get(`/users/${userId}`)
      setUser(response.data)
    } catch (error) {
      catchErrors(error, setError)
    }
  }




JeongYeonwoo's avatar
merge    
JeongYeonwoo committed
26
27
  // let defaultname=''
  let defaultname = sessionStorage.getItem('name');
JeongYeonwoo's avatar
JeongYeonwoo committed
28

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
29
30
31
  const [inner, setInner] = useState([''])
  const [chat, setChat] = useState([inner])   //object로 key는 보낸사람 value는 메세지

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

  function sendMsgCH(e) {
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
40
41
    e.preventDefault()
    props.setSingleChat(inner)
42
    props.sendMsg(e)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
43
    setInner('')
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
44
45
    setDisabled(true)
    console.log(chat)
46
  }
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
47
48
49
  useEffect(() => {
    getProfile(userId)
  }, [userId])
50

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
51
52
  useEffect(() => {
    setChat([...chat, props.singleChat])
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
53
54
    console.log('UseEffect singlechat', chat)
    // check()
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
55
  }, [props.singleChat])
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
56

JeongYeonwoo's avatar
JeongYeonwoo committed
57
58
  useEffect(() => {
    setChat([...chat, props.recievedMsg])
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
59
    console.log('UseEffect recievechat', chat)
JeongYeonwoo's avatar
JeongYeonwoo committed
60
  }, [props.recievedMsg])
Choi Ga Young's avatar
aa    
Choi Ga Young committed
61

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
62
  const time = new Date().toLocaleTimeString()
우지원's avatar
ul    
우지원 committed
63
  return (
JeongYeonwoo's avatar
JeongYeonwoo committed
64

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
65
66
67
68
69
70
71
72
    <div className="chat" id="chat" style={{ border: "2px solid", height: "300%", margin: "1%", borderColor: "#BDBDBD", background: '' }}>
      <h2>현재 {props.roomName} 입니다.{chat.length}</h2>
      {/* 상대방이 보낸 메세지 띄우기 + 같은유저면 프로필 이미지는 생략(chat을 object로 보낸사람과 함께 보내서 구분하자) */}
      {      chat.map((value, index) => (
        <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>
JeongYeonwoo's avatar
ddd    
JeongYeonwoo committed
73
74
          <Col xs={8} className='justify-content-flex-start'>
            <Row>{user.nickname}</Row>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
75
            <Row className='d-flex flex-wrap-nowrap'>
JeongYeonwoo's avatar
ddd    
JeongYeonwoo committed
76
77
              <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 xs={5} className='ml-1'>{new Date().toLocaleTimeString()}</Col>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
78
79
80
81
82
83
            </Row>
          </Col>
        </Row>
      ))
      }
      {/* 내가 보낸 메세지 띄우기 */}
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
84
      {      chat.map((value, index) => (
JeongYeonwoo's avatar
ddd    
JeongYeonwoo committed
85
86
87
        <Row key={index} className='m-1 mr-5 justify-content-end'>
          <Row className='d-flex flex-wrap-nowrap' >
            <Col className='mr-1'>{time}</Col>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
88
            <Row style={{ width: 'max-content', maxWidth: '300px', height: 'auto', paddingLeft: '15px', paddingRight: '15px', background: 'yellow', borderRadius: '3px', fontSize: 'x-large' }}>{value}</Row>
JeongYeonwoo's avatar
ddd    
JeongYeonwoo committed
89
          </Row>
90
        </Row>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
91
92
93
94
95
96

      ))
      }



우지원's avatar
ul    
우지원 committed
97
      <Button variant="light" onClick={props.handleChatc} >{`<`}</Button>
98
      <Form onSubmit={sendMsgCH}>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
99
100
101
102
103
        <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
104
        </Form.Group>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
105

106
      </Form>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
107
    </div >
우지원's avatar
ul    
우지원 committed
108
109
110
111
112
113


  );
}

export default Chat;