HomePage.js 6.22 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)
우지원's avatar
우지원 committed
29
    const [show, setShow] = useState(true)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
30

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

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

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


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

우지원's avatar
우지원 committed
53
54
55
56
57
58
59
60
    //오픈채팅방에서 참가하기
    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])
우지원's avatar
우지원 committed
61
        setShow(false)
우지원's avatar
우지원 committed
62
63
64
65
66
67
    }

    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 })
우지원's avatar
우지원 committed
68
        if (tf.data) {
우지원's avatar
우지원 committed
69
70
71
72
73
74
            alert('참가되었습니다.')
        } else {
            alert('이미 참가된 방입니다.')
        }
    }

우지원's avatar
우지원 committed
75
76
77
78
    function enterButton() {
        setOpen(false)
    }

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
79
80
81
82
83
84
    const sendMsg = (e) => {
        e.preventDefault()
    }

    useEffect(() => {
        socket.emit("chat", {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
85
            roomName: roomCode,
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
86
87
            msg: singleChat
        })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
88
    }, [singleChat])
Choi Ga Young's avatar
에러    
Choi Ga Young committed
89
90


Soo Hyun Kim's avatar
Soo Hyun Kim committed
91
92
    useEffect(() => {
        socket.on("sendedMSG", (msg) => {
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
93
            console.log(msg)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
94
            setRecievedMsg(msg)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
95
        })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
96
    }, [])
우지원's avatar
e    
우지원 committed
97

Soo Hyun Kim's avatar
Soo Hyun Kim committed
98
99
100
101
102
103
104
    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
105
                            <ClosedList enterChatroom={enterChatroom} setRoomName={setRoomName} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
106
                        </Tab>
우지원's avatar
우지원 committed
107
108
                        <Tab eventKey="open" title="공개방" onClick={handleChatc}>
                            <OpenList openListroom={openListroom} setRoomName={setRoomName} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
109
110
111
112
                        </Tab>
                    </Tabs>
                </Col>
                <Col style={{ padding: "0" }}>
우지원's avatar
우지원 committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
                    {show ? <>
                        {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 className="mb-3"> 방코드(방코드를 통해서도 참여할  있습니다.) : {room.roomId}</h5>
                                            <Row className='justify-content-center'>
우지원's avatar
우지원 committed
130
131
                                                <Button variant="outline-warning" size="sm" className="mr-4" onClick={enterButton}>뒤로가기</Button>
                                                <Button variant="outline-warning" size="sm" className="ml-4" type='submit' onClick={attendListRoom}>참가</Button>
우지원's avatar
우지원 committed
132
133
134
135
136
137
138
139
140
                                            </Row>
                                        </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
141
142
                </Col>
            </Row>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
143
            <RoomMake showModal={showModal} handleCloseModal={handleCloseModal} />
Choi Ga Young's avatar
에러    
Choi Ga Young committed
144
            <EnterRoom showEnter={showEnter} enterChatRoom={enterChatroom} handleCloseEnter={handleCloseEnter} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
145
146
        </>
    );
우지원's avatar
ul    
우지원 committed
147
148
149
}

export default Home;
150