Skip to content

Commit 7a6b63c

Browse files
committed
[JAWS-121][Refactoring] any
1 parent b35f4ea commit 7a6b63c

4 files changed

Lines changed: 45 additions & 32 deletions

File tree

โ€Žsrc/_reducer/user.tsโ€Ž

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
22

3+
export interface ChatRoomsInfo {
4+
talentId: string;
5+
roomId: string;
6+
count: number;
7+
otherId: string;
8+
otherNickname: string;
9+
profileImage: string;
10+
clickPurchase: boolean[];
11+
otherIsJoined: boolean;
12+
}
13+
314
export interface UserState {
415
userInfo: {
516
id: string;
617
social: string;
718
nickname: string;
819
image: string | undefined;
920
email: string | undefined;
10-
selling: any;
11-
buying: any;
12-
unreviewed: any;
13-
reviewed: any;
14-
chatRooms: any;
21+
selling?: string[];
22+
buying?: string[];
23+
unreviewed?: string[];
24+
reviewed?: string[];
25+
chatRooms?: ChatRoomsInfo[] | undefined;
1526
} | null;
1627
accessToken: string | null;
1728
isSignin?: boolean;
@@ -69,10 +80,10 @@ export const userSlice = createSlice({
6980
// ๊ตฌ๋งค์ž๊ฐ€ ๋ฆฌ๋ทฐ๋ฅผ ๋“ฑ๋กํ•˜๋ฉด unreviewed์— ์žˆ๋˜ talentId๋ฅผ reviewed๋กœ ์˜ฎ๊ฒจ์ค€๋‹ค.
7081
updateReview: (state, action: PayloadAction<UpdateReviewPayload>) => {
7182
const { talentId } = action.payload;
72-
if (state.userInfo) {
83+
if (state.userInfo?.unreviewed) {
7384
const index = state.userInfo.unreviewed.indexOf(talentId);
74-
state.userInfo.unreviewed.splice(index, 1);
75-
state.userInfo.reviewed.push(talentId);
85+
state.userInfo.unreviewed?.splice(index, 1);
86+
state.userInfo.reviewed?.push(talentId);
7687
}
7788
},
7889

@@ -83,13 +94,13 @@ export const userSlice = createSlice({
8394
},
8495

8596
updateChatRooms: (state, action: PayloadAction<UpdateChatRoomsPayload>) => {
86-
if (state.userInfo) {
87-
state.userInfo.chatRooms.push(action.payload.chatRooms);
97+
if (state.userInfo?.chatRooms) {
98+
state.userInfo.chatRooms?.push(action.payload.chatRooms);
8899
}
89100
},
90101
renewChatRooms: (state, action: PayloadAction<RenewChatRoomsPayload>) => {
91102
console.log('๋ฆฌ๋•์Šค renewChatRooms ํŽ˜์ด๋กœ๋“œ:::::', action.payload);
92-
if (state.userInfo) {
103+
if (state.userInfo?.chatRooms) {
93104
state.userInfo.chatRooms = action.payload.chatRooms;
94105
}
95106
},
@@ -98,8 +109,8 @@ export const userSlice = createSlice({
98109
// ๊ฑฐ๋ž˜์™„๋ฃŒ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„๋•Œ ์„œ๋ฒ„์—์„œ confirmed๋ฅผ boolean๊ฐ’์œผ๋กœ ์ค€๋‹ค.
99110

100111
const { roomId, who, talentId } = action.payload;
101-
if (state.userInfo) {
102-
const roomIndex = state.userInfo.chatRooms.findIndex((room: any) => room.roomId === roomId);
112+
if (state.userInfo?.chatRooms && state.userInfo?.buying && state.userInfo?.unreviewed) {
113+
const roomIndex = state.userInfo.chatRooms?.findIndex((room: any) => room.roomId === roomId);
103114

104115
const { clickPurchase } = state.userInfo.chatRooms[roomIndex];
105116
if (who === 'mine') {
@@ -136,7 +147,7 @@ export const userSlice = createSlice({
136147

137148
escapeRoom: (state, action: PayloadAction<{ talentId: string }>) => {
138149
const { talentId } = action.payload;
139-
if (state.userInfo) {
150+
if (state.userInfo?.buying && state.userInfo?.chatRooms) {
140151
// ๋‚˜๊ฐ„ ๋ฐฉ๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ตฌ๋งค์ค‘๋ชฉ๋ก์˜ talentId๋ฅผ ์—†์• ์ค€๋‹ค.
141152
const buyingIndex = state.userInfo.buying.indexOf(talentId);
142153
state.userInfo.buying.splice(buyingIndex, 1);
@@ -148,7 +159,7 @@ export const userSlice = createSlice({
148159
},
149160
initCount: (state, action: PayloadAction<{ index: number }>) => {
150161
const { index } = action.payload;
151-
if (state.userInfo) {
162+
if (state.userInfo?.chatRooms) {
152163
console.log('roomindex::::', index);
153164
state.userInfo.chatRooms[index].count = 0;
154165
}

โ€Žsrc/components/App.tsxโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function App(): JSX.Element {
5757

5858
connect.emit(
5959
'joinroom',
60-
chatRooms.map((chatRoom: any) => chatRoom.otherId),
60+
chatRooms?.map((chatRoom: any) => chatRoom.otherId),
6161
);
6262
connect.on('hasjoined', (data: any) => {
6363
console.log('ChattingRoom2 -> ChattingRoom2 hasjoined๊ฐ€ ๋˜์—ˆ๋‚˜?', data);

โ€Žsrc/components/pages/ChattingPage/ChattingPage.tsxโ€Ž

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ function ChattingPage({ connectSocket, lastChat, setLastChat }: Props): JSX.Elem
7474
const [showChatList, setShowChatList] = useState<boolean>(false);
7575
const [clickedRoom, setClickedRoom] = useState<number>(-1);
7676

77-
const roomIdList = userInfo?.chatRooms.map((chatRoom: RoomType) => chatRoom.roomId);
78-
const otherList = userInfo?.chatRooms.map((chatRoom: RoomType) => chatRoom.otherId);
79-
const talentIdList = userInfo?.chatRooms.map((chatRoom: RoomType) => chatRoom.talentId);
77+
const roomIdList = userInfo?.chatRooms?.map((chatRoom: RoomType) => chatRoom.roomId);
78+
const otherList = userInfo?.chatRooms?.map((chatRoom: RoomType) => chatRoom.otherId);
79+
const talentIdList = userInfo?.chatRooms?.map((chatRoom: RoomType) => chatRoom.talentId);
8080

8181
useEffect(() => {
8282
// ์ƒ์„ธํŽ˜์ด์ง€์—์„œ [์ฑ„ํŒ…์œผ๋กœ ๊ฑฐ๋ž˜ํ•˜๊ธฐ] ๋ฒ„ํŠผ์„ ํ†ตํ•ด ๋“ค์–ด์˜จ ๊ฒฝ์šฐ
8383
if (isFromDetail) {
8484
// ์ฒซ ์ฑ„ํŒ…์ผ ๊ฒฝ์šฐ, ์ฑ„ํŒ…๋ฐฉ์„ ๋ฐ”๋กœ ์—ด์–ด์ฃผ๊ณ  isFirstChat ๋ณ€๊ฒฝ
85-
if (isFirstChat) {
85+
if (isFirstChat && otherList && roomIdList) {
8686
setCurOtherId(otherList[otherList.length - 1]);
8787
setCurRoomId(roomIdList[roomIdList.length - 1]);
8888
dispatch(setIsFirstChat({ isFromDetail: false, isFirstChat: false, talentId }));
89-
} else {
89+
} else if (talentIdList && otherList && roomIdList) {
9090
// ๊ธฐ์กด ์ฑ„ํŒ…๋ฐฉ์ด ์žˆ์œผ๋ฉด, ๊ทธ ์ฑ„ํŒ…๋ฐฉ์„ ์—ด์–ด์ค€๋‹ค.
9191
// chatRooms์—์„œ ํ•ด๋‹น talentId์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ์•Œ์•„๋‚ด์„œ roomID๋ฅผ ์ฐพ๋Š”๋‹ค.
9292
const chatIndex = talentIdList.indexOf(talentId);
@@ -119,7 +119,7 @@ function ChattingPage({ connectSocket, lastChat, setLastChat }: Props): JSX.Elem
119119
}, [curRoomId]);
120120

121121
const getRoomInfo = () => {
122-
const currentRoomInfo = userInfo?.chatRooms.find((room: RoomType) => room.roomId === curRoomId);
122+
const currentRoomInfo = userInfo?.chatRooms?.find((room: RoomType) => room.roomId === curRoomId);
123123
if (currentRoomInfo) {
124124
return {
125125
userId: userInfo?.id,
@@ -134,14 +134,16 @@ function ChattingPage({ connectSocket, lastChat, setLastChat }: Props): JSX.Elem
134134
};
135135

136136
const changeRoom = (index: number): void => {
137-
setCurOtherId(otherList[index]);
138-
setCurRoomId(roomIdList[index]);
139-
setShowChatList(false);
140-
setClickedRoom(index);
141-
// ์ฑ„ํŒ…๋ฐฉ์— ๋“ค์–ด๊ฐ€๋ฉด ์•ˆ ์ฝ์€ ๋ฉ”์‹œ์ง€ ์ˆ˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
142-
dispatch(initCount({ index }));
143-
// ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ฐฑ์‹ ๋˜๋˜ lastChat๋„ ์ดˆ๊ธฐํ™”์‹œ์ผœ์ค€๋‹ค. (์„œ๋ฒ„์—์„œ ๋ถˆ๋Ÿฌ์™€์„œ render์— ์ €์žฅํ•ด์„œ ๋ Œ๋”ํ• ๊บผ๊ธฐ๋•Œ๋ฌธ์— ๋ƒ…๋‘๋ฉด ๊ฒน์นœ๋‹ค.)
144-
setLastChat(null);
137+
if (otherList && roomIdList) {
138+
setCurOtherId(otherList[index]);
139+
setCurRoomId(roomIdList[index]);
140+
setShowChatList(false);
141+
setClickedRoom(index);
142+
// ์ฑ„ํŒ…๋ฐฉ์— ๋“ค์–ด๊ฐ€๋ฉด ์•ˆ ์ฝ์€ ๋ฉ”์‹œ์ง€ ์ˆ˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
143+
dispatch(initCount({ index }));
144+
// ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ฐฑ์‹ ๋˜๋˜ lastChat๋„ ์ดˆ๊ธฐํ™”์‹œ์ผœ์ค€๋‹ค. (์„œ๋ฒ„์—์„œ ๋ถˆ๋Ÿฌ์™€์„œ render์— ์ €์žฅํ•ด์„œ ๋ Œ๋”ํ• ๊บผ๊ธฐ๋•Œ๋ฌธ์— ๋ƒ…๋‘๋ฉด ๊ฒน์นœ๋‹ค.)
145+
setLastChat(null);
146+
}
145147
};
146148

147149
return (
@@ -156,7 +158,7 @@ function ChattingPage({ connectSocket, lastChat, setLastChat }: Props): JSX.Elem
156158
</CHATLISTESC>
157159
</CHATLISTTITLE>
158160
{!userInfo && <USER clicked={false}>๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•œ ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค.</USER>}
159-
{userInfo?.chatRooms.length === 0 && (
161+
{userInfo?.chatRooms?.length === 0 && (
160162
<USER clicked={false} style={{ textAlign: 'center' }}>
161163
ํ˜„์žฌ ์ฐธ์—ฌํ•˜๊ณ  ๊ณ„์‹  ์ฑ„ํŒ…๋ฐฉ์ด ์—†์Šต๋‹ˆ๋‹ค! <br />
162164
<br />

โ€Žsrc/components/pages/TalentDetailPage/TalentDetailPage.tsxโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function TalentDetailPage({ connectSocket }: Props): JSX.Element {
9090
userRole = 'seller';
9191

9292
// ์œ ์ €์˜ unreviewed ์— ํ•ด๋‹น ๊ธ€์˜ id๊ฐ€์žˆ์œผ๋ฉด ๋ฆฌ๋ทฐ์ž‘์„ฑ๊ฐ€๋Šฅ์ž
93-
} else if (userInfo.unreviewed.indexOf(talentId) !== -1) {
93+
} else if (userInfo.unreviewed?.indexOf(talentId) !== -1) {
9494
userRole = 'reviewer';
9595
}
9696
}

0 commit comments

Comments
ย (0)