HomePage.js 5.56 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
우지원 committed
12
import axios from 'axios';
우지원's avatar
ul    
우지원 committed
13

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

우지원's avatar
우지원 committed
16
17
18
19
20
21
22
const INIT_ROOM = {
    roomName: '',
    interest: '',
    roomId: '',
    member: '',
}

우지원's avatar
ul    
우지원 committed
23
function Home() {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
24
25
    const [showModal, setShowModal] = useState(false);
    const [showEnter, setEnter] = useState(false);
Soo Hyun Kim's avatar
Soo Hyun Kim committed
26
    const [chat, setChat] = useState(false);
우지원's avatar
우지원 committed
27
28
    const [open, setOpen] = useState(false);
    const [room, setRoom] = useState(INIT_ROOM)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
29

Soo Hyun Kim's avatar
Soo Hyun Kim committed
30
    //소켓
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
31
    const [singleChat, setSingleChat] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
32
    const [recievedMsg, setRecievedMsg] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
33
    const [roomName, setRoomName] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
34
    const [roomCode, setRoomCode] = useState('')
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
35

Soo Hyun Kim's avatar
Soo Hyun Kim committed
36
37
38
39
    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
40
    const handleChato = () => setChat(true);
우지원's avatar
우지원 committed
41
42
    const handleChatc = () => setOpen(true);

Soo Hyun Kim's avatar
Soo Hyun Kim committed
43
44


Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
45
    //SOCKET 관련 시작
우지원's avatar
우지원 committed
46
    //내채팅방에서 채팅시작
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
47
48
49
50
51
    function enterChatroom(rName) {    //방 입장하기
        socket.emit('joinRoom', rName)
        console.log(`joinRoom : ${rName} 입장`)
    }

우지원's avatar
우지원 committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    //오픈채팅방에서 참가하기
    async function openListroom(roomId) {
        console.log(roomId)
        const roomInf = await axios.get('/room/changeMem', { params: { 'roomId': roomId } })
        console.log(roomInf)
        console.log(roomInf.data)
        console.log(roomInf.data.[0])
        setRoom(roomInf.data.[0])
    }

    async function attendListRoom() {
        const userId = sessionStorage.getItem('userId'); //sessionStorage에 저장된 userId가져옴
        const roomId = room.roomId
        const tf = await axios.put('/room/changeMem', { userId: userId, roomId: roomId })
        if(tf.data) {
            alert('참가되었습니다.')
        } else {
            alert('이미 참가된 방입니다.')
        }
    }

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
73
74
75
76
77
78
    const sendMsg = (e) => {
        e.preventDefault()
    }

    useEffect(() => {
        socket.emit("chat", {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
79
            roomName: roomCode,
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
80
81
            msg: singleChat
        })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
82
    }, [singleChat])
Choi Ga Young's avatar
에러    
Choi Ga Young committed
83
84


Soo Hyun Kim's avatar
Soo Hyun Kim committed
85
86
    useEffect(() => {
        socket.on("sendedMSG", (msg) => {
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
87
            console.log(msg)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
88
            setRecievedMsg(msg)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
89
        })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
90
    }, [])
우지원's avatar
e    
우지원 committed
91

Soo Hyun Kim's avatar
Soo Hyun Kim committed
92
93
94
95
96
97
98
    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
99
                            <ClosedList enterChatroom={enterChatroom} setRoomName={setRoomName} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
100
                        </Tab>
우지원's avatar
우지원 committed
101
102
                        <Tab eventKey="open" title="공개방" onClick={handleChatc}>
                            <OpenList openListroom={openListroom} setRoomName={setRoomName} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
103
104
105
106
                        </Tab>
                    </Tabs>
                </Col>
                <Col style={{ padding: "0" }}>
우지원's avatar
우지원 committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
                    {chat ?
                        <Chat handleChatc={handleChatc} sendMsg={sendMsg} singleChat={singleChat} recievedMsg={recievedMsg} setSingleChat={setSingleChat} roomName={roomName} />
                        : <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>
                        </div>}

                    {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> 방코드(방코드를 통해서도 참여할  있습니다.) : {room.roomId}</h5>
                                    <Button className="d-flex flex-end mt-4" variant="primary" type="submit" onClick={attendListRoom}> 참가 </Button>
                                </div>
                            </div>
                        </div> :
                        <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>
                        </div>}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
130
131
                </Col>
            </Row>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
132
            <RoomMake showModal={showModal} handleCloseModal={handleCloseModal} />
Choi Ga Young's avatar
에러    
Choi Ga Young committed
133
            <EnterRoom showEnter={showEnter} enterChatRoom={enterChatroom} handleCloseEnter={handleCloseEnter} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
134
135
        </>
    );
우지원's avatar
ul    
우지원 committed
136
137
138
}

export default Home;
139