From 21aca89a6bc9b93107205c9171e1b9ef43b17a5b Mon Sep 17 00:00:00 2001 From: cjeongmin Date: Tue, 21 May 2024 15:17:58 +0900 Subject: [PATCH 1/2] refactor: Modify create chatting room (#57) --- src/app/pages/shared-post-page.tsx | 19 ++++++++----------- src/components/FloatingChatting.tsx | 1 - src/features/chat/chat.api.ts | 20 +++++++++++++------- src/features/chat/chat.hook.ts | 6 ++---- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/app/pages/shared-post-page.tsx b/src/app/pages/shared-post-page.tsx index 47dde2c2ea..ad56004904 100644 --- a/src/app/pages/shared-post-page.tsx +++ b/src/app/pages/shared-post-page.tsx @@ -517,18 +517,9 @@ export function SharedPostPage({ } }, [sharedPost]); - const [roomName, setRoomName] = useState(''); - const [userId, setUserId] = useState(''); const [, setIsChatOpen] = useRecoilState(chatOpenState); - useEffect(() => { - if (sharedPost !== undefined) { - setRoomName(sharedPost.data.publisherAccount.nickname); - setUserId(sharedPost.data.publisherAccount.memberId); - } - }, [sharedPost]); - - const { mutate: chattingMutate } = useCreateChatRoom(roomName, [userId]); + const { mutate: chattingMutate } = useCreateChatRoom(); const isLoading = useMemo( () => @@ -749,7 +740,13 @@ export function SharedPostPage({ { - chattingMutate(); + if (selected == null) return; + + chattingMutate({ + roomName: selected.nickname, + members: [selected.memberId], + }); + setTimeout(() => { setIsChatOpen(true); }, 200); diff --git a/src/components/FloatingChatting.tsx b/src/components/FloatingChatting.tsx index 6a0364de2e..8f20cdc569 100644 --- a/src/components/FloatingChatting.tsx +++ b/src/components/FloatingChatting.tsx @@ -332,7 +332,6 @@ export function FloatingChatting() { const auth = useAuthValue(); if (auth == null) return <>; - return ( <> diff --git a/src/features/chat/chat.api.ts b/src/features/chat/chat.api.ts index 81cb1d41be..e7a5938be7 100644 --- a/src/features/chat/chat.api.ts +++ b/src/features/chat/chat.api.ts @@ -14,11 +14,17 @@ export const getChatRoomList = async (token: string | undefined) => }) .then(res => res.data); -export const postChatRoom = async (roomName: string, members: string[]) => { +export const postChatRoom = async ({ + roomName, + members, +}: { + roomName: string; + members: string[]; +}) => { await axios .post(`/maru-api/chatRoom`, { - roomName: roomName, - members: members, + roomName, + members, }) .then(res => res.data); }; @@ -26,7 +32,7 @@ export const postChatRoom = async (roomName: string, members: string[]) => { export const postInviteUser = async (roomId: number, members: string[]) => { await axios .post(`/maru-api/chatRoom/${roomId}/invite`, { - members: members, + members, }) .then(res => res.data); }; @@ -40,9 +46,9 @@ export const getEnterChatRoom = async ( `/maru-api/chatRoom/${roomId}/chat`, { params: { - roomId: roomId, - page: page, - size: size, + roomId, + page, + size, }, }, ); diff --git a/src/features/chat/chat.hook.ts b/src/features/chat/chat.hook.ts index 3fdd47e077..c62388bab0 100644 --- a/src/features/chat/chat.hook.ts +++ b/src/features/chat/chat.hook.ts @@ -16,11 +16,9 @@ export const useChatRoomList = (token: string | undefined) => enabled: token !== undefined, }); -export const useCreateChatRoom = (roomName: string, members: string[]) => +export const useCreateChatRoom = () => useMutation({ - mutationFn: async () => { - await postChatRoom(roomName, members); - }, + mutationFn: postChatRoom, }); export const useInviteUsers = (roomId: number, members: string[]) => From 5e2191ef421ab996ca9d975d844ab8cfd3f12896 Mon Sep 17 00:00:00 2001 From: cjeongmin Date: Tue, 21 May 2024 15:20:17 +0900 Subject: [PATCH 2/2] fix: Fix build error --- .../pages/mobile/mobile-shared-post-page.tsx | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/app/pages/mobile/mobile-shared-post-page.tsx b/src/app/pages/mobile/mobile-shared-post-page.tsx index 49ef50159a..721fe1f20a 100644 --- a/src/app/pages/mobile/mobile-shared-post-page.tsx +++ b/src/app/pages/mobile/mobile-shared-post-page.tsx @@ -6,7 +6,7 @@ import styled from 'styled-components'; import { Bookmark, CircularProfileImage } from '@/components'; import { ImageGrid } from '@/components/shared-post-page'; -import { useAuthValue, useUserData } from '@/features/auth'; +import { useAuthValue } from '@/features/auth'; import { useCreateChatRoom } from '@/features/chat'; import { fromAddrToCoord } from '@/features/geocoding'; import { useFollowUser, useUnfollowUser } from '@/features/profile'; @@ -445,15 +445,6 @@ export function MobileSharedPostPage({ enabled: type === 'dormitory' && auth?.accessToken != null, }); - const { data: userData } = useUserData(auth?.accessToken != null); - const [userId, setUserId] = useState(''); - - useEffect(() => { - if (userData != null) { - setUserId(userData.memberId); - } - }, [userData]); - useEffect(() => { if (sharedPost?.data.address.roadAddress != null) { fromAddrToCoord({ query: sharedPost?.data.address.roadAddress }).then( @@ -474,16 +465,7 @@ export function MobileSharedPostPage({ } }, [sharedPost]); - const [roomName, setRoomName] = useState(''); - - useEffect(() => { - if (sharedPost !== undefined) { - setRoomName(sharedPost.data.publisherAccount.nickname); - } - }, [sharedPost]); - - const members = [userId]; - const { mutate: chattingMutate } = useCreateChatRoom(roomName, members); + const { mutate: chattingMutate } = useCreateChatRoom(); const isLoading = useMemo( () => @@ -574,7 +556,12 @@ export function MobileSharedPostPage({ { - chattingMutate(); + if (selected == null) return; + + chattingMutate({ + roomName: selected.nickname, + members: [selected.memberId], + }); }} > 채팅