Roomsettingchange.js 4.65 KB
Newer Older
권병윤's avatar
0806    
권병윤 committed
1
2
3
4
5
6
7
8
import { useParams } from "react-router-dom";
import React, { useEffect, useState } from "react";
import RoomApi from "../../apis/room.api";
import catchErrors from "../../context/catchError";

const INIT_Room = {
  id: "",
  name: "",
권병윤's avatar
0807    
권병윤 committed
9
  profileimg: "",
권병윤's avatar
0806    
권병윤 committed
10
11
};

12
const RoomSettingChange = () => {
권병윤's avatar
0806    
권병윤 committed
13
14
15
16
17
18
19
20
  const { roomId } = useParams();
  const [Room, setRoom] = useState(INIT_Room);
  const [error, setError] = useState("");

  Room.id = roomId;

  async function getdata(Roomdata) {
    try {
권병윤's avatar
0807    
권병윤 committed
21
      const data = await RoomApi.getRoom([Roomdata]);
권병윤's avatar
0806    
권병윤 committed
22
23
24
25
26
27
28
29
30
31
32
33
34
      setRoom(data);
      console.log(data);
    } catch (error) {
      catchErrors(error, setError);
    }
  }
  useEffect(() => {
    getdata(roomId);
  }, []);

  const updateinfo = (event) => {
    const { name, value } = event.target;
    setRoom({ ...Room, [name]: value });
35
    console.log(Room.name);
권병윤's avatar
0807    
권병윤 committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  };

  const handleChange = async (event) => {
    const { files } = event.target;
    console.log("files:", files);
    let formData = new FormData();
    formData.append("profileimg", files[0]);
    formData.append("id", roomId);
    try {
      const res = await RoomApi.profileimg(formData);
      // if (files) {
      //   setRoom({ ...Room, profileimg: res });
      // } else {
      //   setRoom();
      // }
    } catch (error) {
      catchErrors(error, setError);
    }
권병윤's avatar
0806    
권병윤 committed
54
55
56
  };

  const changeinfo = async (event) => {
권병윤's avatar
0807    
권병윤 committed
57
58
    window.location.reload();
    // window.location.href = window.location.href;
권병윤's avatar
0806    
권병윤 committed
59
60
61
62
    const req = await RoomApi.changename(Room);
  };

  return (
63
    <div>
권병윤's avatar
0806    
권병윤 committed
64
65
66
67
68
69
70
71
72
73
74
75
76
      <button
        type="submit"
        data-bs-toggle="modal"
        data-bs-target="#changeRoomname"
        style={{
          weight: "100px",
          height: "60px",
          backgroundColor: "#f5cfe3",
          borderRadius: "5px",
          color: "black",
          border: "1px #f5cfe3",
        }}
      >
77
         설정 변경
권병윤's avatar
0806    
권병윤 committed
78
79
80
81
82
83
84
85
86
87
88
89
      </button>

      <div
        className="modal fade"
        id="changeRoomname"
        tabIndex="-1"
        aria-labelledby="changeRoomnameLabel"
        aria-hidden="true"
      >
        <div className="modal-dialog">
          <div className="modal-content">
            <div className="modal-header">
권병윤's avatar
0807    
권병윤 committed
90
91
92
              <div className="modal-title" id="changeRoomsetting">
                 설정 변경
              </div>
권병윤's avatar
0806    
권병윤 committed
93
94
95
96
97
98
99
100
              <button
                type="button"
                className="btn-close"
                data-bs-dismiss="modal"
                aria-label="Close"
              ></button>
            </div>
            <div className="row mb-3">
권병윤's avatar
0807    
권병윤 committed
101
102
              <div
                className="d-flex justify-content-center"
103
                style={{ marginTop: "20px" }}
권병윤's avatar
0807    
권병윤 committed
104
105
106
107
108
109
110
111
112
113
114
115
116
              >
                <input
                  type="file"
                  className="upload-hidden"
                  onChange={handleChange}
                  accept="image/*"
                  name="profileimg"
                />
              </div>
              <div
                className="d-flex justify-content-center"
                style={{ margin: "0px" }}
              >
권병윤's avatar
0806    
권병윤 committed
117
                {/* <Link to="/user/:id"> */}
권병윤's avatar
0807    
권병윤 committed
118
119
120
                <p style={{ marginTop: "31px", marginBottom: "0px" }}>
                   이름:{" "}
                </p>
권병윤's avatar
0806    
권병윤 committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
                <input
                  onChange={updateinfo}
                  name="name"
                  value={Room.name}
                  type="text"
                  className="form-control my-4 "
                  placeholder="변경할 방 이름 입력"
                  style={{
                    background: "#fcf4ff",
                    borderTop: "0",
                    borderRight: "0",
                    borderLeft: "0",
                    borderBottom: "1",
                    borderColor: "#d4cafb",
                    height: "38px",
                    width: "130px",
                    marginTop: "0px",
권병윤's avatar
0807    
권병윤 committed
138
                    marginBottom: "0px",
권병윤's avatar
0806    
권병윤 committed
139
140
                  }}
                />
권병윤's avatar
0807    
권병윤 committed
141
142
              </div>
              <div className="d-flex justify-content-evenly">
권병윤's avatar
0806    
권병윤 committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
                {/* <Link to={`/room/${roomId}/${Room.channel}`}> */}
                <button
                  onClick={changeinfo}
                  type="button"
                  className="col-2 p-1 btn btn-primary"
                >
                  저장
                </button>
                {/* </Link> */}
                <button
                  type="submit"
                  className="col-2 p-1 btn btn-primary"
                  data-bs-dismiss="modal"
                >
                  취소
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

168
export default RoomSettingChange;