import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { Row, Col, Modal, Button, Form, Alert } from 'react-bootstrap';
import Tabs from 'react-bootstrap/Tabs';
import Tab from 'react-bootstrap/Tab';
import ClosedList from '../Components/ClosedList';
import OpenList from '../Components/OpenList';
import Menu from '../Components/Menu';
import catchErrors from '../utils/catchErrors';
import { io } from "socket.io-client"; //모듈 가져오기
import Chat from "../Components/Chat";
import { customAlphabet } from 'nanoid';
import { Redirect } from 'react-router-dom';
const socket = io();
const INIT_ROOM = {
roomName: '',
interest: '',
isOpen: false,
moderator: '',
}
function Home() {
const [show, setShow] = useState(false);
const [show2, setShow2] = useState(false);
const [chat, setChat] = useState(false);
const [room, setRoom] = useState(INIT_ROOM);
const [disabled, setDisabled] = useState(true);
const [error, setError] = useState('');
const [singleChat, setSingleChat] = useState('')
const [roomName, setRoomName] = useState('')
const [success, setSuccess] = useState(false)
const handleClose = () => setShow(false);
const handleShow = () => setShow(true);
const handleChato = () => setChat(true);
const handleChatc = () => setChat(false);
const handleClose2 = () => setShow2(false);
const handleShow2 = () => setShow2(true);
const moderator = sessionStorage.getItem('userId');
const nanoid = customAlphabet('1234567890abcdef', 10)
const roomId = nanoid()
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, moderator, roomId })
//console.log(room)
}
console.log(room)
console.log(room.roomId)
async function handleSubmit(event) {
event.preventDefault()
try {
setError('')
await axios.post('/room/makeRoom', room)
// await axios.patch('/user/signup', { joinroom: `${room.roomId}`})
// await axios.post(`/user/${moderator}`, room.roomId)
setSuccess(true)
// setRoom(INIT_ROOM)
setShow(false)
} catch (error) {
catchErrors(error, setError)
}
}
//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])
// console.log(room.roomId)
if (success) {
// console.log(room.roomId)
alert(`방암호는 ${room.roomId}입니다`)
// return
공개방으로 개설되어 공개방 목록에 공개되며, 코드를 공유하여 참가할 수도 있습니다.
) : (비밀방으로 개설되며, 참여자들에게 코드를 공유해야합니다.
) }