Chat.js 4.27 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
26
  const [user, setUser] = useState('')
  const [error, setError] = useState('')
  const [disabled, setDisabled] = useState(true)
  const [hidden, setHidden] = useState(false)

  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
27
28
  // let defaultname=''
  let defaultname = sessionStorage.getItem('name');
JeongYeonwoo's avatar
JeongYeonwoo committed
29

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

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

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

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

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

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
63
64
65
66
67
68
69
70
71
72
73

  // function check() {
  //   // if (chat[chat.length-1].user ===chat[chat.length-2].user){  //마지막보낸거랑 그 전꺼랑 보낸사람이 같은지 비교
  //   if (chat.length === 2) {
  //     setHidden(false)
  //   } else {
  //     setHidden(true)
  //   }
  //   console.log(hidden)
  // }
  const time = new Date().toLocaleTimeString()
우지원's avatar
ul    
우지원 committed
74
  return (
JeongYeonwoo's avatar
JeongYeonwoo committed
75

JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    <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>
          <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>
      ))
      }
      {/* 내가 보낸 메세지 띄우기 */}
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
95
      {      chat.map((value, index) => (
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
96
97
98
99
100
        <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>
101
        </Row>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
102
103
104
105
106
107

      ))
      }



우지원's avatar
ul    
우지원 committed
108
      <Button variant="light" onClick={props.handleChatc} >{`<`}</Button>
109
      <Form onSubmit={sendMsgCH}>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
110
111
112
113
114
        <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
115
        </Form.Group>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
116

117
      </Form>
JeongYeonwoo's avatar
yeonwoo    
JeongYeonwoo committed
118
    </div >
우지원's avatar
ul    
우지원 committed
119
120
121
122
123
124


  );
}

export default Chat;