CreateRoom.js 3.79 KB
Newer Older
우지원's avatar
e    
우지원 committed
1
import { useEffect, useState } from "react";
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
2
import { Redirect } from "react-router-dom";
우지원's avatar
e    
우지원 committed
3
import roomApi from "../../apis/room.api";
Kim, Chaerin's avatar
merge19    
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
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const INIT_ROOM = {
    name: '',
    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) {
Kim, Chaerin's avatar
Kim, Chaerin committed
24
25
26
27
28
29
30
        const { name, value, files } = event.target;
        if (files) {
            setRoom({ ...room, [name]: files[0] })
        } else {
            setRoom({ ...room, [name]: value })
        }
        console.log(room)
우지원's avatar
e    
우지원 committed
31
32
33
34
    }

    async function handleSubmit(e) {
        e.preventDefault()
Kim, Chaerin's avatar
Kim, Chaerin committed
35
36
37
38
39
40
        let formData = new FormData();
        console.log('profileimg:', room.profileimg)
        console.log('name:', room.name)
        formData.append("name", room.name);
        formData.append("userId", id);
        formData.append("profileimg", room.profileimg);
우지원's avatar
e    
우지원 committed
41
        try {
Kim, Chaerin's avatar
Kim, Chaerin committed
42
43
            const res = await roomApi.create(formData)
            console.log(res)
우지원's avatar
e    
우지원 committed
44
            const Id = res.id
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
45
46
47
            console.log(Id)
            alert(`방참여코드는 ${Id}입니다`)
            setSuccess(true)
우지원's avatar
e    
우지원 committed
48
        } catch (error) {
Kim, Chaerin's avatar
merge19    
Kim, Chaerin committed
49
            catchErrors(error, setError);
우지원's avatar
e    
우지원 committed
50
51
52
53
54
55
        } finally {
            // setLoading(false);
        }
    }

    if (success) {
Kim, Chaerin's avatar
Kim, Chaerin committed
56
        // console.log('success', success)
우지원's avatar
e    
우지원 committed
57
        alert('룸생성이 완료되었습니다!')
Kim, Chaerin's avatar
Kim, Chaerin committed
58
59
        window.location.href=`/user/${id}`
        // return <Redirect to={`/user/${id}`} />
우지원's avatar
e    
우지원 committed
60
61
62
63
64
65
66
67
    }

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

    return (
        <div className="modal-content" >
            <form onSubmit={handleSubmit}>
                <div className="modal-header">
Kim, Chaerin's avatar
Kim, Chaerin committed
68
69
70
                    <div className="modal-title" id="makeModal">
                        방생성하기
                    </div>
우지원's avatar
e    
우지원 committed
71
72
73
74
75
76
77
78
                    <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
79
                    {error && <div className="alert alert-danger">{error}</div>}
우지원's avatar
e    
우지원 committed
80
81
82
83
84
85
86
87
                    <h6>방프로필사진</h6>
                    <div className="mb-4">
                        <input
                            type="file"
                            className="upload-hidden"
                            onChange={handleChange}
                            accept="image/*"
                            name="profileimg"
Kim, Chaerin's avatar
Kim, Chaerin committed
88
                        />
우지원's avatar
e    
우지원 committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
                    </div>
                    <h6>방이름</h6>
                    <div className="input-group">
                        <input
                            type="text"
                            className="form-control"
                            placeholder="방이름을 입력해주세요"
                            aria-label="방이름 입력해주세요"
                            aria-describedby="basic-addon1"
                            onChange={handleChange}
                            name="name"
                        />
                    </div>
                    <div className="modal-footer">
                        <button type="submit" className="btn btn-primary">
                            생성
                        </button>
                    </div>
                </div>
            </form>
        </div >
    );
};

export default CreateRoom;