CreateRoom.js 3.3 KB
Newer Older
우지원's avatar
e    
우지원 committed
1
import { useEffect, useState } from "react";
이재연's avatar
aaa    
이재연 committed
2
import { Redirect, useParams } from "react-router-dom";
우지원's avatar
e    
우지원 committed
3
import roomApi from "../../apis/room.api";
우지원's avatar
0727    
우지원 committed
4
import catchErrors from "../../context/catchError";
우지원's avatar
e    
우지원 committed
5
6
7
8

const userId = localStorage.getItem('user');
const INIT_ROOM = {
    name: '',
우지원's avatar
0726    
우지원 committed
9
10
    owner: userId,
    member: userId,
우지원's avatar
e    
우지원 committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    profileimg: '',
}

const CreateRoom = () => {
    const [room, setRoom] = useState(INIT_ROOM)
    const [error, setError] = useState("");
    const [success, setSuccess] = useState(false)
    const [disabled, setDisabled] = useState(true)

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

    function handleChange(event) {
        const { name, value } = event.target
        setRoom({ ...room, [name]: value })
    }

    async function handleSubmit(e) {
        e.preventDefault()
        try {
            const res = await roomApi.create(room)
            const Id = res.id
우지원's avatar
0726    
우지원 committed
35
36
37
            console.log(Id)
            alert(`방참여코드는 ${Id}입니다`)
            setSuccess(true)
우지원's avatar
e    
우지원 committed
38
        } catch (error) {
우지원's avatar
0727    
우지원 committed
39
            catchErrors(error, setError);
우지원's avatar
e    
우지원 committed
40
41
42
43
44
45
46
47
        } finally {
            // setLoading(false);
        }
    }

    if (success) {
        console.log('success', success)
        alert('룸생성이 완료되었습니다!')
이재연's avatar
이재연 committed
48
        window.location.href='/user'
우지원's avatar
e    
우지원 committed
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    }

    const { name, owner, member, profileimg } = room;

    return (
        <div className="modal-content" >
            <form onSubmit={handleSubmit}>
                <div className="modal-header">
                    <div className="modal-title" id="makeModal">방생성하기</div>
                    <button
                        type="button"
                        className="btn-close"
                        data-bs-dismiss="modal"
                        aria-label="Close"
                    ></button>
                </div>
                <div className="modal-body">
                    <h6>방프로필사진</h6>
                    <div className="mb-4">
                        <input
                            type="file"
                            className="upload-hidden"
                            onChange={handleChange}
                            accept="image/*"
                            name="profileimg"
                            value={room.profileimg} />
                    </div>
                    <h6>방이름</h6>
                    <div className="input-group">
                        <input
                            type="text"
                            className="form-control"
                            placeholder="방이름을 입력해주세요"
                            aria-label="방이름 입력해주세요"
                            aria-describedby="basic-addon1"
                            onChange={handleChange}
                            name="name"
                            value={room.name}
                        />
                    </div>
                    <div className="modal-footer">
                        <button type="submit" className="btn btn-primary">
                            생성
                        </button>
                    </div>
                </div>
            </form>
        </div >
    );
};

export default CreateRoom;