HomePage.js 8.03 KB
Newer Older
Soo Hyun Kim's avatar
Soo Hyun Kim committed
1
import React, { useState, useEffect } from 'react';
Choi Ga Young's avatar
x    
Choi Ga Young committed
2
import { Row, Col, Button, Tabs, Tab } from 'react-bootstrap';
우지원's avatar
ul    
우지원 committed
3
4
import ClosedList from '../Components/ClosedList';
import OpenList from '../Components/OpenList';
Choi Ga Young's avatar
Choi Ga Young committed
5
import Menu from '../Components/Menu';
6
import { io } from "socket.io-client";   //모듈 가져오기
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
7
import Chat from "../Components/Chat";
Soo Hyun Kim's avatar
Soo Hyun Kim committed
8
9
import RoomMake from "../Components/RoomMake"
import EnterRoom from "../Components/EnterRoom"
우지원's avatar
우지원 committed
10
import axios from 'axios';
우지원's avatar
ul    
우지원 committed
11

12
const socket = io();
우지원's avatar
ul    
우지원 committed
13

우지원's avatar
우지원 committed
14
const INIT_ROOM = {
15
16
17
18
  roomName: '',
  interest: '',
  roomId: '',
  member: '',
우지원's avatar
우지원 committed
19
}
20
21
22
23
24
25
26
const INIT_LIST = [{
  interest: '',
  isOpen: '',
  memeber: [],
  roomId: '',
  roomName: '',
}]
우지원's avatar
우지원 committed
27

우지원's avatar
ul    
우지원 committed
28
function Home() {
29
  const userName = sessionStorage.getItem('name')
Choi Ga Young's avatar
x    
Choi Ga Young committed
30
  const userId = sessionStorage.getItem('userId')
31

JeongYeonwoo's avatar
JeongYeonwoo committed
32
  const [showModal, setShowModal] = useState(false);
Choi Ga Young's avatar
dmdk    
Choi Ga Young committed
33
  const [showEnter, setShowEnter] = useState(false);
JeongYeonwoo's avatar
JeongYeonwoo committed
34
  const [chat, setChat] = useState(false);
35
36
  const [open, setOpen] = useState(false);
  const [room, setRoom] = useState(INIT_ROOM)
37
  const [show, setShow] = useState(false)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
38

JeongYeonwoo's avatar
JeongYeonwoo committed
39
40
41
42
  //소켓
  const [singleChat, setSingleChat] = useState('')
  const [recievedMsg, setRecievedMsg] = useState('')
  const [roomCode, setRoomCode] = useState('')
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
43
  const [sysmsg, setSysmsg] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
44
  const [roomName, setRoomName] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
45

JeongYeonwoo's avatar
JeongYeonwoo committed
46
47
  const [singleUser, setSingleUser] = useState('')
  const [recievedUser, setRecievedUser] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
48

JeongYeonwoo's avatar
JeongYeonwoo committed
49
50
51
  const [singleImg, setSingleImg] = useState('')
  const [recievedImg, setRecievedImg] = useState('')

52
53
  const [singleTime, setSingleTime] = useState('')
  const [recievedTime, setRecievedTime] = useState('')
54
  const [leaveInfo, setLeaveInfo] = useState([{ roomName: "", leaveTime: "" }])
55

JeongYeonwoo's avatar
JeongYeonwoo committed
56
57
  const handleCloseModal = () => setShowModal(false);
  const handleShowModal = () => setShowModal(true);
58
59
  const handleCloseEnter = () => setShowEnter(false);
  const handleShowEnter = () => setShowEnter(true);
JeongYeonwoo's avatar
JeongYeonwoo committed
60
61
  const handleChato = () => setChat(true);
  const handleChatc = () => setChat(false);
62
63
  const [closedlist, setClosedList] = useState(INIT_LIST);
  const [openlist, setOpenlist] = useState([])
64

65
  //SOCKET 관련 시작
Choi Ga Young's avatar
x    
Choi Ga Young committed
66
  async function enterChatRoom(rCode) {    //방 입장하기
67
68
69
    socket.emit('joinRoom', rCode)
    socket.emit('newUser', { rmIf: rCode, userInfo: userName })
    console.log(`joinRoom : ${rCode} 입장`)
70
    //여기서 채팅 불러와서 넘겨주던가 해야할거 같은데
71
  }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
72

Soo Hyun Kim's avatar
Soo Hyun Kim committed
73
74
75
76
  function closeChatRoom(rCode) {
    socket.emit('closeRoom', rCode)
    console.log(`${rCode}방 보기 중단`)
  }
Choi Ga Young's avatar
Choi Ga Young committed
77

Soo Hyun Kim's avatar
Soo Hyun Kim committed
78
79
80
81
82
  async function exitRoom(roomId) {
    await axios.put('/room/deleteMem', { userId: userId, roomId: roomId })
    console.log(`${roomId}${userId} 탈퇴`)
    setRoomCode("")
  }
JeongYeonwoo's avatar
JeongYeonwoo committed
83

84
85
86
87
88
89
90
91
92
93
94
95
  async function getClosedList() {
    const userid = sessionStorage.getItem('userId')
    let res = await axios.get('/room/closedlist', { params: { '_id': userid } })
    setClosedList(res.data)
  }

  async function getOpenList() {
    let res = await axios.get('/room/openlist')
    console.log('getOpenlist', res.data)
    setOpenlist(res.data)
  }

96
97
98
99
100
  //오픈채팅방에서 참가하기
  async function openListroom(roomId) {
    console.log(roomId)
    const roomInf = await axios.get('/room/changeMem', { params: { 'roomId': roomId } })
    setRoom(roomInf.data[0])
101
    setOpen(true)
102
103
104
105
106
107
    setShow(false)
  }

  async function attendListRoom() {
    const roomId = room.roomId
    const tf = await axios.put('/room/changeMem', { userId: userId, roomId: roomId })
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
108
109
    const response = await axios.get('/users/check', { params: { '_id': userId } })
    const userNick = response.data.nickname;
110
111
    if (tf.data) {
      alert('참가되었습니다.')
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
112
      setSysmsg(`${userNick}님이 들어왔습니다.`)
113
114
    } else {
      alert('이미 참가된 방입니다.')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
115
    }
116
117
118
119
120
  }

  function enterButton() {
    setOpen(false)
  }
Choi Ga Young's avatar
Choi Ga Young committed
121
122
123
124
  const sendMsg = (e) => {
    e.preventDefault()
  }

125
126
127
128
  useEffect(() => {
    getClosedList();
    getOpenList();
  }, [roomCode]);
Choi Ga Young's avatar
Choi Ga Young committed
129

JeongYeonwoo's avatar
JeongYeonwoo committed
130
  useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
131
    if (!(singleChat === "")) {
JeongYeonwoo's avatar
JeongYeonwoo committed
132
133
      socket.emit("chat", {
        roomInfo: roomCode,
134
135
136
        sendInfo: {
          msg: singleChat,
          sender: singleUser,
137
138
          img: singleImg,
          time: singleTime
139
        }
JeongYeonwoo's avatar
JeongYeonwoo committed
140
      })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
141
      setSingleChat("")
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
142
    }
JeongYeonwoo's avatar
JeongYeonwoo committed
143
  }, [singleChat])
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
144

Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
145
  useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
146
    if (!(sysmsg === '')) {
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
147
148
149
150
151
152
153
154
155
156
157
158
      socket.emit("chat", {
        roomInfo: roomCode,
        sendInfo: {
          msg: sysmsg,
          sender: "system",
          // time: singleTime
        }
      })
      setSysmsg('')
    }
  }, [sysmsg])

JeongYeonwoo's avatar
JeongYeonwoo committed
159
  useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
160
    socket.on("sendedMSG", (sendInfo) => {
161
      setRecievedTime(sendInfo.time)
JeongYeonwoo's avatar
JeongYeonwoo committed
162
163
164
      setRecievedImg(sendInfo.img)
      setRecievedUser(sendInfo.sender)
      setRecievedMsg(sendInfo.msg)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
165
      console.log(sendInfo.msg)
Choi Ga Young's avatar
Choi Ga Young committed
166
    })
JeongYeonwoo's avatar
JeongYeonwoo committed
167
168
169
170
171
172
173
  }, [])

  return (
    <>
      <Menu />
      <Row className="mr-0">
        <Col className="list" md={5}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
174
          <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example" >
JeongYeonwoo's avatar
JeongYeonwoo committed
175
            <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
176
              <ClosedList closedlist={closedlist} singleChat={singleChat} recievedMsg={recievedMsg} leaveInfo={leaveInfo} setLeaveInfo={setLeaveInfo} enterChatRoom={enterChatRoom} setRoomCode={setRoomCode} setRoomName={setRoomName} roomCode={roomCode} closeChatRoom={closeChatRoom} />
JeongYeonwoo's avatar
JeongYeonwoo committed
177
            </Tab>
Choi Ga Young's avatar
dmdk    
Choi Ga Young committed
178
            <Tab eventKey="open" title="공개방" onClick={handleChatc}>
179
              <OpenList openlist={openlist} enterChatRoom={enterChatRoom} openListroom={openListroom} setRoomCode={setRoomCode} setRoomName={setRoomName} roomCode={roomCode} closeChatRoom={closeChatRoom} />
JeongYeonwoo's avatar
JeongYeonwoo committed
180
181
182
183
            </Tab>
          </Tabs>
        </Col>
        <Col style={{ padding: "0" }}>
184
185
186
          <>
            {(show || chat) ?
              null
187
188
189
              : <div style={{ position: "fixed", bottom: "20px", right: "30px" }}>
                <Button variant="primary" onClick={handleShowModal} size="lg" block>생성</Button>
                <Button variant="secondary" onClick={handleShowEnter} size="lg" block>참가</Button>
190
191
192
              </div>
            }
            {chat ?
Choi Ga Young's avatar
Choi Ga Young committed
193
              <Chat handleChatc={handleChatc} leaveInfo={leaveInfo} setLeaveInfo={setLeaveInfo} sendMsg={sendMsg} singleChat={singleChat} singleUser={singleUser} singleImg={singleImg} singleTime={singleTime} recievedMsg={recievedMsg} recievedUser={recievedUser} recievedImg={recievedImg} recievedTime={recievedTime} setSingleChat={setSingleChat} setSingleUser={setSingleUser} setSingleImg={setSingleImg} setSingleTime={setSingleTime} setRecievedMsg={setRecievedMsg} roomCode={roomCode} roomName={roomName} closeChatRoom={closeChatRoom} exitRoom={exitRoom} />
194
              : null}
Choi Ga Young's avatar
Choi Ga Young committed
195
            {open ? 
196
197
198
199
200
201
202
203
204
205
206
              <div className="vh-90 flex-column align-items-center justify-content-center mt-2" variant="dark">
                <div className="d-flex justify-content-center">
                  <div className="mt-5 p-5 shadow w-75">
                    <h2 className="d-flex justify-content-center mb-3">현재 {room.roomName} 입니다.</h2>
                    <h5> 관심분야 : {room.interest}</h5>
                    <h5> 참여인원 : {room.member.length}</h5>
                    <h5 className="mb-3"> 방코드(방코드를 통해서도 참여할  있습니다.) : {room.roomId}</h5>
                    <Row className='justify-content-center'>
                      <Button variant="outline-warning" size="sm" className="mr-4" onClick={enterButton}>뒤로가기</Button>
                      <Button variant="outline-warning" size="sm" className="ml-4" type='submit' onClick={attendListRoom}>참가</Button>
                    </Row>
207
                  </div>
208
209
210
211
                </div>
              </div>
              : null}
          </>
JeongYeonwoo's avatar
JeongYeonwoo committed
212
213
214
        </Col>
      </Row>
      <RoomMake showModal={showModal} handleCloseModal={handleCloseModal} />
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
215
      <EnterRoom showEnter={showEnter} enterChatRoom={enterChatRoom} handleCloseEnter={handleCloseEnter} handleChato={handleChato} setRoomCode={setRoomCode} setRoomName={setRoomName} sysmsg={sysmsg} setSysmsg={setSysmsg} />
JeongYeonwoo's avatar
JeongYeonwoo committed
216
217
    </>
  );
우지원's avatar
ul    
우지원 committed
218
219
220
}

export default Home;
221