ClosedList.js 2.54 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import React, { useState, useEffect } from 'react'
Choi Ga Young's avatar
Choi Ga Young committed
2
import { Badge, ListGroup } from 'react-bootstrap';
Choi Ga Young's avatar
Choi Ga Young committed
3
import axios from 'axios'
Choi Ga Young's avatar
Choi Ga Young committed
4

JeongYeonwoo's avatar
JeongYeonwoo committed
5
6
7
8
9
10
11
const INIT_LIST = [{
  interest: '',
  isOpen: '',
  memeber: [],
  roomId: '',
  roomName: '',
}]
12
function ClosedList(props) {
13

JeongYeonwoo's avatar
JeongYeonwoo committed
14
  const [list, setList] = useState(INIT_LIST);
우지원's avatar
수정    
우지원 committed
15

Choi Ga Young's avatar
Choi Ga Young committed
16
17
  useEffect(() => {
    getClosedList();
Soo Hyun Kim's avatar
Soo Hyun Kim committed
18
  }, [props.roomCode]);
우지원's avatar
수정    
우지원 committed
19

20

Choi Ga Young's avatar
Choi Ga Young committed
21
  async function getClosedList() {
22
23
    const userid = sessionStorage.getItem('userId')
    let res = await axios.get('/room/closedlist', { params: { '_id': userid } })
Choi Ga Young's avatar
Choi Ga Young committed
24
25
    setList(res.data)
  }
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
26

JeongYeonwoo's avatar
JeongYeonwoo committed
27
28
  const realTime = new Date().toISOString()

Soo Hyun Kim's avatar
Soo Hyun Kim committed
29
  function enterChatRoomCH(e) {
30
    if (props.roomCode) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
31
32
      props.closeChatRoom(props.roomCode)
    }
33
    const roomCode = e.target.name
Soo Hyun Kim's avatar
Soo Hyun Kim committed
34
    const roomName = e.target.value
35
    props.enterChatRoom(roomCode)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
36
    props.setRoomCode(roomCode)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
37
    props.setRoomName(roomName)
38

39
    console.log('1111111111111111111', props.leaveInfo, typeof (props.leaveInfo))
40
  }
우지원's avatar
수정    
우지원 committed
41

JeongYeonwoo's avatar
JeongYeonwoo committed
42
43
44
45
46
47
48
  const [checknew, setChecknew] = useState([])
  const [unreadnumber, setUnreadnumber] = useState([''])
  async function dbChat() {
    for (let i = 0; i <= list.length - 1; i++) {
      const respond = await axios.get('/room/dbChat', { params: { 'roomId': list[i].roomId } })
      setChecknew(respond.data)
    }
49
  }
JeongYeonwoo's avatar
JeongYeonwoo committed
50
51
52
  useEffect(() => {
    dbChat()
  }, [props.singleChat, props.recievedMsg])
우지원's avatar
수정    
우지원 committed
53

JeongYeonwoo's avatar
JeongYeonwoo committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  useEffect(() => {
    // console.log('chat 클라이언트 변경!')
    unreadMessage()
  }, [checknew])

  async function unreadMessage() {
    //여기서 나간시간과 db의 메세지와 시간비교해서 개수를 count해주면 됨
    //물론 그 방 별로 찾아서 list를 맵써서 하던?
    // console.log('ddddddddd', list[0].roomId)

    setUnreadnumber([''])
    let arr = []
    for (let i = 0; i <= list.length - 1; i++) {
      const respond = await axios.get('/room/unreadMessage', { params: { 'leaveInfo': props.leaveInfo, 'roomId': list[i].roomId } })
      // setUnreadnumber([...unreadnumber, respond.data])
      arr = [...arr, respond.data]
    }
    setUnreadnumber(arr)
    // console.log('클라이언트 클로즈드 카운드', unreadnumber, list.length)

  }
75
76


Choi Ga Young's avatar
Choi Ga Young committed
77
78
  return (
    <div>
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
79
      {list.map((item, index) =>
Choi Ga Young's avatar
Choi Ga Young committed
80
        <ListGroup key={index}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
81
          <ListGroup.Item action onClick={enterChatRoomCH} name={item.roomId} value={item.roomName}>
Choi Ga Young's avatar
Choi Ga Young committed
82
            {item.roomName}
JeongYeonwoo's avatar
JeongYeonwoo committed
83
            {unreadnumber[index] ? <Badge className='ml-2' pill variant='danger'>{unreadnumber[index]}</Badge> : ''}
Choi Ga Young's avatar
Choi Ga Young committed
84
85
          </ListGroup.Item>
        </ListGroup>
86
87
88
  )
}
    </div >
Choi Ga Young's avatar
Choi Ga Young committed
89
90
91
  )
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
92
export default ClosedList