HomePage.js 8.87 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';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
3
import styled from 'styled-components';
우지원's avatar
ul    
우지원 committed
4
5
import ClosedList from '../Components/ClosedList';
import OpenList from '../Components/OpenList';
Choi Ga Young's avatar
Choi Ga Young committed
6
import Menu from '../Components/Menu';
7
import { io } from "socket.io-client";   //모듈 가져오기
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
8
import Chat from "../Components/Chat";
Soo Hyun Kim's avatar
Soo Hyun Kim committed
9
10
import RoomMake from "../Components/RoomMake"
import EnterRoom from "../Components/EnterRoom"
우지원's avatar
우지원 committed
11
import axios from 'axios';
우지원's avatar
ul    
우지원 committed
12

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

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

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

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

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

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

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

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

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

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

  function closeChatRoom(rCode) {
    socket.emit('closeRoom', rCode)
    console.log(`${rCode}방 보기 중단`)
  }

  async function exitRoom(roomId) {
Choi Ga Young's avatar
퇴장    
Choi Ga Young committed
80
81
    const response = await axios.get('/users/check', { params: { '_id': userId } })
    const userNick = response.data.nickname;
Soo Hyun Kim's avatar
Soo Hyun Kim committed
82
    await axios.put('/room/deleteMem', { userId: userId, roomId: roomId })
Choi Ga Young's avatar
퇴장    
Choi Ga Young committed
83
    setSysmsg(`${userNick}님이 나갔습니다.`)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
84
85
86
87
    console.log(`${roomId}${userId} 탈퇴`)
    setRoomCode("")
  }

88
89
90
91
92
93
94
95
96
97
98
99
  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)
  }

100
101
102
103
104
  //오픈채팅방에서 참가하기
  async function openListroom(roomId) {
    console.log(roomId)
    const roomInf = await axios.get('/room/changeMem', { params: { 'roomId': roomId } })
    setRoom(roomInf.data[0])
105
    setOpen(true)
106
107
108
109
110
111
    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
112
113
    const response = await axios.get('/users/check', { params: { '_id': userId } })
    const userNick = response.data.nickname;
114
115
    if (tf.data) {
      alert('참가되었습니다.')
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
116
      setSysmsg(`${userNick}님이 들어왔습니다.`)
117
118
    } else {
      alert('이미 참가된 방입니다.')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
119
    }
120
121
122
123
124
  }

  function enterButton() {
    setOpen(false)
  }
Choi Ga Young's avatar
Choi Ga Young committed
125
126
127
128
  const sendMsg = (e) => {
    e.preventDefault()
  }

129
130
131
132
  useEffect(() => {
    getClosedList();
    getOpenList();
  }, [roomCode]);
Choi Ga Young's avatar
Choi Ga Young committed
133

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

Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
149
  useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
150
    if (!(sysmsg === '')) {
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
151
152
153
154
155
156
157
158
159
160
      socket.emit("chat", {
        roomInfo: roomCode,
        sendInfo: {
          msg: sysmsg,
          sender: "system",
        }
      })
      setSysmsg('')
    }
  }, [sysmsg])
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
161

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

  return (
    <>
      <Menu />
      <Row className="mr-0">
        <Col className="list" md={5}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
177
178
179
180
          {/* <Navbar>
            <Navbar.Toggle aria-controls="basic-navbar-nav" />
            <Navbar.Collapse>
              <Nav> */}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
181
182
183
184
185
186
187
188
189
190
          <Sdiv chat={chat}>
            <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example" >
              <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
                <ClosedList closedlist={closedlist} singleChat={singleChat} recievedMsg={recievedMsg} leaveInfo={leaveInfo} setLeaveInfo={setLeaveInfo} enterChatRoom={enterChatRoom} setRoomCode={setRoomCode} setRoomName={setRoomName} roomCode={roomCode} closeChatRoom={closeChatRoom} />
              </Tab>
              <Tab eventKey="open" title="공개방" onClick={handleChatc}>
                <OpenList openlist={openlist} enterChatRoom={enterChatRoom} openListroom={openListroom} setRoomCode={setRoomCode} setRoomName={setRoomName} roomCode={roomCode} closeChatRoom={closeChatRoom} />
              </Tab>
            </Tabs>
          </Sdiv>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
191
192
193
          {/* </Nav>
            </Navbar.Collapse>
          </Navbar> */}
JeongYeonwoo's avatar
JeongYeonwoo committed
194
        </Col>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
195
        <Col style={{ padding: "5px", marginLeft: "15px" }}>
196
197
198
          <>
            {(show || chat) ?
              null
199
              : <div style={{ position: "fixed", bottom: "20px", right: "30px" }}>
200
201
                <Button style={{ borderColor: "#9174ad", backgroundColor: "#9174ad", color: 'white' }} onClick={handleShowModal} size="lg" block>생성</Button>
                <Button style={{ borderColor: "#9174ad", backgroundColor: "#9174ad", color: 'white' }} onClick={handleShowEnter} size="lg" block>참가</Button>
202
203
204
              </div>
            }
            {chat ?
Choi Ga Young's avatar
Choi Ga Young committed
205
              <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} />
206
207
208
209
210
211
212
213
214
215
              : null}
            {open ?
              <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'>
216
217
                      <Button variant="outline" style={{ border: "3px solid", borderColor: "#b49dc9" }} size="sm" className="mr-4" onClick={enterButton}>뒤로가기</Button>
                      <Button variant="outline" style={{ border: "3px solid", borderColor: "#b49dc9" }} size="sm" className="ml-4" type='submit' onClick={attendListRoom}>참가</Button>
218
                    </Row>
219
                  </div>
220
221
222
223
                </div>
              </div>
              : null}
          </>
JeongYeonwoo's avatar
JeongYeonwoo committed
224
225
226
        </Col>
      </Row>
      <RoomMake showModal={showModal} handleCloseModal={handleCloseModal} />
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
227
      <EnterRoom showEnter={showEnter} enterChatRoom={enterChatRoom} handleCloseEnter={handleCloseEnter} handleChato={handleChato} setRoomCode={setRoomCode} setRoomName={setRoomName} sysmsg={sysmsg} setSysmsg={setSysmsg} />
JeongYeonwoo's avatar
JeongYeonwoo committed
228
229
    </>
  );
우지원's avatar
ul    
우지원 committed
230
231
}

Soo Hyun Kim's avatar
Soo Hyun Kim committed
232
233
234
235
236
237
238
239
const Sdiv = styled.div`
    @media screen and (max-width: 768px) {
     display: ${({ chat }) => {
     return chat === false ? 'block' : 'none'
    }}
   }
`

우지원's avatar
e    
우지원 committed
240
export default Home;