HomePage.js 6.61 KB
Newer Older
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';
Choi Ga Young's avatar
Choi Ga Young 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";
12

13
const socket = io();
14

Soo Hyun Kim's avatar
Soo Hyun Kim committed
15
const INIT_ROOM = {
16
17
18
  roomName: '',
  interest: '',
  isOpen: false
Soo Hyun Kim's avatar
Soo Hyun Kim committed
19
}
JeongYeonwoo's avatar
D    
JeongYeonwoo committed
20

Choi Ga Young's avatar
Choi Ga Young committed
21
function Home() {
22
  // const [namelist, setNamelist] = useState([])
JeongYeonwoo's avatar
JeongYeonwoo committed
23

Choi Ga Young's avatar
Choi Ga Young committed
24
  const [show, setShow] = useState(false);
25
  const [show2, setShow2] = useState(false);
Choi Ga Young's avatar
Choi Ga Young committed
26
  const [chat, setChat] = useState(false);
27
28
29
30
31
32
  const [room, setRoom] = useState(INIT_ROOM);
  const [disabled, setDisabled] = useState(true);
  const [error, setError] = useState('');

  const [singleChat, setSingleChat] = useState('')
  const [roomName, setRoomName] = useState('')
Choi Ga Young's avatar
Choi Ga Young committed
33
34
35

  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);
JeongYeonwoo's avatar
D    
JeongYeonwoo committed
36
  const handleChato = () => setChat(true);
Choi Ga Young's avatar
Choi Ga Young committed
37
  const handleChatc = () => setChat(false);
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  const handleClose2 = () => setShow2(false);
  const handleShow2 = () => setShow2(true);

  useEffect(() => {
    const isRoom = Object.values(room).every(el => Boolean(el))
    isRoom ? setDisabled(false) : setDisabled(true)
  }, [room])

  function handleChange(event) {
    const { name, value } = event.target
    setRoom({ ...room, [name]: value })
    console.log(room)
  }

  async function handleSubmit(event) {
    event.preventDefault()
    try {
      setError('')
      await axios.post('/room/makeRoom', room)
      setRoom(INIT_ROOM)
    } catch (error) {
      catchErrors(error, setError)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
60
    }
61
  }
Choi Ga Young's avatar
Choi Ga Young committed
62

63
  //SOCKET 관련 시작
64

65
66
67
  function enterChatroom(rName) {    //방 입장하기
    socket.emit('joinRoom', rName)
    console.log(`joinRoom : ${rName} 입장`)
68
69
70
71
72
73
  }

  const sendMsg = (e) => {
    e.preventDefault()
    socket.emit("chat", {
      roomName: roomName,
JeongYeonwoo's avatar
D    
JeongYeonwoo committed
74
      msg: inner,
75
76
77
78
    });
  }

  useEffect(() => {
79
80
81
82
    socket.emit("chat", {
      roomName: roomName,
      msg: singleChat
    })
83
    socket.on('broadcast', (msg) => {
84
85
      console.log(msg)
      setSingleChat(msg)
86
    })
87
  }, [singleChat])
88

Choi Ga Young's avatar
Choi Ga Young committed
89
  return (
90
    <>
Choi Ga Young's avatar
Choi Ga Young committed
91
      <Menu />
92
93
94
95
      <Row className="mr-0">
        <Col className="list" md={5}>
          <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example">
            <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
96
              <ClosedList enterChatroom={enterChatroom} setRoomName={setRoomName} />
97
98
            </Tab>
            <Tab eventKey="open" title="공개방" >
99
              <OpenList enterChatroom={enterChatroom} setRoomName={setRoomName} />
100
101
102
103
            </Tab>
          </Tabs>
        </Col>
        <Col style={{ padding: "0" }}>
104
          {chat ? <Chat handleChatc={handleChatc} sendMsg={sendMsg} singleChat={singleChat} setSingleChat={setSingleChat} roomName={roomName} /> : null}
105

106
107
108
          <div style={{ position: "fixed", bottom: "20px", right: "30px" }}>
            <Button variant="primary" onClick={handleShow} size="lg" block>
              생성
Soo Hyun Kim's avatar
Soo Hyun Kim committed
109
                        </Button>
우지원's avatar
ul    
우지원 committed
110

111
112
            <Button variant="secondary" onClick={handleShow2} size="lg" block>
              참가
Soo Hyun Kim's avatar
Soo Hyun Kim committed
113
                        </Button>
114
          </div>
115

116
117
118
119
          <Modal show={show} onHide={handleClose}>
            <Modal.Header closeButton>
              <Modal.Title> 생성</Modal.Title>
            </Modal.Header>
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
            {error && <Alert variant='danger'>
              {error}
            </Alert>}
            <Modal.Body>
              <Form onSubmit={handleSubmit}>
                <Form.Group as={Row} controlId="chatName">
                  <Form.Label column sm={4}> 이름</Form.Label>
                  <Col>
                    <Form.Control name='roomName' type='text' value={room.roomName} onChange={handleChange} />
                  </Col>
                </Form.Group>
                <Form.Group as={Row} controlId="chatInterest">
                  <Form.Label column sm={4}>관심 분야</Form.Label>
                  <Col>
                    <Form.Control as="select" defaultValue="Choose..." name='interest' type='text' value={room.interest} onChange={handleChange}>
                      <option>Choose...</option>
                      <option>과학</option>
                      <option>수학</option>
                      <option>예술</option>
                      <option>언어</option>
                      <option>취미</option>
                    </Form.Control>
                    {/* <Form.Control type="text" /> */}
                  </Col>
                </Form.Group>
                <Form.Group as={Row} controlId="chatIsOpen">
                  <Form.Label column sm={4}>공개방</Form.Label>
                  <Col>
                    <Form.Check
                      type="checkbox"
                      checked={room.isOpen}
                      name='isOpen'
                      onChange={() => setRoom({ ...room, isOpen: !room.isOpen })} />
                  </Col>
                </Form.Group>
                {
                  (room.isOpen)
                    ? (<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>

          <Modal show={show2} onHide={handleClose2}>
            <Modal.Header closeButton>
              <Modal.Title>참여 코드로 채팅 참가</Modal.Title>
            </Modal.Header>
            <Modal.Body>
              <Form onSubmit={() => { console.log('제출') }}>
                <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>
            </Modal.Body>
188
189
190
191
          </Modal>
        </Col>
      </Row>
    </>
Choi Ga Young's avatar
Choi Ga Young committed
192
  );
193
194
}

Choi Ga Young's avatar
Choi Ga Young committed
195
export default Home;