import { useState } from 'react' import { Link, useParams } from 'react-router-dom' import roomApi from '../../apis/room.api'; import userApi from '../../apis/user.api' import catchErrors from "../../context/catchError"; const ChannelSingle = (props) => { const [error, setError] = useState(""); const [userName, setUserName] = useState(''); const [success, setSuccess] = useState(false); const [roomName, setRoomName] = useState(''); const { roomId, channelId } = useParams(); console.log('props', props.channel) console.log('hi', channelId) const userId = localStorage.getItem('user') async function joinChannel(e) { console.log(e, userId) try { const data = await userApi.getUser(userId); setUserName(data.name) const index1 = indexCheck(e) const A = doubleJoinCheck(data.name) console.log(A) const mem = props.channel[index1].joinUser const joinCh = mem.includes(data.name); console.log(A) if (!joinCh) { if (A) { console.log('더블더블') 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 }) setRoomName(e) setSuccess(true) } else { alert('이미 참여된 채널입니다.') } } catch (error) { catchErrors(error, setError); } } console.log('정보들어왔나', roomName, success) function indexCheck(e) { for (const index1 in props.channel) { if (props.channel[index1].channelName === e) { return index1 } } } function doubleJoinCheck(e) { for (const index in props.channel) { for (const el in props.channel[index].joinUser) { if (props.channel[index].joinUser[el] === e) { const doublejoinCh = props.channel[index].channelName const A = { index1: index, index2: el, joinChName: doublejoinCh, } return A } } } } if (success) { alert(`${roomName} 채널에 참가되었습니다.`) window.location.href = `/room/${roomId}/${roomName}` } return (
{props.channel.map((el) => (
joinChannel(el.channelName)} > {el.channelName === channelId ? ( ) : ( )}
{el.channelName}
{el.joinUser && el.joinUser.map((e) => (
  • {e}

))}
))}
) } export default ChannelSingle