HomePage.js 3.83 KB
Newer Older
Soo Hyun Kim's avatar
Soo Hyun Kim committed
1
import React, { useState, useEffect } from 'react';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
2
3
import axios from 'axios';
import { Row, Col, Modal, Button, Form, Alert } from 'react-bootstrap';
우지원's avatar
ul    
우지원 committed
4
5
6
7
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
8
import Menu from '../Components/Menu';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
9
import catchErrors from '../utils/catchErrors';
10
import { io } from "socket.io-client";   //모듈 가져오기
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
11
import Chat from "../Components/Chat";
Soo Hyun Kim's avatar
Soo Hyun Kim committed
12
13
import RoomMake from "../Components/RoomMake"
import EnterRoom from "../Components/EnterRoom"
우지원's avatar
ul    
우지원 committed
14

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

function Home() {
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
soo0115    
Soo Hyun Kim committed
25
    const [roomName, setRoomName] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
26
    const [roomCode, setRoomCode] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
27

Soo Hyun Kim's avatar
Soo Hyun Kim committed
28
29
    //방참가
    const [roomId, setRoomId] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
30

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

우지원's avatar
e    
우지원 committed
38
    const member = sessionStorage.getItem('userId');
우지원's avatar
수정    
우지원 committed
39

Soo Hyun Kim's avatar
Soo Hyun Kim committed
40
    useEffect(() => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
41
42
43
        const isRoom = Object.values(room).every(el => Boolean(el))
        isRoom ? setDisabled(false) : setDisabled(true)
    }, [room])
Soo Hyun Kim's avatar
Soo Hyun Kim committed
44

Soo Hyun Kim's avatar
Soo Hyun Kim committed
45
46
    function handleChange(event) {
        const { name, value } = event.target
47
        setRoom({ ...room, [name]: value, member })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
48
49
    }

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
50
51
52
53
54
55
56
57
58
59
60
61
    //SOCKET 관련 시작
    function enterChatroom(rName) {    //방 입장하기
        socket.emit('joinRoom', rName)
        console.log(`joinRoom : ${rName} 입장`)
    }

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

    useEffect(() => {
        socket.emit("chat", {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
62
            roomName: roomCode,
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
63
64
            msg: singleChat
        })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
65
66
67
68
69
    }, [singleChat])
    
    
    useEffect(() => {
        socket.on("sendedMSG", (msg) => {
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
70
            console.log(msg)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
71
            setRecievedMsg(msg)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
72
        })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
73
    }, [])
우지원's avatar
e    
우지원 committed
74

Soo Hyun Kim's avatar
Soo Hyun Kim committed
75
76
77
78
79
80
81
    return (
        <>
            <Menu />
            <Row className="mr-0">
                <Col className="list" md={5}>
                    <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example">
                        <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
우지원's avatar
수정    
우지원 committed
82
                            <ClosedList enterChatroom={enterChatroom} setRoomName={setRoomName} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
83
84
                        </Tab>
                        <Tab eventKey="open" title="공개방" >
우지원's avatar
수정    
우지원 committed
85
                            <OpenList enterChatroom={enterChatroom} setRoomName={setRoomName} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
86
87
88
89
                        </Tab>
                    </Tabs>
                </Col>
                <Col style={{ padding: "0" }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
90
                    {chat ? <Chat handleChatc={handleChatc} sendMsg={sendMsg} singleChat={singleChat} recievedMsg={recievedMsg} setSingleChat={setSingleChat} roomName={roomName}/> : null}
우지원's avatar
ul    
우지원 committed
91

Soo Hyun Kim's avatar
Soo Hyun Kim committed
92
                    <div style={{ position: "fixed", bottom: "20px", right: "30px" }}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
93
                        <Button variant="primary" onClick={handleShowModal} size="lg" block>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
94
95
                            생성
                        </Button>
우지원's avatar
ul    
우지원 committed
96

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

export default Home;
110