HomePage.js 3.02 KB
Newer Older
Soo Hyun Kim's avatar
Soo Hyun Kim committed
1
import React, { useState, useEffect } from 'react';
2
import { Row, Col, Modal, Button, Navbar, Nav } from 'react-bootstrap';
우지원's avatar
ul    
우지원 committed
3
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';
import Chat from '../Components/Chat';
Choi Ga Young's avatar
Choi Ga Young committed
8
import Menu from '../Components/Menu';
9

10
import { io } from "socket.io-client";   //모듈 가져오기
우지원's avatar
ul    
우지원 committed
11

12
// const userName = "정연우";
13

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

function Home() {
JeongYeonwoo's avatar
JeongYeonwoo committed
17
18
  const [username, setUsername] = useState('defaultUser')

Choi Ga Young's avatar
Choi Ga Young committed
19
  const [show, setShow] = useState(false);
Choi Ga Young's avatar
Choi Ga Young committed
20
  const [chat, setChat] = useState(false);
Choi Ga Young's avatar
Choi Ga Young committed
21
22
23

  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);
24
  const handleChato = () => setChat(true); 
Choi Ga Young's avatar
Choi Ga Young committed
25
  const handleChatc = () => setChat(false);
Choi Ga Young's avatar
Choi Ga Young committed
26

27
28
  const [inner, setInner] = useState(['']) //인풋값
  const [chatmsg, setChatmsg] = useState([inner]) // 전송된 채팅들 저장하는 배열
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  const [roomName, setRoomName] = useState('dd')   //방 선택이 방1을 눌럿다 방2를 누르면 전체로 전송됨

  function enterChatroom(roomName) {    //방 입장하기
    socket.emit('joinRoom', roomName)
    console.log(`joinRoom : ${roomName} 입장`)
  }

  const sendMsg = (e) => {
    e.preventDefault()
    console.log('sendMsg', inner)
    console.log(roomName)
    socket.emit("chat", {
      roomName: roomName,
      msg: inner
    });
  }

  function clearChat() {
    setChatmsg([])
  }

  useEffect(() => {
    socket.on('broadcast', (msg) => {

      console.log('msg', msg)
      console.log('inner :', inner)

JeongYeonwoo's avatar
JeongYeonwoo committed
56
57
      setChatmsg([...chatmsg, msg])
      console.log('useeffect2', chatmsg)
58
    })
JeongYeonwoo's avatar
JeongYeonwoo committed
59
    console.log('useeffect2', chatmsg)
60
61
  }, [chatmsg])

Choi Ga Young's avatar
Choi Ga Young committed
62
  return (
63
    <>
Choi Ga Young's avatar
Choi Ga Young committed
64
      <Menu />
65
66
67
68
      <Row className="mr-0">
        <Col className="list" md={5}>
          <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example">
            <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
69
              <ClosedList enterChatroom={enterChatroom} setRoomName={setRoomName} clearChat={clearChat} />
70
71
72
73
74
75
76
            </Tab>
            <Tab eventKey="open" title="공개방" >
              <OpenList />
            </Tab>
          </Tabs>
        </Col>
        <Col style={{ padding: "0" }}>
77
          {chat ? <Chat handleChatc={handleChatc} sendMsg={sendMsg} setInner={setInner} chatmsg={chatmsg} roomName={roomName} /> : null}
78

79
80
          <Button variant="primary" onClick={handleShow} style={{ position: "fixed", bottom: "10px", right: "10px" }} >
            생성
Choi Ga Young's avatar
Choi Ga Young committed
81
        </Button>
82

83
84
85
86
87
88
89
90
          <Modal show={show} onHide={handleClose}>
            <Modal.Header closeButton>
              <Modal.Title> 생성</Modal.Title>
            </Modal.Header>
            <Modal.Body>여기에 form 입력</Modal.Body>
            <Modal.Footer>
              <Button variant="primary" onClick={handleClose}>
                생성
Choi Ga Young's avatar
Choi Ga Young committed
91
            </Button>
92
93
94
95
96
            </Modal.Footer>
          </Modal>
        </Col>
      </Row>
    </>
Choi Ga Young's avatar
Choi Ga Young committed
97
  );
우지원's avatar
ul    
우지원 committed
98
99
100
}

export default Home;
101