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

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

우지원's avatar
우지원 committed
16
const INIT_ROOM = {
17
18
19
20
  roomName: '',
  interest: '',
  roomId: '',
  member: '',
우지원's avatar
우지원 committed
21
22
}

우지원's avatar
ul    
우지원 committed
23
function Home() {
24
  const userName = sessionStorage.getItem('name')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
25
  const userId = sessionStorage.getItem('userId');
26

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

JeongYeonwoo's avatar
JeongYeonwoo committed
34
35
36
37
  //소켓
  const [singleChat, setSingleChat] = useState('')
  const [recievedMsg, setRecievedMsg] = useState('')
  const [roomCode, setRoomCode] = useState('')
38
  const [newUser, setNewUser] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
39
  const [roomName, setRoomName] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
40

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

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

JeongYeonwoo's avatar
JeongYeonwoo committed
47
48
  const handleCloseModal = () => setShowModal(false);
  const handleShowModal = () => setShowModal(true);
49
50
  const handleCloseEnter = () => setShowEnter(false);
  const handleShowEnter = () => setShowEnter(true);
JeongYeonwoo's avatar
JeongYeonwoo committed
51
52
  const handleChato = () => setChat(true);
  const handleChatc = () => setChat(false);
Choi Ga Young's avatar
Choi Ga Young committed
53

Soo Hyun Kim's avatar
Soo Hyun Kim committed
54
55
56
57
58
59
60
61

  //SOCKET 방 enter, close, exit  event
  function enterChatRoom(rCode) {    //방 입장하기
    socket.emit('joinRoom', rCode)
    socket.emit('newUser', { rmIf: rCode, userInfo: userName })
    console.log(`joinRoom : ${rCode} 입장`)
  }

62
63
64
  socket.on("sendUser", (data) => {
    setNewUser(data)
  })
JeongYeonwoo's avatar
JeongYeonwoo committed
65

Soo Hyun Kim's avatar
Soo Hyun Kim committed
66
67
68
69
70
71
72
73
74
75
76
  function closeChatRoom(rCode) {
    socket.emit('closeRoom', rCode)
    console.log(`${rCode}방 보기 중단`)
  }

  async function exitRoom(roomId) {
    await axios.put('/room/deleteMem', { userId: userId, roomId: roomId })
    console.log(`${roomId}${userId} 탈퇴`)
    setRoomCode("")
  }

77
78
79
80
81
82
83
84
  //오픈채팅방에서 참가하기
  async function openListroom(roomId) {
    console.log(roomId)
    const roomInf = await axios.get('/room/changeMem', { params: { 'roomId': roomId } })
    console.log(roomInf)
    console.log(roomInf.data)
    console.log(roomInf.data[0])
    setRoom(roomInf.data[0])
85
    setOpen(true)
86
87
88
89
90
91
92
93
94
95
    setShow(false)
  }

  async function attendListRoom() {
    const roomId = room.roomId
    const tf = await axios.put('/room/changeMem', { userId: userId, roomId: roomId })
    if (tf.data) {
      alert('참가되었습니다.')
    } else {
      alert('이미 참가된 방입니다.')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
96
    }
97
98
99
100
101
  }

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


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


JeongYeonwoo's avatar
JeongYeonwoo committed
122
  useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
123
    socket.on("sendedMSG", (sendInfo) => {
JeongYeonwoo's avatar
JeongYeonwoo committed
124
125
126
      setRecievedImg(sendInfo.img)
      setRecievedUser(sendInfo.sender)
      setRecievedMsg(sendInfo.msg)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
127
      console.log(sendInfo.msg)
Choi Ga Young's avatar
Choi Ga Young committed
128
    })
JeongYeonwoo's avatar
JeongYeonwoo committed
129
130
131
132
133
134
135
  }, [])

  return (
    <>
      <Menu />
      <Row className="mr-0">
        <Col className="list" md={5}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
136
          <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example" >
JeongYeonwoo's avatar
JeongYeonwoo committed
137
            <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
Soo Hyun Kim's avatar
Soo Hyun Kim committed
138
              <ClosedList enterChatRoom={enterChatRoom} setRoomCode={setRoomCode} setRoomName={setRoomName} roomCode={roomCode} closeChatRoom={closeChatRoom}/>
JeongYeonwoo's avatar
JeongYeonwoo committed
139
            </Tab>
Choi Ga Young's avatar
dmdk    
Choi Ga Young committed
140
            <Tab eventKey="open" title="공개방" onClick={handleChatc}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
141
              <OpenList enterChatRoom={enterChatRoom} openListroom={openListroom} setRoomCode={setRoomCode} setRoomName={setRoomName} roomCode={roomCode} closeChatRoom={closeChatRoom}/>
JeongYeonwoo's avatar
JeongYeonwoo committed
142
143
144
145
            </Tab>
          </Tabs>
        </Col>
        <Col style={{ padding: "0" }}>
146
147
148
          <>
            {(show || chat) ?
              null
149
150
151
              : <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>
152
153
154
              </div>
            }
            {chat ?
Soo Hyun Kim's avatar
Soo Hyun Kim committed
155
              <Chat handleChatc={handleChatc} sendMsg={sendMsg} singleChat={singleChat} singleUser={singleUser} singleImg={singleImg} recievedMsg={recievedMsg} recievedUser={recievedUser} recievedImg={recievedImg} setSingleChat={setSingleChat} setRecievedMsg={setRecievedMsg} setSingleUser={setSingleUser} setSingleImg={setSingleImg} roomCode={roomCode} roomName={roomName} closeChatRoom={closeChatRoom} exitRoom={exitRoom}/>
156
157
158
159
160
161
162
163
164
165
166
167
168
              : 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'>
                      <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>
169
                  </div>
170
171
172
173
                </div>
              </div>
              : null}
          </>
JeongYeonwoo's avatar
JeongYeonwoo committed
174
175
176
        </Col>
      </Row>
      <RoomMake showModal={showModal} handleCloseModal={handleCloseModal} />
177
      <EnterRoom showEnter={showEnter} enterChatRoom={enterChatRoom} handleCloseEnter={handleCloseEnter} handleChato={handleChato} setRoomCode={setRoomCode} setRoomName={setRoomName} />
JeongYeonwoo's avatar
JeongYeonwoo committed
178
179
    </>
  );
우지원's avatar
ul    
우지원 committed
180
181
182
}

export default Home;
183