ChannelSingle.js 3.14 KB
Newer Older
우지원's avatar
0805    
우지원 committed
1
2
import { useState } from 'react'
import { Link, useParams } from 'react-router-dom'
3
4
5
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
  const [error, setError] = useState("");
우지원's avatar
0805    
우지원 committed
9
  const [success, setSuccess] = useState(false);
10
11
  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
  const userId = localStorage.getItem('user')


  async function joinChannel(e) {
    console.log(e, userId)
    try {
      const data = await userApi.getUser(userId);
우지원's avatar
0805    
우지원 committed
21
22
23
      const el = indexCheck(e)
      console.log(el)
      const mem = props.channel[el].joinUser
24
25
26
27
      console.log(mem)
      const joinCh = mem.includes(data.name);
      console.log(joinCh)
      if (!joinCh) {
우지원's avatar
0805    
우지원 committed
28
        const roomA = await roomApi.joinChannel({ roomId: roomId, channelName: e, plusUser: data.name, index: el })
29
30
        console.log(roomA)
        setRoomName(e)
우지원's avatar
0805    
우지원 committed
31
        setSuccess(true)
32
33
34
35
36
37
38
      } else {
        alert('이미 참여된 채널입니다.')
      }
    } catch (error) {
      catchErrors(error, setError);
    }
  }
우지원's avatar
0805    
우지원 committed
39
40
41

  console.log('정보들어왔나', roomName, success)

42
  function indexCheck(e) {
우지원's avatar
0805    
우지원 committed
43
44
45
46
47
48
    for (const el in props.channel) {
      console.log('체크', props.channel[el].channelName, e)
      console.log('체크', props.channel[el].channelName === e)
      if (props.channel[el].channelName === e) {
        console.log('el', el)
        return el
49
50
51
52
      }
    }
  }

우지원's avatar
0805    
우지원 committed
53
  if (success) {
54
    alert(`${roomName} 채널에 참가되었습니다.`)
우지원's avatar
0805    
우지원 committed
55
    window.location.href = `/room/${roomId}/${roomName}`
56
57
  }

Kim, Chaerin's avatar
Kim, Chaerin committed
58
59
  return (
    <div>
우지원's avatar
우지원 committed
60
      <div className="overflow-auto" style={{ height: '610px' }}>
Kim, Chaerin's avatar
Kim, Chaerin committed
61
62
        {props.channel.map((el) => (
          <div className="mb-3">
우지원's avatar
우지원 committed
63
            <Link to={`/room/${roomId}/${el.channelName}`}>
Kim, Chaerin's avatar
Kim, Chaerin committed
64
65
              <div
                className="m-3 p-1 row"
우지원's avatar
우지원 committed
66
                style={{ backgroundColor: '#E0CEE8' }}
67
                onClick={() => joinChannel(el.channelName)}
Kim, Chaerin's avatar
Kim, Chaerin committed
68
              >
우지원's avatar
우지원 committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
                {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
85
86
                  {el.channelName}
                </h5>
우지원's avatar
우지원 committed
87
              </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
88
            </Link>
우지원's avatar
우지원 committed
89

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

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