ChannelList.js 2.14 KB
Newer Older
1
2
import { Link, useParams } from 'react-router-dom'
import React, { useEffect, useState } from 'react';
우지원's avatar
e    
우지원 committed
3
import RightHamburger from './RightHamburger';
4
5
6
7
8
9
10
11
12
import roomApi from '../../apis/room.api';
import catchErrors from '../../context/catchError';
import userApi from '../../apis/user.api';

const INIT_CHANNEL = {
  channelName: "",
  joinUser: [],
};

Kim, Chaerin's avatar
Kim, Chaerin committed
13
14

const ChannelList = () => {
15
16
17
  const { roomId } = useParams();
  const [error, setError] = useState("");
  const [channel, setChannel] = useState([INIT_CHANNEL]);
Kim, Chaerin's avatar
Kim, Chaerin committed
18
19
  const id = localStorage.getItem('user');

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  async function getChannel(roomId) {
    try {
      const data = await roomApi.getRoom([roomId]);
      const Channel = data[0].channel;
      const channelList = [];
      for (const prop in Channel) {
        for (const el in Channel[prop]) {
          channelList.push({
            channelName: el,
            joinUser: Channel[prop][el],
          });
        }
      }
      setChannel(channelList);
    } catch (error) {
      catchErrors(error, setError);
    }
  }

  async function exitChannel() {
    try {
      const data = await userApi.getUser(id);
      const A = doubleJoinCheck(data.name)
      if (A) {
        await roomApi.doubleJoin({ roomId: roomId, index1: A.index1, index2: A.index2, joinChName: A.joinChName })
      }
    } catch (error) {
      catchErrors(error, setError);
    }
  }

  function doubleJoinCheck(e) {
    for (const index in channel) {
      for (const el in channel[index].joinUser) {
        if (channel[index].joinUser[el] === e) {
          const doublejoinCh = channel[index].channelName
          const A = {
            index1: index,
            index2: el,
            joinChName: doublejoinCh,
          }
          return A
        }
      }
    }
  }

  useEffect(() => {
    getChannel(roomId);
  }, [roomId])

Kim, Chaerin's avatar
Kim, Chaerin committed
71
72
  return (
    <div>
Kim, Chaerin's avatar
Kim, Chaerin committed
73
74
      <nav className="navbar navbar-light ">
        <div className="col-2"></div>
75
        <div onClick={exitChannel}>
Kim, Chaerin's avatar
Kim, Chaerin committed
76
          <Link to={`/user/${id}`}>
seoyeon's avatar
seoyeon committed
77
            <img src="/BORA.png" style={{ width: '160px' }} />
우지원's avatar
우지원 committed
78
79
          </Link>
        </div>
우지원's avatar
e    
우지원 committed
80
        <RightHamburger />
우지원's avatar
우지원 committed
81
      </nav>
seoyeon's avatar
seoyeon committed
82
83
84
    </div>
  )
}
우지원's avatar
우지원 committed
85

seoyeon's avatar
seoyeon committed
86
export default ChannelList