HomePage.js 3.8 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
ul    
우지원 committed
12

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

function Home() {
16
17
  const userName = sessionStorage.getItem('name')

JeongYeonwoo's avatar
JeongYeonwoo committed
18
19
20
  const [showModal, setShowModal] = useState(false);
  const [showEnter, setEnter] = useState(false);
  const [chat, setChat] = useState(false);
Soo Hyun Kim's avatar
Soo Hyun Kim committed
21

JeongYeonwoo's avatar
JeongYeonwoo committed
22
23
24
25
  //소켓
  const [singleChat, setSingleChat] = useState('')
  const [recievedMsg, setRecievedMsg] = useState('')
  const [roomCode, setRoomCode] = useState('')
26
  const [newUser, setNewUser] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
27

JeongYeonwoo's avatar
JeongYeonwoo committed
28
29
  const [singleUser, setSingleUser] = useState('')
  const [recievedUser, setRecievedUser] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
30

JeongYeonwoo's avatar
JeongYeonwoo committed
31
32
33
  const [singleImg, setSingleImg] = useState('')
  const [recievedImg, setRecievedImg] = useState('')

34

35
36
37
38
39
40
  //SOCKET 관련 시작
  function enterChatRoom(rCode) {    //방 입장하기
    socket.emit('joinRoom', rCode)
    socket.emit('newUser', { rmIf: rCode, userInfo: userName })
    console.log(`joinRoom : ${rCode} 입장`)
  }
JeongYeonwoo's avatar
JeongYeonwoo committed
41
  const [roomName, setRoomName] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
42

JeongYeonwoo's avatar
JeongYeonwoo committed
43
44
45
46
47
48
  const handleCloseModal = () => setShowModal(false);
  const handleShowModal = () => setShowModal(true);
  const handleCloseEnter = () => setEnter(false);
  const handleShowEnter = () => setEnter(true);
  const handleChato = () => setChat(true);
  const handleChatc = () => setChat(false);
Choi Ga Young's avatar
Choi Ga Young committed
49

50
51
52
  socket.on("sendUser", (data) => {
    setNewUser(data)
  })
JeongYeonwoo's avatar
JeongYeonwoo committed
53
54
55
56
57

  useEffect(() => {
    if (!(singleChat == '')) {
      socket.emit("chat", {
        roomInfo: roomCode,
58
59
60
61
62
        sendInfo: {
          msg: singleChat,
          sender: singleUser,
          img: singleImg
        }
JeongYeonwoo's avatar
JeongYeonwoo committed
63
      })
64
      setSingleChat([''])
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
65
    }
JeongYeonwoo's avatar
JeongYeonwoo committed
66
  }, [singleChat])
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
67
68


JeongYeonwoo's avatar
JeongYeonwoo committed
69
  useEffect(() => {
70
    socket.on("sendedMSG", (msg) => {
JeongYeonwoo's avatar
JeongYeonwoo committed
71
72
73
      setRecievedImg(sendInfo.img)
      setRecievedUser(sendInfo.sender)
      setRecievedMsg(sendInfo.msg)
Choi Ga Young's avatar
Choi Ga Young committed
74
    })
JeongYeonwoo's avatar
JeongYeonwoo committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  }, [])

  return (
    <>
      <Menu />
      <Row className="mr-0">
        <Col className="list" md={5}>
          <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example">
            <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
              <ClosedList enterChatRoom={enterChatRoom} setRoomCode={setRoomCode} />
            </Tab>
            <Tab eventKey="open" title="공개방" onClick={handleChato}>
              <OpenList enterChatRoom={enterChatRoom} setRoomCode={setRoomCode} />
            </Tab>
          </Tabs>
        </Col>
        <Col style={{ padding: "0" }}>

93
          {chat ? <Chat handleChatc={handleChatc} sendMsg={sendMsg} singleChat={singleChat} recievedMsg={recievedMsg} newUser={newUser} setSingleChat={setSingleChat} roomCode={roomCode} singleImg={singleImg} setSingleImg={setSingleImg} recievedImg={recievedImg} singleUser={singleUser} setSingleUser={setSingleUser} recievedUser={recievedUser} /> : <div style={{ position: "fixed", bottom: "20px", right: "30px" }}>
JeongYeonwoo's avatar
JeongYeonwoo committed
94
95
            <Button variant="primary" onClick={handleShowModal} size="lg" block>
              생성
Soo Hyun Kim's avatar
Soo Hyun Kim committed
96
                        </Button>
우지원's avatar
ul    
우지원 committed
97

JeongYeonwoo's avatar
JeongYeonwoo committed
98
99
            <Button variant="secondary" onClick={handleShowEnter} size="lg" block>
              참가
Soo Hyun Kim's avatar
Soo Hyun Kim committed
100
                        </Button>
101
          </div>}
JeongYeonwoo's avatar
JeongYeonwoo committed
102
103
104
105
106
107
        </Col>
      </Row>
      <RoomMake showModal={showModal} handleCloseModal={handleCloseModal} />
      <EnterRoom showEnter={showEnter} enterChatRoom={enterChatRoom} handleCloseEnter={handleCloseEnter} handleChato={handleChato} setRoomName={setRoomName} />
    </>
  );
우지원's avatar
ul    
우지원 committed
108
109
110
}

export default Home;
111