HomePage.js 3.25 KB
Newer Older
1
import React, { useState, useEffect } from 'react';
2
import { Row, Col, Modal, Button, Navbar, Nav } from 'react-bootstrap';
Choi Ga Young's avatar
Choi Ga Young 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 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";   //모듈 가져오기
11

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

14
const socket = io();
15

JeongYeonwoo's avatar
D    
JeongYeonwoo committed
16

Choi Ga Young's avatar
Choi Ga Young committed
17
function Home() {
JeongYeonwoo's avatar
D    
JeongYeonwoo committed
18
19
  const [namelist, setNamelist] = useState([])

JeongYeonwoo's avatar
JeongYeonwoo committed
20
21
  const [username, setUsername] = useState('defaultUser')

Choi Ga Young's avatar
Choi Ga Young committed
22
  const [show, setShow] = useState(false);
Choi Ga Young's avatar
Choi Ga Young committed
23
  const [chat, setChat] = useState(false);
Choi Ga Young's avatar
Choi Ga Young committed
24
25
26

  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);
JeongYeonwoo's avatar
D    
JeongYeonwoo committed
27
  const handleChato = () => setChat(true);
Choi Ga Young's avatar
Choi Ga Young committed
28
  const handleChatc = () => setChat(false);
Choi Ga Young's avatar
Choi Ga Young committed
29

30
31
  const [inner, setInner] = useState(['']) //인풋값
  const [chatmsg, setChatmsg] = useState([inner]) // 전송된 채팅들 저장하는 배열
32
33
34
35
36
37
38
39
40
  const [roomName, setRoomName] = useState('dd')   //방 선택이 방1을 눌럿다 방2를 누르면 전체로 전송됨

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

  const sendMsg = (e) => {
    e.preventDefault()
JeongYeonwoo's avatar
D    
JeongYeonwoo committed
41
    const name = sessionStorage.getItem('name')
42
43
44
45
    console.log('sendMsg', inner)
    console.log(roomName)
    socket.emit("chat", {
      roomName: roomName,
JeongYeonwoo's avatar
D    
JeongYeonwoo committed
46
47
      msg: inner,
      name : name
48
49
50
51
52
53
54
55
56
57
58
59
60
    });
  }

  function clearChat() {
    setChatmsg([])
  }

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

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

JeongYeonwoo's avatar
D    
JeongYeonwoo committed
61
      setChatmsg([...chatmsg, msg[0]])
JeongYeonwoo's avatar
JeongYeonwoo committed
62
      console.log('useeffect2', chatmsg)
JeongYeonwoo's avatar
D    
JeongYeonwoo committed
63
64
65
      console.log(msg[1])
      setNamelist([...namelist, msg[1]])
      console.log(namelist)
66
    })
JeongYeonwoo's avatar
JeongYeonwoo committed
67
    console.log('useeffect2', chatmsg)
68
69
70
  }, [chatmsg])


Choi Ga Young's avatar
Choi Ga Young committed
71
  return (
72
    <>
Choi Ga Young's avatar
Choi Ga Young committed
73
      <Menu />
74
75
76
77
      <Row className="mr-0">
        <Col className="list" md={5}>
          <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example">
            <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
78
              <ClosedList enterChatroom={enterChatroom} setRoomName={setRoomName} clearChat={clearChat} />
79
80
81
82
83
84
85
            </Tab>
            <Tab eventKey="open" title="공개방" >
              <OpenList />
            </Tab>
          </Tabs>
        </Col>
        <Col style={{ padding: "0" }}>
JeongYeonwoo's avatar
D    
JeongYeonwoo committed
86
          {chat ? <Chat handleChatc={handleChatc} sendMsg={sendMsg} setInner={setInner} chatmsg={chatmsg} roomName={roomName} namelist={namelist}/> : null}
87

88
89
          <Button variant="primary" onClick={handleShow} style={{ position: "fixed", bottom: "10px", right: "10px" }} >
            생성
Choi Ga Young's avatar
Choi Ga Young committed
90
        </Button>
91

92
93
94
95
96
97
98
99
          <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
100
            </Button>
101
102
103
104
105
            </Modal.Footer>
          </Modal>
        </Col>
      </Row>
    </>
Choi Ga Young's avatar
Choi Ga Young committed
106
  );
107
108
}

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