HomePage.js 6.92 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
soo0115    
Soo Hyun Kim committed
37

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

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

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

48

49
  //SOCKET 관련 시작
Choi Ga Young's avatar
x    
Choi Ga Young committed
50
  async function enterChatRoom(rCode) {    //방 입장하기
51
52
53
    socket.emit('joinRoom', rCode)
    socket.emit('newUser', { rmIf: rCode, userInfo: userName })
    console.log(`joinRoom : ${rCode} 입장`)
54
    //여기서 채팅 불러와서 넘겨주던가 해야할거 같은데
55
  }
JeongYeonwoo's avatar
JeongYeonwoo committed
56
  const [roomName, setRoomName] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
57

JeongYeonwoo's avatar
JeongYeonwoo committed
58
59
  const handleCloseModal = () => setShowModal(false);
  const handleShowModal = () => setShowModal(true);
60
61
  const handleCloseEnter = () => setShowEnter(false);
  const handleShowEnter = () => setShowEnter(true);
JeongYeonwoo's avatar
JeongYeonwoo committed
62
63
  const handleChato = () => setChat(true);
  const handleChatc = () => setChat(false);
Choi Ga Young's avatar
Choi Ga Young committed
64

JeongYeonwoo's avatar
JeongYeonwoo committed
65

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

  function enterButton() {
    setOpen(false)
  }
Choi Ga Young's avatar
Choi Ga Young committed
94
95
96
97
98
  const sendMsg = (e) => {
    e.preventDefault()
  }


JeongYeonwoo's avatar
JeongYeonwoo committed
99
100
101
102
  useEffect(() => {
    if (!(singleChat == '')) {
      socket.emit("chat", {
        roomInfo: roomCode,
103
104
105
        sendInfo: {
          msg: singleChat,
          sender: singleUser,
106
107
          img: singleImg,
          time: singleTime
108
        }
JeongYeonwoo's avatar
JeongYeonwoo committed
109
      })
110
      setSingleChat([''])
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
111
    }
JeongYeonwoo's avatar
JeongYeonwoo committed
112
  }, [singleChat])
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
113

Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  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
128

JeongYeonwoo's avatar
JeongYeonwoo committed
129
  useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
130
    socket.on("sendedMSG", (sendInfo) => {
131
      setRecievedTime(sendInfo.time)
JeongYeonwoo's avatar
JeongYeonwoo committed
132
133
134
      setRecievedImg(sendInfo.img)
      setRecievedUser(sendInfo.sender)
      setRecievedMsg(sendInfo.msg)
Choi Ga Young's avatar
Choi Ga Young committed
135
    })
JeongYeonwoo's avatar
JeongYeonwoo committed
136
137
138
139
140
141
142
143
144
  }, [])

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

export default Home;
190