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
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

+2-1
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 };
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;
+4-2
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

-1
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

+6-1
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

+2
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

+2
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

+2-3
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

+6-7
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

client/src/pages/Chatroom.tsx renamed to client/src/pages/Chatroom/Chatroom.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import styled from 'styled-components';
3+
import { getChatroomInfo } from '@dispatch/index';
34
import { ChatroomHeader, ChatroomBody } from '@components/organisms';
45

56
interface ChatroomProps {
67
children: React.ReactNode;
78
title: string;
9+
selectedChatroomId: number;
810
}
911

1012
const ChatroomContainer = styled.div<any>`
1113
height: 100%;
1214
`;
1315

14-
const Chatroom: React.FC<ChatroomProps> = ({ title = '5주-그룹-프로젝트-슬랙b', children, ...props }) => {
16+
const Chatroom: React.FC<ChatroomProps> = ({ title, selectedChatroomId, children, ...props }) => {
17+
useEffect(() => {
18+
getChatroomInfo(selectedChatroomId);
19+
}, []);
1520
return (
1621
<ChatroomContainer {...props}>
1722
<ChatroomHeader title={title}>{}</ChatroomHeader>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { connect } from 'react-redux';
2+
import { Chatroom } from './Chatroom';
3+
4+
function mapReduxStateToReactProps(state: any) {
5+
return {
6+
selectedChatroomId: state.sidebarData.selectedChatroomId,
7+
title: state.chatroomData.title
8+
};
9+
}
10+
11+
export default connect(mapReduxStateToReactProps)(Chatroom);

client/src/pages/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Chatroom } from './Chatroom';
1+
import Chatroom from './Chatroom/ChatroomContainer';
22
import { Login } from './Login';
33

44
export { Chatroom, Login };

0 commit comments

Comments
 (0)