RoomSingle.js 2.22 KB
Newer Older
이재연's avatar
이재연 committed
1
2
3
4
5
import { useEffect, useState } from "react";
import { Link, 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

이재연's avatar
이재연 committed
7
8
9
10
11
12
13
const id = localStorage.getItem("user");
const INIT_ROOM = {
  roomId:"",
  name: "",
  profileimg: "",
  member: "",
};
Kim, Chaerin's avatar
Kim, Chaerin committed
14
const RoomSingle = () => {
이재연's avatar
이재연 committed
15
16
17
18
  const [room, setRoom] = useState([INIT_ROOM]);
  const [error, setError] = useState("");
  const channelId = 'main';
  const {roomId}=useParams(room.roomId);
이재연's avatar
이재연 committed
19
20
  async function getJoinRoom(Id) {
    try {
이재연's avatar
이재연 committed
21
22
23
24
25
26
27
28
29
30
31
32
33
      const User = await userApi.getUser(Id);
      const RoomNumArr = User.roomNumber;
      const Room = await roomApi.getRoom(RoomNumArr);
      let roomlist = [];
      for (let prop in Room) {
        roomlist.push({
          roomId:Room[prop].id,
          name: Room[prop].name,
          profileimg: Room[prop].profileimg,
          member: Room[prop].member.length,
        });
      }
      setRoom(roomlist);
이재연's avatar
이재연 committed
34
    } catch (error) {
이재연's avatar
이재연 committed
35
      catchErrors(error, setError);
이재연's avatar
이재연 committed
36
37
38
    }
  }
  useEffect(() => {
이재연's avatar
이재연 committed
39
40
41
    getJoinRoom(id);
  }, [id]);
  console.log(room)
우지원's avatar
우지원 committed
42

이재연's avatar
이재연 committed
43
  const { profileimg } = room;
Kim, Chaerin's avatar
Kim, Chaerin committed
44
  return (
이재연's avatar
이재연 committed
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
71
72
73
74
75
76
77
78
79
80
    <div>
      {room &&
        room.map((el) => (
          <Link
            to={`/room/${el.roomId}/${channelId}`}
            className="text-decoration-none text-dark"
          >
            <div
              className="d-flex mx-4 my-2 p-2"
              style={{ backgroundColor: "#C4C4C4" }}
            >
              <div style={{ width: "37px", height: "37px" }}>
                <img
                  src={`/roomUploads/${el.profileimg}`}
                  className="rounded-circle"
                  style={{ width: "37px", height: "37px" }}
                />
              </div>
              <div
                className="mx-3 mt-2"
                style={{
                  width: "250px",
                  overflow: "scroll",
                  whiteSpace: "nowrap",
                  msOverflowStyle: "none",
                }}
              >
                {el.name}
              </div>
              <div className="ms-auto mt-2"> {el.member}/100 </div>
            </div>
          </Link>
        ))}
    </div>
  );
};
Kim, Chaerin's avatar
Kim, Chaerin committed
81

이재연's avatar
이재연 committed
82
export default RoomSingle;