-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from prgrms-web-devcourse-final-project/40-fe…
…ature/chatting [Feature] #40 채팅 구현
- Loading branch information
Showing
30 changed files
with
486 additions
and
383 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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { AxiosError } from 'axios' | ||
import { APIResponse, CommonAPIRequest, CommonAPIResponse, ErrorResponse, PaginationResponse } from '~types/api' | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
|
||
export type FetchChatMessageListRequest = Pick<CommonAPIRequest, 'chatRoomId' | 'lastMessageCreatedAt'> | ||
|
||
export type FetchChatMessageListResponse = PaginationResponse & { | ||
content: Array< | ||
Pick<CommonAPIResponse, 'chatId' | 'createdAt' | 'updatedAt' | 'chatRoomId' | 'memberInfo' | 'isRead' | 'text'> | ||
> | ||
} | ||
|
||
export const fetchChatMessageList = async ({ | ||
chatRoomId, | ||
lastMessageCreatedAt, | ||
}: FetchChatMessageListRequest): Promise<APIResponse<FetchChatMessageListResponse>> => { | ||
try { | ||
const { data } = await axiosInstance.get<APIResponse<FetchChatMessageListResponse>>(`/chat/message/${chatRoomId}`, { | ||
params: { lastMessageCreatedAt }, | ||
}) | ||
return data | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
const { response } = error as AxiosError<ErrorResponse> | ||
|
||
if (response) { | ||
const { code, message } = response.data | ||
switch (code) { | ||
case 400: | ||
throw new Error(message || '잘못된 요청입니다.') | ||
case 401: | ||
throw new Error(message || '인증에 실패했습니다.') | ||
case 500: | ||
throw new Error(message || '서버 오류가 발생했습니다.') | ||
default: | ||
throw new Error(message || '알 수 없는 오류가 발생했습니다.') | ||
} | ||
} | ||
// 요청 자체가 실패한 경우 | ||
throw new Error('네트워크 연결을 확인해주세요') | ||
} | ||
|
||
console.error('예상치 못한 에러:', error) | ||
throw new Error('다시 시도해주세요') | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useSuspenseInfiniteQuery } from '@tanstack/react-query' | ||
import { fetchChatMessageList } from '~apis/chat/fetchChatMessageList' | ||
import { queryKey } from '~constants/queryKey' | ||
|
||
type useChatMessageListProps = { | ||
chatRoomId: number | ||
} | ||
|
||
export default function useChatMessageList({ chatRoomId }: useChatMessageListProps) { | ||
return useSuspenseInfiniteQuery({ | ||
queryKey: queryKey.social.chatMessageList(chatRoomId), | ||
queryFn: async ({ pageParam }) => { | ||
return await fetchChatMessageList({ lastMessageCreatedAt: pageParam as string, chatRoomId }) | ||
}, | ||
getNextPageParam: lastPage => { | ||
return lastPage.data.last ? undefined : lastPage.data.content[0].createdAt | ||
}, | ||
initialPageParam: '', | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { fetchChatRoomList } from '~apis/chatRoom/fetchChatRoomList' | ||
import { queryKey } from '~constants/queryKey' | ||
|
||
export default function useChatList() { | ||
return useQuery({ | ||
queryKey: queryKey.social.chatRoomList(), | ||
queryFn: () => fetchChatRoomList().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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { fetchFriendList } from '~apis/friend/fetchFriendList' | ||
import { queryKey } from '~constants/queryKey' | ||
|
||
export default function useFriendList() { | ||
return useQuery({ | ||
queryKey: queryKey.social.friendList(), | ||
queryFn: () => fetchFriendList().then(res => res.data), | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
import { APIResponse, CommonAPIResponse } from '~types/api' | ||
|
||
export type RequestFriendRequest = { | ||
memberId: number | ||
decision: 'ACCEPT' | 'DENY' | ||
} | ||
|
||
export type RequestFriendResponse = Pick< | ||
CommonAPIResponse, | ||
'memberId' | 'name' | 'email' | 'provider' | 'gender' | 'address' | 'familyRole' | 'profileImg' | ||
> | ||
|
||
export const requestFriend = async (req: RequestFriendRequest): Promise<APIResponse<RequestFriendResponse>> => { | ||
const { data } = await axiosInstance.post<APIResponse<RequestFriendResponse>>(`/friend`, req) | ||
return 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
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
Oops, something went wrong.