Commit d7bf440f authored by 우지원's avatar 우지원
Browse files

채널퇴장 완료!!!!!!!!!!ㅠㅠㅠㅠㅠㅠ

parent ef2257c4
import { Link } from 'react-router-dom' import { Link, useParams } from 'react-router-dom'
import React, { useState } from 'react'; import React, { useEffect, useState } from 'react';
import RightHamburger from './RightHamburger'; import RightHamburger from './RightHamburger';
import roomApi from '../../apis/room.api';
import catchErrors from '../../context/catchError';
import userApi from '../../apis/user.api';
const INIT_CHANNEL = {
channelName: "",
joinUser: [],
};
const ChannelList = () => { const ChannelList = () => {
const { roomId } = useParams();
const [error, setError] = useState("");
const [channel, setChannel] = useState([INIT_CHANNEL]);
const id = localStorage.getItem('user'); const id = localStorage.getItem('user');
async function getChannel(roomId) {
try {
const data = await roomApi.getRoom([roomId]);
const Channel = data[0].channel;
const channelList = [];
for (const prop in Channel) {
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
}
}
}
}
useEffect(() => {
getChannel(roomId);
}, [roomId])
return ( return (
<div> <div>
<nav className="navbar navbar-light "> <nav className="navbar navbar-light ">
<div className="col-2"></div> <div className="col-2"></div>
<div> <div onClick={exitChannel}>
<Link to={`/user/${id}`}> <Link to={`/user/${id}`}>
<img src="/BORA.png" style={{ width: '160px' }} /> <img src="/BORA.png" style={{ width: '160px' }} />
</Link> </Link>
......
...@@ -6,12 +6,9 @@ import catchErrors from "../../context/catchError"; ...@@ -6,12 +6,9 @@ import catchErrors from "../../context/catchError";
const ChannelSingle = (props) => { const ChannelSingle = (props) => {
const [error, setError] = useState(""); const [error, setError] = useState("");
const [userName, setUserName] = useState('');
const [success, setSuccess] = useState(false); const [success, setSuccess] = useState(false);
const [roomName, setRoomName] = useState(''); const [roomName, setRoomName] = useState('');
const { roomId, channelId } = useParams(); const { roomId, channelId } = useParams();
console.log('props', props.channel)
console.log('hi', channelId)
const userId = localStorage.getItem('user') const userId = localStorage.getItem('user')
...@@ -19,22 +16,14 @@ const ChannelSingle = (props) => { ...@@ -19,22 +16,14 @@ const ChannelSingle = (props) => {
console.log(e, userId) console.log(e, userId)
try { try {
const data = await userApi.getUser(userId); const data = await userApi.getUser(userId);
setUserName(data.name)
const index1 = indexCheck(e) const index1 = indexCheck(e)
const A = doubleJoinCheck(data.name) const A = doubleJoinCheck(data.name)
console.log(A)
const mem = props.channel[index1].joinUser const mem = props.channel[index1].joinUser
const joinCh = mem.includes(data.name); const joinCh = mem.includes(data.name);
console.log(A)
if (!joinCh) { if (!joinCh) {
if (A) { if (A) {
console.log('더블더블') await roomApi.doubleJoin({ roomId: roomId, index1: A.index1, index2: A.index2, joinChName: A.joinChName })
console.log(A.index1, A.index2, A.joinChName)
// { roomId: roomId, index1: A.index1, index2: A.index2, joinChName: A.joinChName }
const double = await roomApi.doubleJoin({ roomId: roomId, index1: A.index1, index2: A.index2, joinChName: A.joinChName })
console.log(double)
} }
console.log('서버연결시작')
const roomA = await roomApi.joinChannel({ roomId: roomId, channelName: e, plusUser: data.name, index: index1 }) const roomA = await roomApi.joinChannel({ roomId: roomId, channelName: e, plusUser: data.name, index: index1 })
setRoomName(e) setRoomName(e)
setSuccess(true) setSuccess(true)
...@@ -46,8 +35,6 @@ const ChannelSingle = (props) => { ...@@ -46,8 +35,6 @@ const ChannelSingle = (props) => {
} }
} }
console.log('정보들어왔나', roomName, success)
function indexCheck(e) { function indexCheck(e) {
for (const index1 in props.channel) { for (const index1 in props.channel) {
if (props.channel[index1].channelName === e) { if (props.channel[index1].channelName === e) {
......
...@@ -17,9 +17,7 @@ const InitRoom = () => { ...@@ -17,9 +17,7 @@ const InitRoom = () => {
async function getRoom(roomId) { async function getRoom(roomId) {
try { try {
const data = await roomApi.getRoom([roomId]); const data = await roomApi.getRoom([roomId]);
console.log(data)
setRoom({...room, id:data[0].id, name:data[0].name, profileimg: data[0].profileimg}) setRoom({...room, id:data[0].id, name:data[0].name, profileimg: data[0].profileimg})
console.log(room.profileimg)
} catch (error) { } catch (error) {
catchErrors(error, setError); catchErrors(error, setError);
} }
......
...@@ -32,28 +32,22 @@ const RightHamburger = () => { ...@@ -32,28 +32,22 @@ const RightHamburger = () => {
console.log("id, roomid정보", id, roomId); console.log("id, roomid정보", id, roomId);
try { try {
const data = await RoomApi.exitRoom({ id, roomId }); const data = await RoomApi.exitRoom({ id, roomId });
console.log(data);
} catch (error) { } catch (error) {
catchErrors(error, setError); catchErrors(error, setError);
} }
} }
async function getChannel(roomId) { async function getChannel(roomId) {
const ID = roomId;
try { try {
const data = await RoomApi.getRoom([ID]); const data = await RoomApi.getRoom([roomId]);
const Channel = data[0].channel; const Channel = data[0].channel;
console.log("방데이터:", Channel);
const channelList = []; const channelList = [];
for (const prop in Channel) { for (const prop in Channel) {
// Channel의 항목(prop)으로 작업을 실행합니다 // Channel의 항목(prop)으로 작업을 실행합니다
for (const key in Channel[prop]) { for (const el in Channel[prop]) {
console.log(key);
console.log(prop);
console.log(Channel[prop][key]);
channelList.push({ channelList.push({
channelName: key, channelName: el,
joinUser: Channel[prop][key], joinUser: Channel[prop][el],
}); });
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment