HomePage.js 8.71 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";
우지원's avatar
수정    
우지원 committed
12
13
import { customAlphabet } from 'nanoid';
import { Redirect } from 'react-router-dom';
14

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

Soo Hyun Kim's avatar
Soo Hyun Kim committed
17
18
const INIT_ROOM = {
    roomName: '',
Soo Hyun Kim's avatar
Soo Hyun Kim committed
19
    interest: '',
우지원's avatar
수정    
우지원 committed
20
21
    isOpen: false,
    moderator: '',
Soo Hyun Kim's avatar
Soo Hyun Kim committed
22
}
우지원's avatar
ul    
우지원 committed
23
24

function Home() {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
25
26
27
    const [show, setShow] = useState(false);
    const [show2, setShow2] = useState(false);
    const [chat, setChat] = useState(false);
Soo Hyun Kim's avatar
Soo Hyun Kim committed
28
    const [room, setRoom] = useState(INIT_ROOM);
Soo Hyun Kim's avatar
Soo Hyun Kim committed
29
    const [disabled, setDisabled] = useState(true);
Soo Hyun Kim's avatar
Soo Hyun Kim committed
30
    const [error, setError] = useState('');
Soo Hyun Kim's avatar
Soo Hyun Kim committed
31

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
32
33
    const [singleChat, setSingleChat] = useState('')
    const [roomName, setRoomName] = useState('')
우지원's avatar
수정    
우지원 committed
34
    const [success, setSuccess] = useState(false)
Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
35

Soo Hyun Kim's avatar
Soo Hyun Kim committed
36
37
38
39
40
41
42
    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);
    const handleChato = () => setChat(true);
    const handleChatc = () => setChat(false);
    const handleClose2 = () => setShow2(false);
    const handleShow2 = () => setShow2(true);

우지원's avatar
수정    
우지원 committed
43
44
45
46
    const moderator = sessionStorage.getItem('userId');
    const nanoid = customAlphabet('1234567890abcdef', 10)
    const roomId = nanoid()

Soo Hyun Kim's avatar
Soo Hyun Kim committed
47
    useEffect(() => {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
48
49
50
        const isRoom = Object.values(room).every(el => Boolean(el))
        isRoom ? setDisabled(false) : setDisabled(true)
    }, [room])
Soo Hyun Kim's avatar
Soo Hyun Kim committed
51

Soo Hyun Kim's avatar
Soo Hyun Kim committed
52
53
    function handleChange(event) {
        const { name, value } = event.target
우지원's avatar
수정    
우지원 committed
54
55
        setRoom({ ...room, [name]: value, moderator, roomId })
        //console.log(room)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
56
57
    }

우지원's avatar
수정    
우지원 committed
58
59
60
    console.log(room)
    console.log(room.roomId)

Soo Hyun Kim's avatar
Soo Hyun Kim committed
61
    async function handleSubmit(event) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
62
        event.preventDefault()
우지원's avatar
수정    
우지원 committed
63

Soo Hyun Kim's avatar
Soo Hyun Kim committed
64
65
        try {
            setError('')
66
            await axios.post('/room/makeRoom', room)
우지원's avatar
수정    
우지원 committed
67
68
69
70
71
72
            // await axios.patch('/user/signup', { joinroom: `${room.roomId}`})
            // await axios.post(`/user/${moderator}`, room.roomId)
            setSuccess(true)
            // setRoom(INIT_ROOM)
            setShow(false)
        } catch (error) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
73
74
            catchErrors(error, setError)
        }
우지원's avatar
수정    
우지원 committed
75

Soo Hyun Kim's avatar
Soo Hyun Kim committed
76
77
    }

Soo Hyun Kim's avatar
soo0115    
Soo Hyun Kim committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    //SOCKET 관련 시작
    function enterChatroom(rName) {    //방 입장하기
        socket.emit('joinRoom', rName)
        console.log(`joinRoom : ${rName} 입장`)
    }

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

    useEffect(() => {
        socket.emit("chat", {
            roomName: roomName,
            msg: singleChat
        })
        socket.on('broadcast', (msg) => {
            console.log(msg)
            setSingleChat(msg)
        })
    }, [singleChat])

우지원's avatar
수정    
우지원 committed
99
    // console.log(room.roomId)
100
    if (success) {
우지원's avatar
수정    
우지원 committed
101
102
103
104
105
        // console.log(room.roomId)
        alert(`방암호는 ${room.roomId}입니다`)
        // return <Redirect to='/home' />        
    }

Soo Hyun Kim's avatar
Soo Hyun Kim committed
106
107
108
109
110
111
112
    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
113
                            <ClosedList enterChatroom={enterChatroom} setRoomName={setRoomName} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
114
115
                        </Tab>
                        <Tab eventKey="open" title="공개방" >
우지원's avatar
수정    
우지원 committed
116
                            <OpenList enterChatroom={enterChatroom} setRoomName={setRoomName} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
117
118
119
120
                        </Tab>
                    </Tabs>
                </Col>
                <Col style={{ padding: "0" }}>
우지원's avatar
수정    
우지원 committed
121
                    {chat ? <Chat handleChatc={handleChatc} sendMsg={sendMsg} singleChat={singleChat} setSingleChat={setSingleChat} roomName={roomName} /> : null}
우지원's avatar
ul    
우지원 committed
122

Soo Hyun Kim's avatar
Soo Hyun Kim committed
123
124
125
126
                    <div style={{ position: "fixed", bottom: "20px", right: "30px" }}>
                        <Button variant="primary" onClick={handleShow} size="lg" block>
                            생성
                        </Button>
우지원's avatar
ul    
우지원 committed
127

Soo Hyun Kim's avatar
Soo Hyun Kim committed
128
129
130
131
                        <Button variant="secondary" onClick={handleShow2} size="lg" block>
                            참가
                        </Button>
                    </div>
우지원's avatar
ul    
우지원 committed
132

Soo Hyun Kim's avatar
Soo Hyun Kim committed
133
134
135
136
                    <Modal show={show} onHide={handleClose}>
                        <Modal.Header closeButton>
                            <Modal.Title> 생성</Modal.Title>
                        </Modal.Header>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
137
138
139
                        {error && <Alert variant='danger'>
                            {error}
                        </Alert>}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
140
141
142
143
144
                        <Modal.Body>
                            <Form onSubmit={handleSubmit}>
                                <Form.Group as={Row} controlId="chatName">
                                    <Form.Label column sm={4}> 이름</Form.Label>
                                    <Col>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
145
                                        <Form.Control name='roomName' type='text' value={room.roomName} onChange={handleChange} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
146
147
148
149
150
                                    </Col>
                                </Form.Group>
                                <Form.Group as={Row} controlId="chatInterest">
                                    <Form.Label column sm={4}>관심 분야</Form.Label>
                                    <Col>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
151
                                        <Form.Control as="select" defaultValue="Choose..." name='interest' type='text' value={room.interest} onChange={handleChange}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
                                            <option>Choose...</option>
                                            <option>과학</option>
                                            <option>수학</option>
                                            <option>예술</option>
                                            <option>언어</option>
                                            <option>취미</option>
                                        </Form.Control>
                                    </Col>
                                </Form.Group>
                                <Form.Group as={Row} controlId="chatIsOpen">
                                    <Form.Label column sm={4}>공개방</Form.Label>
                                    <Col>
                                        <Form.Check
                                            type="checkbox"
Soo Hyun Kim's avatar
Soo Hyun Kim committed
166
                                            checked={room.isOpen}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
167
                                            name='isOpen'
Soo Hyun Kim's avatar
Soo Hyun Kim committed
168
                                            onChange={() => setRoom({ ...room, isOpen: !room.isOpen })} />
Soo Hyun Kim's avatar
Soo Hyun Kim committed
169
170
171
                                    </Col>
                                </Form.Group>
                                {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
172
                                    (room.isOpen)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
173
174
175
176
177
178
179
180
181
182
183
                                        ? (<p><b>공개방</b>으로 개설되어 공개방 목록에 공개되며, 코드를 공유하여 참가할 수도 있습니다.</p>)
                                        : (<p><b>비밀방</b>으로 개설되며, 참여자들에게 코드를 공유해야합니다.</p>)
                                }
                                <Form.Group as={Row}>
                                    <Col sm={{ span: 5, offset: 4 }}>
                                        <Button type="submit" > 생성</Button>
                                    </Col>
                                </Form.Group>
                            </Form>
                        </Modal.Body>
                    </Modal>
우지원's avatar
ul    
우지원 committed
184

Soo Hyun Kim's avatar
Soo Hyun Kim committed
185
                    <Modal show={show2} onHide={handleClose2}>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
186
187
188
189
                        <Modal.Header closeButton>
                            <Modal.Title>참여 코드로 채팅 참가</Modal.Title>
                        </Modal.Header>
                        <Modal.Body>
우지원's avatar
수정    
우지원 committed
190
191
192
193
194
195
196
197
198
199
200
201
202
                            {/* <Form onSubmit={handleSubmit2}> */}
                            <Form.Group as={Row} controlId="formCodeE">
                                <Form.Label column sm={4}>참여 코드</Form.Label>
                                <Col>
                                    <Form.Control type="text" />
                                </Col>
                            </Form.Group>
                            <Form.Group as={Row}>
                                <Col sm={{ span: 5, offset: 4 }}>
                                    <Button type="submit">참가</Button>
                                </Col>
                            </Form.Group>
                            {/* </Form> */}
Soo Hyun Kim's avatar
Soo Hyun Kim committed
203
204
                        </Modal.Body>
                    </Modal>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
205
206
207
208
                </Col>
            </Row>
        </>
    );
우지원's avatar
ul    
우지원 committed
209
210
211
}

export default Home;
212