HomePage.js 3.77 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() {
Choi Ga Young's avatar
Choi Ga Young committed
16
17
    const userName = sessionStorage.getItem('name')

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

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

Soo Hyun Kim's avatar
Soo Hyun Kim committed
28
    //방참가
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
29

30
31
    const [roomName, setRoomName] = useState('')

Soo Hyun Kim's avatar
Soo Hyun Kim committed
32
33
34
35
    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
36
37
38
    const handleChato = () => setChat(true);
    const handleChatc = () => setChat(false);

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
39
    //SOCKET 관련 시작
Choi Ga Young's avatar
Choi Ga Young committed
40
41
    function enterChatRoom(rCode) {    //방 입장하기
        socket.emit('joinRoom', rCode)
Choi Ga Young's avatar
Choi Ga Young committed
42
        socket.emit('newUser', { rmIf: rCode, userInfo: userName })
Choi Ga Young's avatar
Choi Ga Young committed
43
        console.log(`joinRoom : ${rCode} 입장`)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
44
45
46
47
48
49
    }

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

Choi Ga Young's avatar
Choi Ga Young committed
50
51
52
53
    socket.on("sendUser", (data) => {
        setNewUser(data)
    })

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
54
    useEffect(() => {
Choi Ga Young's avatar
Choi Ga Young committed
55
56
        if (!(singleChat == '')) {
            socket.emit("chat", {
57
                roomInfo: roomCode,
Choi Ga Young's avatar
Choi Ga Young committed
58
59
                username: userName,
                msg: singleChat,
Choi Ga Young's avatar
Choi Ga Young committed
60
61
62
            })
            setSingleChat([''])
        }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
63
    }, [singleChat])
Choi Ga Young's avatar
에러    
Choi Ga Young committed
64
65


Soo Hyun Kim's avatar
Soo Hyun Kim committed
66
67
    useEffect(() => {
        socket.on("sendedMSG", (msg) => {
Choi Ga Young's avatar
Choi Ga Young committed
68
            console.log('msg',msg)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
69
            setRecievedMsg(msg)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
70
        })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
71
    }, [])
우지원's avatar
e    
우지원 committed
72

Soo Hyun Kim's avatar
Soo Hyun Kim committed
73
74
75
76
77
78
79
    return (
        <>
            <Menu />
            <Row className="mr-0">
                <Col className="list" md={5}>
                    <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example">
                        <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
80
                            <ClosedList enterChatRoom={enterChatRoom} setRoomCode={setRoomCode} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
81
                        </Tab>
82
83
                        <Tab eventKey="open" title="공개방" onClick={handleChato}>
                            <OpenList enterChatRoom={enterChatRoom} setRoomCode={setRoomCode} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
84
85
86
87
                        </Tab>
                    </Tabs>
                </Col>
                <Col style={{ padding: "0" }}>
우지원's avatar
ul    
우지원 committed
88

Choi Ga Young's avatar
Choi Ga Young committed
89
                    {chat ? <Chat handleChatc={handleChatc} sendMsg={sendMsg} singleChat={singleChat} recievedMsg={recievedMsg} newUser={newUser} setSingleChat={setSingleChat} roomCode={roomCode} /> : <div style={{ position: "fixed", bottom: "20px", right: "30px" }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
90
                        <Button variant="primary" onClick={handleShowModal} size="lg" block>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
91
92
                            생성
                        </Button>
우지원's avatar
ul    
우지원 committed
93

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

export default Home;
107