JoinRoom.js 2.88 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";
Kim, Chaerin's avatar
Kim, Chaerin committed
4
import catchErrors from "../../context/catchError";
우지원's avatar
e    
우지원 committed
5

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

const JoinRoom = () => {
Kim, Chaerin's avatar
Kim, Chaerin committed
9
    const [roomId, setRoomId] = useState("");
우지원's avatar
e    
우지원 committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    const [disabled, setDisabled] = useState(true);
    const [error, setError] = useState("");
    const [success, setSuccess] = useState(false);

    useEffect(() => {
        const isRoom = Object.values(roomApi).every((el) => Boolean(el));
        isRoom ? setDisabled(false) : setDisabled(true);
    }, [roomId]);

    function handleChange(event) {
        const { value } = event.target;
        setRoomId(value);
    }

    async function handleSubmit(e) {
        e.preventDefault();
        try {
            // setLoading(true);
Kim, Chaerin's avatar
Kim, Chaerin committed
28
29
30
            setError("");
            const data = await roomApi.join({ userId: id, roomId: roomId });
            console.log('서버연결됬나요', data)
우지원's avatar
e    
우지원 committed
31
32
            setSuccess(true);
        } catch (error) {
Kim, Chaerin's avatar
Kim, Chaerin committed
33
            catchErrors(error, setError);
우지원's avatar
e    
우지원 committed
34
35
36
37
        } finally {
            // setLoading(false);
        }
    }
Kim, Chaerin's avatar
Kim, Chaerin committed
38

우지원's avatar
e    
우지원 committed
39
    if (success) {
Kim, Chaerin's avatar
Kim, Chaerin committed
40
41
42
        // console.log('success', success)
        alert('룸참여가 완료되었습니다!')
        window.location.href=`/room/${roomId}/1`
우지원's avatar
e    
우지원 committed
43
44
45
46
47
48
    }

    return (
        <div className="modal-content">
            <form onSubmit={handleSubmit}>
                <div className="modal-header">
Kim, Chaerin's avatar
Kim, Chaerin committed
49
50
51
                    <div className="modal-title" id="joinModal">
                        방참여하기
                    </div>
우지원's avatar
e    
우지원 committed
52
53
54
55
56
57
58
59
                    <button
                        type="button"
                        className="btn-close"
                        data-bs-dismiss="modal"
                        aria-label="Close"
                    ></button>
                </div>
                <div className="modal-body">
Kim, Chaerin's avatar
Kim, Chaerin committed
60
                    {error && <div className="alert alert-danger">{error}</div>}
우지원's avatar
e    
우지원 committed
61
62
63
64
65
66
67
68
69
70
71
72
73
                    <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">
Kim, Chaerin's avatar
Kim, Chaerin committed
74
75
76
                        <button
                            type="submit"
                            className="btn btn-primary">
우지원's avatar
e    
우지원 committed
77
78
79
80
81
82
83
84
85
                            확인
                        </button>
                    </div>
                </div>
            </form>
        </div>
    );
};

Kim, Chaerin's avatar
Kim, Chaerin committed
86
export default JoinRoom;