-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 채팅 생성 기능 * refactor: 게더링 카드 유저 아이디 props 설정 * feat: 채팅 채팅방 목록 * feat: node global 설정 * feat: 채팅방 참여 * feat: 채팅 보내기 기능 * remove: 안쓰는 파일 삭제
- Loading branch information
Showing
25 changed files
with
885 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,30 @@ | ||
import type { PostCreateChatRoomResponse } from './chatting.dto'; | ||
import type { ChatCategory } from './types'; | ||
|
||
import type { NotificationType } from '@/features/notification/notification.type'; | ||
import api from '@/shared/api/baseApi'; | ||
|
||
export const postCreateChatRoom = (chatCategory: NotificationType, targetId: number) => | ||
export const postCreateChatRoom = (chatCategory: ChatCategory, targetId: number) => | ||
api | ||
.post<PostCreateChatRoomResponse>('/chat-room', { chatCategory, targetId }) | ||
.then(res => res.data) | ||
.catch(error => { | ||
console.error('채팅방 생성 요청 실패:', error.response?.data); | ||
console.error('요청 데이터:', { chatCategory, targetId }); | ||
throw error; | ||
}); | ||
export const getChatRoomList = () => api.get('/chat-room/me').then(res => res.data); | ||
export const participateChatRoom = (chatRoomId: number) => | ||
api | ||
.post<PostCreateChatRoomResponse>(`/chat-room/participation/${chatRoomId}`) | ||
.then(res => res.data); | ||
|
||
export const getChatHistory = (chatRoomId: number, size: number = 3, lastSendAt?: string) => { | ||
const params = new URLSearchParams({ | ||
size: size.toString(), | ||
...(lastSendAt && { lastSendAt }), | ||
}); | ||
|
||
return api.get(`/chat-room/chats/${chatRoomId}?${params}`).then(res => res.data); | ||
}; | ||
export const leaveChatRoom = (chatRoomId: number) => | ||
api.delete(`/chat-room/leave/${chatRoomId}`).then(res => res.data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,113 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { useInfiniteQuery } from '@tanstack/react-query'; | ||
|
||
import { postCreateChatRoom } from './chatting.api'; | ||
import { | ||
getChatRoomList, | ||
participateChatRoom, | ||
postCreateChatRoom, | ||
getChatHistory, | ||
leaveChatRoom, | ||
} from './chatting.api'; | ||
import type { ChatCategory, ChatListResponse } from './types'; | ||
|
||
import type { NotificationType } from '@/features/notification/notification.type'; | ||
import { customToast } from '@/shared/ui'; | ||
|
||
export const useCreateChatRoom = () => | ||
useMutation({ | ||
mutationFn: ({ | ||
chatCategory, | ||
targetId, | ||
}: { | ||
chatCategory: NotificationType; | ||
targetId: number; | ||
}) => postCreateChatRoom(chatCategory, targetId), | ||
onSuccess: async () => { | ||
mutationFn: ({ chatCategory, targetId }: { chatCategory: ChatCategory; targetId: number }) => | ||
postCreateChatRoom(chatCategory, targetId), | ||
onSuccess: async response => { | ||
console.log('채팅방 생성 결과:', response); // 응답 데이터 확인 | ||
await customToast({ text: '채팅방이 생성되었습니다.', timer: 3000, icon: 'success' }); | ||
}, | ||
onError: async () => { | ||
await customToast({ text: '채팅방 생성에 실패하였습니다', timer: 3000, icon: 'error' }); | ||
}, | ||
}); | ||
|
||
export const useChattingList = () => { | ||
return useQuery<ChatListResponse>({ | ||
queryKey: ['chatRooms'], | ||
queryFn: async () => { | ||
try { | ||
const response = await getChatRoomList(); | ||
console.log('채팅방 목록 응답:', response); // 디버깅용 | ||
return response; | ||
} catch (error) { | ||
console.error('채팅방 목록 조회 실패:', error); | ||
throw error; | ||
} | ||
}, | ||
// 실패 시 재시도 방지 | ||
retry: false, | ||
// 빈 데이터 초기값 설정 | ||
initialData: { | ||
data: { | ||
chatRooms: [], | ||
}, | ||
timeStamp: new Date().toISOString(), | ||
}, | ||
}); | ||
}; | ||
|
||
// 채팅방 참여 훅 | ||
export const useChatRoomParticipation = () => | ||
useMutation({ | ||
mutationFn: (chatRoomId: number) => participateChatRoom(chatRoomId), | ||
onSuccess: async response => { | ||
console.log('채팅방 참여 결과:', response); | ||
await customToast({ text: '채팅방에 참여하였습니다.', timer: 3000, icon: 'success' }); | ||
}, | ||
onError: async () => { | ||
await customToast({ text: '채팅방 참여에 실패하였습니다', timer: 3000, icon: 'error' }); | ||
}, | ||
}); | ||
|
||
export const useChatHistory = (chatRoomId: number, size: number = 20) => { | ||
return useInfiniteQuery({ | ||
queryKey: ['chatHistory', chatRoomId], | ||
queryFn: async ({ pageParam }) => { | ||
try { | ||
const response = await getChatHistory(chatRoomId, size, pageParam); | ||
return response.data; | ||
} catch (error) { | ||
console.error('채팅 히스토리 조회 실패:', error); | ||
throw error; | ||
} | ||
}, | ||
getNextPageParam: lastPage => { | ||
if (!lastPage.hasNext) return undefined; | ||
return lastPage.lastSendAt; | ||
}, | ||
initialPageParam: undefined, | ||
select: data => ({ | ||
pages: data.pages, | ||
pageParams: data.pageParams, | ||
// 모든 채팅 메시지를 하나의 배열로 합치기 | ||
allChats: data.pages.flatMap(page => page.chats), | ||
}), | ||
}); | ||
}; | ||
export const useLeaveChatRoom = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation({ | ||
mutationFn: (chatRoomId: number) => leaveChatRoom(chatRoomId), | ||
onSuccess: async () => { | ||
// 채팅방 목록 쿼리 무효화하여 새로고침 | ||
queryClient.invalidateQueries({ queryKey: ['chatRooms'] }); | ||
// 성공 토스트 메시지 | ||
await customToast({ | ||
text: '채팅방에서 나갔습니다.', | ||
timer: 3000, | ||
icon: 'success', | ||
}); | ||
}, | ||
onError: async () => { | ||
await customToast({ | ||
text: '채팅방 나가기에 실패했습니다.', | ||
timer: 3000, | ||
icon: 'error', | ||
}); | ||
}, | ||
}); | ||
}; |
Oops, something went wrong.
17f3f79
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚡ Lighthouse report for http://localhost:3000/
Detailed Metrics