JoinRoom.js 2.22 KB
Newer Older
우지원's avatar
e    
우지원 committed
1
2
3
import { useEffect, useState } from "react";
import { Redirect } from "react-router-dom";
import roomApi from "../../apis/room.api";
이재연's avatar
x    
이재연 committed
4
import catchErrors from "../../context/catchError";
우지원's avatar
e    
우지원 committed
5

우지원's avatar
e    
우지원 committed
6
const id = localStorage.getItem("user");
우지원's avatar
e    
우지원 committed
7
8

const JoinRoom = () => {
이재연's avatar
x    
이재연 committed
9
10
11
12
  const [roomId, setRoomId] = useState("");
  const [disabled, setDisabled] = useState(true);
  const [error, setError] = useState("");
  const [success, setSuccess] = useState(false);
우지원's avatar
e    
우지원 committed
13

이재연's avatar
x    
이재연 committed
14
15
16
17
  useEffect(() => {
    const isRoom = Object.values(roomApi).every((el) => Boolean(el));
    isRoom ? setDisabled(false) : setDisabled(true);
  }, [roomId]);
우지원's avatar
e    
우지원 committed
18

이재연's avatar
x    
이재연 committed
19
20
21
22
  function handleChange(event) {
    const { value } = event.target;
    setRoomId(value);
  }
우지원's avatar
e    
우지원 committed
23

이재연's avatar
x    
이재연 committed
24
25
26
27
28
  async function handleSubmit(e) {
    e.preventDefault();
    try {
      // setLoading(true);
      setError("");
우지원's avatar
e    
우지원 committed
29
      const data = await roomApi.join({ userId: id, roomId: roomId });
이재연's avatar
x    
이재연 committed
30
31
32
33
34
      setSuccess(true);
    } catch (error) {
      catchErrors(error, setError);
    } finally {
      // setLoading(false);
우지원's avatar
e    
우지원 committed
35
    }
이재연's avatar
x    
이재연 committed
36
  }
우지원's avatar
e    
우지원 committed
37

이재연's avatar
x    
이재연 committed
38
  if (success) {
우지원's avatar
e    
우지원 committed
39
40
41
    // console.log('success', success)
    alert('룸참여가 완료되었습니다!')
    return <Redirect to={`/user/${id}`} />;
이재연's avatar
x    
이재연 committed
42
  }
우지원's avatar
e    
우지원 committed
43

이재연's avatar
x    
이재연 committed
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
71
72
73
74
75
  return (
    <div className="modal-content">
      <form onSubmit={handleSubmit}>
        <div className="modal-header">
          <div className="modal-title" id="joinModal">
            방참여하기
          </div>
          <button
            type="button"
            className="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div className="modal-body">
          <div className="input-group mb-3">
            <input
              type="text"
              className="form-control"
              placeholder="참여코드를 입력하세요"
              aria-label="참여코드를 입력하세요"
              aria-describedby="basic-addon1"
              name="roomId"
              value={roomId}
              onChange={handleChange}
            />
          </div>
          <div className="modal-footer">
            <button type="submit" className="btn btn-primary">
              확인
            </button>
          </div>
우지원's avatar
e    
우지원 committed
76
        </div>
이재연's avatar
x    
이재연 committed
77
78
79
      </form>
    </div>
  );
우지원's avatar
e    
우지원 committed
80
81
};

이재연's avatar
x    
이재연 committed
82
export default JoinRoom;