Skip to content

Commit 4475e8c

Browse files
committed
Refactor: getCurrentUserId 적용
1 parent cfde082 commit 4475e8c

4 files changed

Lines changed: 15 additions & 14 deletions

File tree

src/pages/Chats/ChatRoom/ChatBox/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { useRecoilValue } from 'recoil';
44
import { useParams } from 'react-router-dom';
55
import { OpponentInfoAtom } from '@recoil/util/OpponentInfo';
66
import { useSocket } from '@context/SocketProvider';
7+
import { getCurrentUserId } from '@utils/getCurrentUserId';
78

89
const ChatBox: React.FC = () => {
910
const [newMessage, setNewMessage] = useState('');
1011
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
1112
const socket = useSocket();
1213

1314
const { chatRoomId } = useParams();
14-
const storageValue = localStorage.getItem('my_id');
15-
const userId = storageValue ? Number(storageValue) : -1;
15+
const currentUserId = getCurrentUserId();
1616
const opponentInfo = useRecoilValue(OpponentInfoAtom);
1717
const isOpponentValid = !!(opponentInfo && opponentInfo.id);
1818

@@ -53,7 +53,7 @@ const ChatBox: React.FC = () => {
5353
chatRoomId: Number(chatRoomId),
5454
toUserId: opponentInfo?.id,
5555
content: newMessage,
56-
fromUserId: userId,
56+
fromUserId: currentUserId,
5757
createdAt: new Date().toISOString(),
5858
};
5959
socket.emit('sendMessage', sendMessageRequest);

src/pages/Chats/ChatRoom/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type { chatRoomMessagesData } from '@apis/chatting/dto';
3030
import { postUserBlockApi } from '@apis/user-block';
3131
import type { PostUserBlockRequest } from '@apis/user-block/dto';
3232
import { handleError } from '@apis/util/handleError';
33+
import { getCurrentUserId } from '@utils/getCurrentUserId';
3334

3435
const ChatRoom: React.FC = () => {
3536
const [extendedMessages, setextendedMessages] = useState<ExtendedMessageDto[]>([]);
@@ -49,9 +50,8 @@ const ChatRoom: React.FC = () => {
4950
const nav = useNavigate();
5051
const socket = useSocket();
5152

52-
const storageValue = localStorage.getItem('my_id');
53-
const userId = storageValue ? Number(storageValue) : -1;
5453
const { chatRoomId } = useParams();
54+
const currentUserId = getCurrentUserId();
5555
const opponentInfo = useRecoilValue(OpponentInfoAtom);
5656

5757
// 메시지 수신 시 아래로 스크롤 (스크롤 아래 고정)
@@ -74,7 +74,7 @@ const ChatRoom: React.FC = () => {
7474
const postUserBlock = async () => {
7575
try {
7676
const data: PostUserBlockRequest = {
77-
fromUserId: userId,
77+
fromUserId: currentUserId,
7878
toUserId: opponentInfo?.id || -1,
7979
action: 'block',
8080
};
@@ -98,7 +98,7 @@ const ChatRoom: React.FC = () => {
9898
if (socket) {
9999
const data = {
100100
chatRoomId: Number(chatRoomId),
101-
userId: userId,
101+
userId: currentUserId,
102102
};
103103
socket.emit('leaveChatRoom', data);
104104
nav('/chats', { replace: true });
@@ -141,7 +141,7 @@ const ChatRoom: React.FC = () => {
141141
// 메시지 수신 시 렌더링에 필요한 정보 추가
142142
// 이거 위에랑 합칠수없나?
143143
useEffect(() => {
144-
const temp = createExtendedMessages(allMessages, userId, opponentInfo);
144+
const temp = createExtendedMessages(allMessages, currentUserId, opponentInfo);
145145
setextendedMessages(temp);
146146
}, [allMessages]);
147147

src/pages/Chats/RecentChat/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ChatRoomItem from '../ChatRoomItem/index';
66
import { StyledText } from '@components/Text/StyledText';
77
import { useSocket } from '@context/SocketProvider';
88
import type { ChatRoomData } from '@apis/chatting/dto';
9+
import { getCurrentUserId } from '@utils/getCurrentUserId';
910

1011
interface RecentChatProps {
1112
matchingCount: number;
@@ -16,8 +17,7 @@ const RecentChat: React.FC<RecentChatProps> = () => {
1617
const [chatRoomList, setChatRoomList] = useState<ChatRoomData[]>([]);
1718
const [isLoading, setIsLoading] = useState(true);
1819
const socket = useSocket();
19-
const storageValue = localStorage.getItem('my_id');
20-
const userId = Number(storageValue);
20+
const currentUserId = getCurrentUserId();
2121

2222
useEffect(() => {
2323
// 채팅방 리스트 조회
@@ -27,7 +27,7 @@ const RecentChat: React.FC<RecentChatProps> = () => {
2727
};
2828

2929
if (socket) {
30-
socket.emit('getChatRooms', { userId: userId });
30+
socket.emit('getChatRooms', { userId: currentUserId });
3131
socket.on('chatRoomList', getChatRooms);
3232
}
3333

src/pages/Home/OOTD/Feed/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { togglePostLikeStatusApi } from '@apis/post-like';
4040
import { postUserBlockApi } from '@apis/user-block';
4141
import type { PostUserBlockRequest } from '@apis/user-block/dto';
4242
import type { FeedProps } from './dto';
43+
import { getCurrentUserId } from '@utils/getCurrentUserId';
4344

4445
const Feed: React.FC<FeedProps> = ({ feed }) => {
4546
const [isLikeClicked, setIsLikeClicked] = useState(feed.isPostLike);
@@ -50,7 +51,7 @@ const Feed: React.FC<FeedProps> = ({ feed }) => {
5051
const [modalContent, setModalContent] = useState('');
5152

5253
const nav = useNavigate();
53-
const userId = localStorage.getItem('my_id');
54+
const currentUserId = getCurrentUserId();
5455
const timeAgo = dayjs(feed.createdAt).locale('ko').fromNow();
5556

5657
const handleMoreButtonClick = (e: React.MouseEvent) => {
@@ -111,7 +112,7 @@ const Feed: React.FC<FeedProps> = ({ feed }) => {
111112
const postUserBlock = async () => {
112113
try {
113114
const data: PostUserBlockRequest = {
114-
fromUserId: Number(userId) || -1,
115+
fromUserId: currentUserId || -1,
115116
toUserId: feed.user.id,
116117
action: 'block',
117118
};
@@ -133,7 +134,7 @@ const Feed: React.FC<FeedProps> = ({ feed }) => {
133134
const createMatching = async (comment: string) => {
134135
try {
135136
const matchingRequest: CreateMatchingRequest = {
136-
requesterId: Number(userId) || -1,
137+
requesterId: currentUserId || -1,
137138
targetId: feed.user.id || -1,
138139
message: comment,
139140
};

0 commit comments

Comments
 (0)