Skip to content

Commit d9c2643

Browse files
committed
Added alert showing API callbacks in ChatRoomEditorModal
1 parent e33ee44 commit d9c2643

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/app/chatroom/editor/ChatRoomEditorAPI.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import appConfig from '../../../config/appConfig.json';
22
import Cookies from 'js-cookie';
33

4-
export const editChatRoom = (id, name, description) => {
5-
fetch(appConfig.apiUrl + 'secure/chatroom', {
4+
export const editChatRoom = async (id, name, description) => {
5+
return fetch(appConfig.apiUrl + 'secure/chatroom', {
66
method: 'PUT',
77
headers: {
88
'Authorization': Cookies.get('Authorization'),
@@ -13,5 +13,9 @@ export const editChatRoom = (id, name, description) => {
1313
'name': name,
1414
'description': description
1515
})
16+
}).then(response => {
17+
return response.json();
18+
}).then(jsonResponse => {
19+
return jsonResponse;
1620
})
1721
}

src/app/chatroom/editor/ChatRoomEditorModal.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class ChatRoomEditorModal extends Component {
1010
this.state = {
1111
modal: false,
1212
nameInput: '',
13-
descInput: ''
13+
descInput: '',
14+
alert: ''
1415
}
1516
this.toggle = this.toggle.bind(this);
1617
this.handleNameInput = this.handleNameInput.bind(this);
@@ -27,7 +28,9 @@ class ChatRoomEditorModal extends Component {
2728
*/
2829

2930
editChatRoom() {
30-
editChatRoom(this.props.chatRoomId, this.state.nameInput, this.state.descInput);
31+
editChatRoom(this.props.chatRoomId, this.state.nameInput, this.state.descInput).then(jsonResponse => {
32+
this.handleResponseFromAPI(jsonResponse);
33+
});
3134
}
3235

3336
setFieldsValue() {
@@ -46,6 +49,23 @@ class ChatRoomEditorModal extends Component {
4649
}
4750
}
4851

52+
handleResponseFromAPI(jsonResponse) {
53+
switch (jsonResponse.response) {
54+
case "INSUFFICIENT_RIGHTS":
55+
this.setAlert('warning', "It seems that you don't have right to do that.");
56+
break;
57+
case "CHAT_ROOM_NAME_TAKEN":
58+
this.setAlert('warning', "Well this name is already taken, think about something else.");
59+
break;
60+
case "CANT_UPDATE_CHAT_ROOM_THAT_DOESNT_EXIST":
61+
this.setAlert('warning', "Are you trying to edit a room that doesn't exist?");
62+
break;
63+
case "CHAT_ROOM_UPDATED_SUCCESSFULLY":
64+
this.setAlert('success', "ChatRoom has been edited successfully");
65+
break;
66+
}
67+
}
68+
4969
/**
5070
* DOM related
5171
*/
@@ -68,12 +88,22 @@ class ChatRoomEditorModal extends Component {
6888
})
6989
}
7090

91+
setAlert(type, message) {
92+
const alertClass = 'alert alert-' + type;
93+
this.setState({
94+
alert: <div className={alertClass} role='alert'>{message}</div>
95+
})
96+
}
97+
7198
render() {
99+
const alert = this.state.alert;
100+
72101
return (
73102
<div>
74103
<Modal isOpen={this.state.modal} toggle={this.toggle}>
75104
<ModalHeader toggle={this.toggle}>Edit ChatRoom</ModalHeader>
76105
<ModalBody>
106+
{alert}
77107
<label>Name:</label>
78108
<input type="text" className="form-control" value={this.state.nameInput} onChange={this.handleNameInput} />
79109
<label>Description:</label>

0 commit comments

Comments
 (0)