ChannelSingle.js 2.76 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
12
13
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
  const [error, setError] = useState("");
  const [succes, setSucces] = useState(false);
  const [roomName, setRoomName] = useState('');
  const { roomId, channelId } = useParams();
  const userId = localStorage.getItem('user')


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

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

Kim, Chaerin's avatar
Kim, Chaerin committed
46
47
  return (
    <div>
우지원's avatar
우지원 committed
48
      <div className="overflow-auto" style={{ height: '610px' }}>
Kim, Chaerin's avatar
Kim, Chaerin committed
49
50
        {props.channel.map((el) => (
          <div className="mb-3">
우지원's avatar
우지원 committed
51
            <Link to={`/room/${roomId}/${el.channelName}`}>
Kim, Chaerin's avatar
Kim, Chaerin committed
52
53
              <div
                className="m-3 p-1 row"
우지원's avatar
우지원 committed
54
                style={{ backgroundColor: '#E0CEE8' }}
55
                onClick={() => joinChannel(el.channelName)}
Kim, Chaerin's avatar
Kim, Chaerin committed
56
              >
우지원's avatar
우지원 committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
                {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
73
74
                  {el.channelName}
                </h5>
우지원's avatar
우지원 committed
75
              </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
76
            </Link>
우지원's avatar
우지원 committed
77

78
79
            {el.joinUser &&
              el.joinUser.map((e) => (
Kim, Chaerin's avatar
Kim, Chaerin committed
80
                <div>
우지원's avatar
우지원 committed
81
                  <ul className="mx-5" style={{ color: '#76D079' }}>
Kim, Chaerin's avatar
Kim, Chaerin committed
82
                    <li>
우지원's avatar
우지원 committed
83
                      <p style={{ color: 'black' }}>{e}</p>
Kim, Chaerin's avatar
Kim, Chaerin committed
84
85
86
87
88
89
                    </li>
                  </ul>
                </div>
              ))}
          </div>
        ))}
seoyeon's avatar
seoyeon committed
90
91
      </div>
    </div>
우지원's avatar
우지원 committed
92
93
  )
}
Kim, Chaerin's avatar
Kim, Chaerin committed
94

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