ChannelSingle.js 3.11 KB
Newer Older
1
2
3
4
5
import { useState, useEffect } from 'react'
import { Link, Redirect, useParams } from 'react-router-dom'
import roomApi from '../../apis/room.api';
import userApi from '../../apis/user.api'
import catchErrors from "../../context/catchError";
우지원's avatar
우지원 committed
6

Kim, Chaerin's avatar
Kim, Chaerin committed
7
const ChannelSingle = (props) => {
8
9
10
11
  const [error, setError] = useState("");
  const [succes, setSucces] = useState(false);
  const [roomName, setRoomName] = useState('');
  const { roomId, channelId } = useParams();
우지원's avatar
우지원 committed
12
13
  console.log('props', props.channel)
  console.log('hi', channelId)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  const userId = localStorage.getItem('user')


  async function joinChannel(e) {
    console.log(e, userId)
    try {
      const data = await userApi.getUser(userId);
      const key = indexCheck(e)
      console.log(key)
      const mem = props.channel[key].joinUser
      console.log(mem)
      const joinCh = mem.includes(data.name);
      console.log(joinCh)
      if (!joinCh) {
        const roomA = await roomApi.joinChannel({ roomId: roomId, channelName: e, plusUser: data.name, index: key })
        console.log(roomA)
        setRoomName(e)
        setSucces(true)
      } else {
        alert('이미 참여된 채널입니다.')
      }
    } catch (error) {
      catchErrors(error, setError);
    }
  }
  
  function indexCheck(e) {
    for (const key in props.channel) {
      console.log('체크', props.channel[key].channelName, e)
      console.log('체크', props.channel[key].channelName === e)
      if (props.channel[key].channelName === e) {
        console.log('key', key)
        return key
      }
    }
  }

  if(succes){
    alert(`${roomName} 채널에 참가되었습니다.`)
    window.location.href=`/room/${roomId}/${roomName}`
  }

Kim, Chaerin's avatar
Kim, Chaerin committed
56
57
  return (
    <div>
우지원's avatar
우지원 committed
58
      <div className="overflow-auto" style={{ height: '610px' }}>
Kim, Chaerin's avatar
Kim, Chaerin committed
59
60
        {props.channel.map((el) => (
          <div className="mb-3">
우지원's avatar
우지원 committed
61
            <Link to={`/room/${roomId}/${el.channelName}`}>
Kim, Chaerin's avatar
Kim, Chaerin committed
62
63
              <div
                className="m-3 p-1 row"
우지원's avatar
우지원 committed
64
                style={{ backgroundColor: '#E0CEE8' }}
65
                onClick={() => joinChannel(el.channelName)}
Kim, Chaerin's avatar
Kim, Chaerin committed
66
              >
우지원's avatar
우지원 committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
                {el.channelName === channelId ? (
                  <img
                    className="col-auto mt-2"
                    src="/fullSpeaker.png"
                    width="25px"
                    height="25px"
                  />
                ) : (
                  <img
                    className="col-auto mt-2"
                    src="/emptySpeaker.png"
                    width="25px"
                    height="25px"
                  />
                )}
                <h5 className="col mt-2" style={{ color: 'black' }}>
Kim, Chaerin's avatar
Kim, Chaerin committed
83
84
                  {el.channelName}
                </h5>
우지원's avatar
우지원 committed
85
              </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
86
            </Link>
우지원's avatar
우지원 committed
87

88
89
            {el.joinUser &&
              el.joinUser.map((e) => (
Kim, Chaerin's avatar
Kim, Chaerin committed
90
                <div>
우지원's avatar
우지원 committed
91
                  <ul className="mx-5" style={{ color: '#76D079' }}>
Kim, Chaerin's avatar
Kim, Chaerin committed
92
                    <li>
우지원's avatar
우지원 committed
93
                      <p style={{ color: 'black' }}>{e}</p>
Kim, Chaerin's avatar
Kim, Chaerin committed
94
95
96
97
98
99
                    </li>
                  </ul>
                </div>
              ))}
          </div>
        ))}
seoyeon's avatar
seoyeon committed
100
101
      </div>
    </div>
우지원's avatar
우지원 committed
102
103
  )
}
Kim, Chaerin's avatar
Kim, Chaerin committed
104

우지원's avatar
우지원 committed
105
export default ChannelSingle