import React, { useState, useEffect } from "react"; import { Link, useParams } from "react-router-dom"; import ChannelSingle from "./ChannelSingle"; import catchErrors from "../../context/catchError"; import KakaoShareButton from "../KakaoShareButton"; import RoomSettingChange from "./RoomSettingChange"; import ChannelSettingChange from "./ChannelSettingChange"; import userApi from "../../apis/user.api"; import roomApi from "../../apis/room.api"; const INIT_ROOM = { name: "", owner: "", }; const INIT_CHANNEL = { channelName: "", joinUser: [], }; const RightHamburger = () => { const [channel, setChannel] = useState([INIT_CHANNEL]); const [room, setRoom] = useState([INIT_ROOM]); const { roomId } = useParams(); const [error, setError] = useState(""); const id = sessionStorage.getItem("user"); async function getRoom(roomId) { try { const data = await roomApi.getRoom([roomId]); const roomdata = []; roomdata.push({ name: data[0].name, owner: data[0].owner, }); setRoom(roomdata); } catch (error) { catchErrors(error, setError); } } async function exitRoom() { try { const data = await roomApi.exitRoom({ id, roomId }); if (data.owner === Number(id)){ const room = await roomApi.removeRoom({roomId : roomId}) console.log(room) } } catch (error) { catchErrors(error, setError); } } async function getChannel(roomId) { try { const data = await roomApi.getRoom([roomId]); const Channel = data[0].channel; const channelList = []; for (const prop in Channel) { // Channel의 항목(prop)으로 작업을 실행합니다 for (const el in Channel[prop]) { channelList.push({ channelName: el, joinUser: Channel[prop][el], }); } } setChannel(channelList); } catch (error) { catchErrors(error, setError); } } async function exitChannel() { try { const data = await userApi.getUser(id); const A = doubleJoinCheck(data.name); if (A) { await roomApi.doubleJoin({ roomId: roomId, index1: A.index1, index2: A.index2, joinChName: A.joinChName, }); } } catch (error) { catchErrors(error, setError); } } function doubleJoinCheck(e) { for (const index in channel) { for (const el in channel[index].joinUser) { if (channel[index].joinUser[el] === e) { const doublejoinCh = channel[index].channelName; const A = { index1: index, index2: el, joinChName: doublejoinCh, }; return A; } } } } async function exitChannel() { try { const data = await userApi.getUser(id); const A = doubleJoinCheck(data.name) if (A) { await roomApi.doubleJoin({ roomId: roomId, index1: A.index1, index2: A.index2, joinChName: A.joinChName }) } } catch (error) { catchErrors(error, setError); } } function doubleJoinCheck(e) { for (const index in channel) { for (const el in channel[index].joinUser) { if (channel[index].joinUser[el] === e) { const doublejoinCh = channel[index].channelName const A = { index1: index, index2: el, joinChName: doublejoinCh, } return A } } } } // console.log(channel) useEffect(() => { getChannel(roomId); getRoom(roomId); }, [roomId]); function roomIdCopy() { const t = document.querySelector("#roomId").innerText; console.log(t); navigator.clipboard.writeText(t); document.execCommand("copy"); } return (

{room[0].name}

{" "} {`${roomId}`}{" "}
{room[0].owner == id ? ( ) : null}
); }; export default RightHamburger;