Skip to content

Commit be08016

Browse files
authored
feat: chatroom 정보 연동 (#104)
* refactor: reducer 데이터 저장 호출 변경 * feat: chatroom reducer 정의 * feat: chatroom dispatch 정의 * feat: getChatroom axios api 정의 * feat: channel 이동 시 chatroom 정보 얻어오는 로직 추가 * feat: Chatroom Container 정의 및 Chatroom 정보 불러오기
1 parent f97d34a commit be08016

File tree

13 files changed

+81
-18
lines changed

13 files changed

+81
-18
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { api } from '@utils/index';
2+
import store from '@store/index';
3+
4+
const getChatroomInfo = async (selectedChatroomId: number) => {
5+
const chatroomInfo = await api.getChatroom(selectedChatroomId);
6+
store.dispatch({ type: 'LOAD', ...chatroomInfo });
7+
};
8+
9+
export { getChatroomInfo };

client/src/common/dispatch/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getUserInfo } from './user-dispatch';
22
import { getUserChatroom } from './user-chatroom-dispatch';
3+
import { getChatroomInfo } from './chatroom-dispatch';
34

4-
export { getUserInfo, getUserChatroom };
5+
export { getUserInfo, getUserChatroom, getChatroomInfo };
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const initialState = {
2+
chatType: '',
3+
description: '',
4+
isPrivate: false,
5+
title: '',
6+
topic: null,
7+
userCount: 0,
8+
users: []
9+
};
10+
11+
const chatroomReducer = (state = initialState, action: any) => {
12+
switch (action.type) {
13+
case 'LOAD':
14+
return {
15+
...state,
16+
chatType: action.chatType,
17+
description: action.description,
18+
isPrivate: action.isPrivate,
19+
title: action.title,
20+
topic: action.topic,
21+
userCount: action.userCount,
22+
users: action.users
23+
};
24+
default:
25+
return state;
26+
}
27+
};
28+
29+
export default chatroomReducer;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { combineReducers } from 'redux';
22
import userReducer from './user-reducer';
33
import sidebarReducer from './sidebar-reducer';
4+
import chatroomReducer from './chatroom-reducer';
45

56
const rootReducer = combineReducers({
6-
userReducer,
7-
sidebarReducer
7+
userData: userReducer,
8+
sidebarData: sidebarReducer,
9+
chatroomData: chatroomReducer
810
});
911

1012
export default rootReducer;

client/src/common/reducer/sidebar-reducer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const sidebarReducer = (state = initialState, action: any) => {
1919
directMessages: action.directMessages
2020
};
2121
case 'UPDATESIDEBAR':
22-
case 'SELECTCHATROOM':
2322
return {
2423
...state,
2524
selectedChatroomId: action.selectedChatroomId

client/src/common/utils/api.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ const getUserChatroom = async () => {
2222
return response.data;
2323
};
2424

25-
export { getToken, oauthLogin, getUserInfo, getUserChatroom };
25+
const getChatroom = async (id: number) => {
26+
const response = await axios.get(`/api/chatrooms/${id}`);
27+
return response.data;
28+
};
29+
30+
export { getToken, oauthLogin, getUserInfo, getUserChatroom, getChatroom };

client/src/components/molecules/Channel/ChannelContainer.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { connect } from 'react-redux';
2+
import { getChatroomInfo } from '@dispatch/index';
23
import { Channel } from './Channel';
34

45
function mapDispatchToProps(dispatch: any) {
56
return {
67
chatroomClick(selectedChatroomId: number) {
78
dispatch({ type: 'UPDATESIDEBAR', selectedChatroomId });
9+
getChatroomInfo(selectedChatroomId);
810
}
911
};
1012
}

client/src/components/molecules/DM/DMContainer.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { connect } from 'react-redux';
2+
import { getChatroomInfo } from '@dispatch/index';
23
import { DM } from './DM';
34

45
function mapDispatchToProps(dispatch: any) {
56
return {
67
chatroomClick(selectedChatroomId: number) {
78
dispatch({ type: 'UPDATESIDEBAR', selectedChatroomId });
9+
getChatroomInfo(selectedChatroomId);
810
}
911
};
1012
}

client/src/components/organisms/Header/HeaderContainer.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { connect } from 'react-redux';
22
import { Header } from './Header';
33

4-
function mapReduxStateToReactProps(reducer: any) {
5-
const state = reducer.userReducer;
4+
function mapReduxStateToReactProps(state: any) {
65
return {
7-
profileUri: state.profileUri
6+
profileUri: state.userData.profileUri
87
};
98
}
109

client/src/components/organisms/Sidebar/SidebarContainer.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { connect } from 'react-redux';
22
import { Sidebar } from './Sidebar';
33

4-
function mapReduxStateToReactProps(reducer: any) {
5-
const state = reducer.sidebarReducer;
4+
function mapReduxStateToReactProps(state: any) {
65
return {
7-
starred: state.starred,
8-
otherSections: state.otherSections,
9-
channels: state.channels,
10-
directMessages: state.directMessages,
11-
selectedChatroomId: state.selectedChatroomId
6+
starred: state.sidebarData.starred,
7+
otherSections: state.sidebarData.otherSections,
8+
channels: state.sidebarData.channels,
9+
directMessages: state.sidebarData.directMessages,
10+
selectedChatroomId: state.sidebarData.selectedChatroomId
1211
};
1312
}
1413

0 commit comments

Comments
 (0)