ChannelSingle.js 3.89 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
우지원 committed
9
  const [userName, setUserName] = useState('');
우지원's avatar
0805    
우지원 committed
10
  const [success, setSuccess] = useState(false);
11
12
  const [roomName, setRoomName] = useState('');
  const { roomId, channelId } = useParams();
우지원's avatar
우지원 committed
13
14
  console.log('props', props.channel)
  console.log('hi', channelId)
15
16
17
18
19
20
21
  const userId = localStorage.getItem('user')


  async function joinChannel(e) {
    console.log(e, userId)
    try {
      const data = await userApi.getUser(userId);
우지원's avatar
우지원 committed
22
23
24
25
26
      setUserName(data.name)
      const index1 = indexCheck(e)
      const A = doubleJoinCheck(data.name)
      console.log(A)
      const mem = props.channel[index1].joinUser
27
      const joinCh = mem.includes(data.name);
우지원's avatar
우지원 committed
28
      console.log(A)
29
      if (!joinCh) {
우지원's avatar
우지원 committed
30
31
32
33
34
35
36
37
38
        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 })
39
        setRoomName(e)
우지원's avatar
0805    
우지원 committed
40
        setSuccess(true)
41
42
43
44
45
46
47
      } else {
        alert('이미 참여된 채널입니다.')
      }
    } catch (error) {
      catchErrors(error, setError);
    }
  }
우지원's avatar
0805    
우지원 committed
48
49
50

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

51
  function indexCheck(e) {
우지원's avatar
우지원 committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    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
        }
71
72
73
74
      }
    }
  }

우지원's avatar
0805    
우지원 committed
75
  if (success) {
76
    alert(`${roomName} 채널에 참가되었습니다.`)
우지원's avatar
0805    
우지원 committed
77
    window.location.href = `/room/${roomId}/${roomName}`
78
79
  }

Kim, Chaerin's avatar
Kim, Chaerin committed
80
81
  return (
    <div>
우지원's avatar
우지원 committed
82
      <div className="overflow-auto" style={{ height: '610px' }}>
Kim, Chaerin's avatar
Kim, Chaerin committed
83
84
        {props.channel.map((el) => (
          <div className="mb-3">
우지원's avatar
우지원 committed
85
            <Link to={`/room/${roomId}/${el.channelName}`}>
Kim, Chaerin's avatar
Kim, Chaerin committed
86
87
              <div
                className="m-3 p-1 row"
우지원's avatar
우지원 committed
88
                style={{ backgroundColor: '#E0CEE8' }}
89
                onClick={() => joinChannel(el.channelName)}
Kim, Chaerin's avatar
Kim, Chaerin committed
90
              >
우지원's avatar
우지원 committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
                {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
107
108
                  {el.channelName}
                </h5>
우지원's avatar
우지원 committed
109
              </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
110
            </Link>
우지원's avatar
우지원 committed
111

112
113
            {el.joinUser &&
              el.joinUser.map((e) => (
Kim, Chaerin's avatar
Kim, Chaerin committed
114
                <div>
우지원's avatar
우지원 committed
115
                  <ul className="mx-5" style={{ color: '#76D079' }}>
Kim, Chaerin's avatar
Kim, Chaerin committed
116
                    <li>
우지원's avatar
우지원 committed
117
                      <p style={{ color: 'black' }}>{e}</p>
Kim, Chaerin's avatar
Kim, Chaerin committed
118
119
120
121
122
123
                    </li>
                  </ul>
                </div>
              ))}
          </div>
        ))}
seoyeon's avatar
seoyeon committed
124
125
      </div>
    </div>
우지원's avatar
우지원 committed
126
127
  )
}
Kim, Chaerin's avatar
Kim, Chaerin committed
128

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