HomePage.js 3.47 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() {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
16
17
    const [showModal, setShowModal] = useState(false);
    const [showEnter, setEnter] = useState(false);
Soo Hyun Kim's avatar
Soo Hyun Kim committed
18
19
    const [chat, setChat] = useState(false);

Soo Hyun Kim's avatar
Soo Hyun Kim committed
20
    //소켓
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
21
    const [singleChat, setSingleChat] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
22
    const [recievedMsg, setRecievedMsg] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
23
    const [roomName, setRoomName] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
24
    const [roomCode, setRoomCode] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
25

Soo Hyun Kim's avatar
Soo Hyun Kim committed
26
27
28
29
    const handleCloseModal = () => setShowModal(false);
    const handleShowModal = () => setShowModal(true);
    const handleCloseEnter = () => setEnter(false);
    const handleShowEnter = () => setEnter(true);
Soo Hyun Kim's avatar
Soo Hyun Kim committed
30
31
32
    const handleChato = () => setChat(true);
    const handleChatc = () => setChat(false);

Choi Ga Young's avatar
Choi Ga Young committed
33

Soo Hyun Kim's avatar
Soo Hyun Kim committed
34

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
35
    //SOCKET 관련 시작
Soo Hyun Kim's avatar
Soo Hyun Kim committed
36
    function enterChatRoom(rName) {    //방 입장하기
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
37
38
39
40
41
42
43
44
45
        socket.emit('joinRoom', rName)
        console.log(`joinRoom : ${rName} 입장`)
    }

    const sendMsg = (e) => {
        e.preventDefault()
    }

    useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
46
47
48
49
50
51
52
        if (!(singleChat == '')) {
            socket.emit("chat", {
                roomName: roomCode,
                msg: singleChat
            })
            setSingleChat([''])
        }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
53
    }, [singleChat])
Choi Ga Young's avatar
에러    
Choi Ga Young committed
54
55


Soo Hyun Kim's avatar
Soo Hyun Kim committed
56
57
    useEffect(() => {
        socket.on("sendedMSG", (msg) => {
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
58
            console.log(msg)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
59
            setRecievedMsg(msg)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
60
        })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
61
    }, [])
우지원's avatar
e    
우지원 committed
62

Soo Hyun Kim's avatar
Soo Hyun Kim committed
63
64
65
66
67
68
69
    return (
        <>
            <Menu />
            <Row className="mr-0">
                <Col className="list" md={5}>
                    <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example">
                        <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
Soo Hyun Kim's avatar
Soo Hyun Kim committed
70
                            <ClosedList enterChatRoom={enterChatRoom} setRoomName={setRoomName}/>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
71
72
                        </Tab>
                        <Tab eventKey="open" title="공개방" >
Soo Hyun Kim's avatar
Soo Hyun Kim committed
73
                            <OpenList enterChatRoom={enterChatRoom} setRoomName={setRoomName}/>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
74
75
76
77
                        </Tab>
                    </Tabs>
                </Col>
                <Col style={{ padding: "0" }}>
Choi Ga Young's avatar
에러    
Choi Ga Young committed
78
                    {chat ? <Chat handleChatc={handleChatc} sendMsg={sendMsg} singleChat={singleChat} recievedMsg={recievedMsg} setSingleChat={setSingleChat} roomName={roomName} /> : null}
우지원's avatar
ul    
우지원 committed
79

Soo Hyun Kim's avatar
Soo Hyun Kim committed
80
                    <div style={{ position: "fixed", bottom: "20px", right: "30px" }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
81
                        <Button variant="primary" onClick={handleShowModal} size="lg" block>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
82
83
                            생성
                        </Button>
우지원's avatar
ul    
우지원 committed
84

Soo Hyun Kim's avatar
Soo Hyun Kim committed
85
                        <Button variant="secondary" onClick={handleShowEnter} size="lg" block>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
86
87
88
89
90
                            참가
                        </Button>
                    </div>
                </Col>
            </Row>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
91
            <RoomMake showModal={showModal} handleCloseModal={handleCloseModal} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
92
            <EnterRoom showEnter={showEnter} enterChatRoom={enterChatRoom} handleCloseEnter={handleCloseEnter} handleChato={handleChato} setRoomName={setRoomName}/>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
93
94
        </>
    );
우지원's avatar
ul    
우지원 committed
95
96
97
}

export default Home;
98