ChannelSingle.js 3.32 KB
Newer Older
우지원's avatar
0805    
우지원 committed
1
import { useState } from 'react'
우지원's avatar
우지원 committed
2
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
  console.log(props.channel);
9
  const [error, setError] = useState("");
우지원's avatar
0805    
우지원 committed
10
  const [success, setSuccess] = useState(false);
11
12
13
14
15
16
17
18
19
  const [roomName, setRoomName] = useState('');
  const { roomId, channelId } = useParams();
  const userId = localStorage.getItem('user')


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

39
  function indexCheck(e) {
우지원's avatar
우지원 committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    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
        }
59
60
61
62
      }
    }
  }

우지원's avatar
0805    
우지원 committed
63
  if (success) {
64
    alert(`${roomName} 채널에 참가되었습니다.`)
우지원's avatar
0805    
우지원 committed
65
    window.location.href = `/room/${roomId}/${roomName}`
66
67
  }

Kim, Chaerin's avatar
Kim, Chaerin committed
68
69
  return (
    <div>
우지원's avatar
우지원 committed
70
      <div className="overflow-auto" style={{ height: '610px' }}>
Kim, Chaerin's avatar
Kim, Chaerin committed
71
72
        {props.channel.map((el,i) => (
          <div className="mb-3" key={`channel${i}`}>
우지원's avatar
우지원 committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
            <div
              className="m-3 p-1 row"
              style={{ backgroundColor: '#E0CEE8' }}
              onClick={() => joinChannel(el.channelName)}
            >
              {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' }}>
                {el.channelName}
              </h5>
            </div>
우지원's avatar
우지원 committed
97

우지원's avatar
우지원 committed
98
99
            {el.joinUser &&
              el.joinUser.map((e,i) => (
Kim, Chaerin's avatar
Kim, Chaerin committed
100
                <div key={`member${i}`}>
우지원's avatar
우지원 committed
101
                  <ul className="mx-5" style={{ color: '#76D079' }}>
Kim, Chaerin's avatar
Kim, Chaerin committed
102
                    <li>
우지원's avatar
우지원 committed
103
                      <p style={{ color: 'black' }}>{e}</p>
Kim, Chaerin's avatar
Kim, Chaerin committed
104
105
106
107
108
109
                    </li>
                  </ul>
                </div>
              ))}
          </div>
        ))}
seoyeon's avatar
seoyeon committed
110
111
      </div>
    </div>
우지원's avatar
우지원 committed
112
113
  )
}
Kim, Chaerin's avatar
Kim, Chaerin committed
114

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