Skip to content

Commit 59d3021

Browse files
committed
그룹 생성 오류 수정
1 parent 283e7bb commit 59d3021

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

polling-app-client/src/components/Dashboard/GroupCreateModal.jsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react';
22
import { Modal, Input, message } from 'antd';
3-
import { ACCESS_TOKEN } from '../../constants';
3+
import { createGroup } from '../../util/APIUtils';
4+
45

56
class GroupCreateModal extends Component {
67
constructor(props) {
@@ -17,25 +18,22 @@ class GroupCreateModal extends Component {
1718
}
1819

1920
handleSubmit = () => {
21+
2022
const { name, description, imageUrl } = this.state;
21-
const token = localStorage.getItem(ACCESS_TOKEN);
2223

23-
fetch('/api/groups', {
24-
method: 'POST',
25-
headers: {
26-
'Content-Type': 'application/json',
27-
Authorization: `Bearer ${token}`,
28-
},
29-
body: JSON.stringify({ name, description, imageUrl }),
30-
})
31-
.then(res => {
32-
if (!res.ok) throw new Error('생성 실패');
33-
return res.json();
34-
})
24+
createGroup({ name, description, imageUrl })
3525
.then(data => {
26+
if (!data || !data.name) {
27+
throw new Error("응답에 그룹 이름이 없습니다.");
28+
}
3629
message.success(`그룹 "${data.name}" 생성 완료`);
37-
this.props.onCreated(); // 부모 콜백 실행
38-
this.props.onClose(); // 모달 닫기
30+
try {
31+
this.props.onCreated();
32+
this.props.onClose();
33+
} catch (e) {
34+
console.error("후처리 중 오류:", e);
35+
}
36+
3937
})
4038
.catch(() => {
4139
message.error('그룹 생성에 실패했습니다.');

polling-app-client/src/util/APIUtils.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@ const request = (options) => {
1212
const defaults = {headers: headers};
1313
options = Object.assign({}, defaults, options);
1414

15-
return fetch(options.url, options)
16-
.then(response =>
17-
response.json().then(json => {
18-
if(!response.ok) {
19-
return Promise.reject(json);
20-
}
21-
return json;
22-
})
23-
);
24-
};
15+
return fetch(options.url, options)
16+
.then(async response => {
17+
let json = null;
18+
19+
try {
20+
json = await response.json();
21+
} catch (e) {
22+
// 응답이 비어 있거나 JSON이 아님
23+
}
2524

25+
if (!response.ok) {
26+
throw json || { message: 'Unknown error occurred' };
27+
}
28+
29+
return json;
30+
});
31+
};
2632
export function getAllPolls(page, size) {
2733
page = page || 0;
2834
size = size || POLL_LIST_SIZE;
@@ -123,4 +129,12 @@ export function getMyGroups() {
123129
url: API_BASE_URL + "/groups/my",
124130
method: 'GET'
125131
});
132+
}
133+
134+
export function createGroup(groupData) {
135+
return request({
136+
url: API_BASE_URL + "/groups",
137+
method: "POST",
138+
body: JSON.stringify(groupData),
139+
});
126140
}

0 commit comments

Comments
 (0)