HomePage.js 7.23 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
}

우지원's avatar
ul    
우지원 committed
21
function Home() {
22
  const userName = sessionStorage.getItem('name')
Choi Ga Young's avatar
x    
Choi Ga Young committed
23
  const userId = sessionStorage.getItem('userId')
24

JeongYeonwoo's avatar
JeongYeonwoo committed
25
  const [showModal, setShowModal] = useState(false);
Choi Ga Young's avatar
dmdk    
Choi Ga Young committed
26
  const [showEnter, setShowEnter] = useState(false);
JeongYeonwoo's avatar
JeongYeonwoo committed
27
  const [chat, setChat] = useState(false);
28
29
  const [open, setOpen] = useState(false);
  const [room, setRoom] = useState(INIT_ROOM)
30
  const [show, setShow] = useState(false)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
31

JeongYeonwoo's avatar
JeongYeonwoo committed
32
33
34
35
  //소켓
  const [singleChat, setSingleChat] = useState('')
  const [recievedMsg, setRecievedMsg] = useState('')
  const [roomCode, setRoomCode] = useState('')
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
36
  const [sysmsg, setSysmsg] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
37
  const [roomName, setRoomName] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
38

JeongYeonwoo's avatar
JeongYeonwoo committed
39
40
  const [singleUser, setSingleUser] = useState('')
  const [recievedUser, setRecievedUser] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
41

JeongYeonwoo's avatar
JeongYeonwoo committed
42
43
44
  const [singleImg, setSingleImg] = useState('')
  const [recievedImg, setRecievedImg] = useState('')

45
46
47
48
  const [singleTime, setSingleTime] = useState('')
  const [recievedTime, setRecievedTime] = useState('')
  const [leaveInfo, setLeaveInfo] = useState([''])

JeongYeonwoo's avatar
JeongYeonwoo committed
49
50
  const handleCloseModal = () => setShowModal(false);
  const handleShowModal = () => setShowModal(true);
51
52
  const handleCloseEnter = () => setShowEnter(false);
  const handleShowEnter = () => setShowEnter(true);
JeongYeonwoo's avatar
JeongYeonwoo committed
53
54
  const handleChato = () => setChat(true);
  const handleChatc = () => setChat(false);
55

56
  //SOCKET 관련 시작
Choi Ga Young's avatar
x    
Choi Ga Young committed
57
  async function enterChatRoom(rCode) {    //방 입장하기
58
59
60
    socket.emit('joinRoom', rCode)
    socket.emit('newUser', { rmIf: rCode, userInfo: userName })
    console.log(`joinRoom : ${rCode} 입장`)
61
    //여기서 채팅 불러와서 넘겨주던가 해야할거 같은데
62
  }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
63

Soo Hyun Kim's avatar
Soo Hyun Kim committed
64
65
66
67
  function closeChatRoom(rCode) {
    socket.emit('closeRoom', rCode)
    console.log(`${rCode}방 보기 중단`)
  }
Choi Ga Young's avatar
Choi Ga Young committed
68

Soo Hyun Kim's avatar
Soo Hyun Kim committed
69
70
71
72
73
  async function exitRoom(roomId) {
    await axios.put('/room/deleteMem', { userId: userId, roomId: roomId })
    console.log(`${roomId}${userId} 탈퇴`)
    setRoomCode("")
  }
JeongYeonwoo's avatar
JeongYeonwoo committed
74

75
76
77
78
79
  //오픈채팅방에서 참가하기
  async function openListroom(roomId) {
    console.log(roomId)
    const roomInf = await axios.get('/room/changeMem', { params: { 'roomId': roomId } })
    setRoom(roomInf.data[0])
80
    setOpen(true)
81
82
83
84
85
86
    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
87
88
    const response = await axios.get('/users/check', { params: { '_id': userId } })
    const userNick = response.data.nickname;
89
90
    if (tf.data) {
      alert('참가되었습니다.')
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
91
      setSysmsg(`${userNick}님이 들어왔습니다.`)
92
93
    } else {
      alert('이미 참가된 방입니다.')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
94
    }
95
96
97
98
99
  }

  function enterButton() {
    setOpen(false)
  }
Choi Ga Young's avatar
Choi Ga Young committed
100
101
102
103
  const sendMsg = (e) => {
    e.preventDefault()
  }

JeongYeonwoo's avatar
JeongYeonwoo committed
104
  useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
105
    if (!(singleChat == "")) {
JeongYeonwoo's avatar
JeongYeonwoo committed
106
107
      socket.emit("chat", {
        roomInfo: roomCode,
108
109
110
        sendInfo: {
          msg: singleChat,
          sender: singleUser,
111
112
          img: singleImg,
          time: singleTime
113
        }
JeongYeonwoo's avatar
JeongYeonwoo committed
114
      })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
115
      setSingleChat("")
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
116
    }
JeongYeonwoo's avatar
JeongYeonwoo committed
117
  }, [singleChat])
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
118

Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  useEffect(() => {
    if (!(sysmsg == '')) {
      socket.emit("chat", {
        roomInfo: roomCode,
        sendInfo: {
          msg: sysmsg,
          sender: "system",
          // time: singleTime
        }
      })
      setSysmsg('')
    }
  }, [sysmsg])

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
133

JeongYeonwoo's avatar
JeongYeonwoo committed
134
  useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
135
    socket.on("sendedMSG", (sendInfo) => {
136
      setRecievedTime(sendInfo.time)
JeongYeonwoo's avatar
JeongYeonwoo committed
137
138
139
      setRecievedImg(sendInfo.img)
      setRecievedUser(sendInfo.sender)
      setRecievedMsg(sendInfo.msg)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
140
      console.log(sendInfo.msg)
Choi Ga Young's avatar
Choi Ga Young committed
141
    })
JeongYeonwoo's avatar
JeongYeonwoo committed
142
143
144
145
146
147
148
  }, [])

  return (
    <>
      <Menu />
      <Row className="mr-0">
        <Col className="list" md={5}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
149
          <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example" >
JeongYeonwoo's avatar
JeongYeonwoo committed
150
            <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
Choi Ga Young's avatar
Choi Ga Young committed
151
              <ClosedList enterChatRoom={enterChatRoom} setRoomCode={setRoomCode} setRoomName={setRoomName} roomCode={roomCode} closeChatRoom={closeChatRoom} />
JeongYeonwoo's avatar
JeongYeonwoo committed
152
            </Tab>
Choi Ga Young's avatar
dmdk    
Choi Ga Young committed
153
            <Tab eventKey="open" title="공개방" onClick={handleChatc}>
Choi Ga Young's avatar
Choi Ga Young committed
154
              <OpenList enterChatRoom={enterChatRoom} openListroom={openListroom} setRoomCode={setRoomCode} setRoomName={setRoomName} roomCode={roomCode} closeChatRoom={closeChatRoom} />
JeongYeonwoo's avatar
JeongYeonwoo committed
155
156
157
158
            </Tab>
          </Tabs>
        </Col>
        <Col style={{ padding: "0" }}>
159
160
161
          <>
            {(show || chat) ?
              null
162
163
164
              : <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>
165
166
167
              </div>
            }
            {chat ?
Choi Ga Young's avatar
Choi Ga Young committed
168
              <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} />
169
              : null}
Choi Ga Young's avatar
Choi Ga Young committed
170
            {open ? 
171
172
173
174
175
176
177
178
179
180
181
              <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>
182
                  </div>
183
184
185
186
                </div>
              </div>
              : null}
          </>
JeongYeonwoo's avatar
JeongYeonwoo committed
187
188
189
        </Col>
      </Row>
      <RoomMake showModal={showModal} handleCloseModal={handleCloseModal} />
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
190
      <EnterRoom showEnter={showEnter} enterChatRoom={enterChatRoom} handleCloseEnter={handleCloseEnter} handleChato={handleChato} setRoomCode={setRoomCode} setRoomName={setRoomName} sysmsg={sysmsg} setSysmsg={setSysmsg} />
JeongYeonwoo's avatar
JeongYeonwoo committed
191
192
    </>
  );
우지원's avatar
ul    
우지원 committed
193
194
195
}

export default Home;
196