-
Notifications
You must be signed in to change notification settings - Fork 1
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 #126 from prgrms-web-devcourse-final-project/44-fe…
…ature/social-data-binding [Feature] #44 소셜, 프로필 페이지 데이터 바인딩
- Loading branch information
Showing
34 changed files
with
466 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,48 @@ | ||
import { AxiosError } from 'axios' | ||
import { APIResponse, CommonAPIResponse, ErrorResponse } from '~types/api' | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
|
||
export type CreateChatRoomRequest = { | ||
opponentMemberId: number | ||
} | ||
|
||
export type CreateChatRoomResponse = Pick< | ||
CommonAPIResponse, | ||
'chatRoomId' | 'name' | 'lastMessage' | 'unreadMessageCount' | 'members' | ||
> | ||
|
||
/** | ||
* 새로운 채팅방을 생성하고, 채팅방 정보를 반환합니다. | ||
*? 이미 동일한 맴버와의 채팅방이 있다면, 해당 채팅방을 반환합니다. | ||
*/ | ||
export const createChatRoom = async (req: CreateChatRoomRequest): Promise<APIResponse<CreateChatRoomResponse>> => { | ||
try { | ||
const { data } = await axiosInstance.post<APIResponse<CreateChatRoomResponse>>(`/chat/rooms`, req) | ||
console.log(data.message || '채팅방 생성 성공') | ||
console.log(data.data) | ||
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,37 @@ | ||
import { AxiosError } from 'axios' | ||
import { APIResponse, CommonAPIResponse, ErrorResponse } from '~types/api' | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
|
||
export type FetchChatRoomListResponse = Array< | ||
Pick<CommonAPIResponse, 'chatRoomId' | 'name' | 'lastMessage' | 'unreadMessageCount' | 'members'> | ||
> | ||
|
||
export const fetchChatRoomList = async (): Promise<APIResponse<FetchChatRoomListResponse>> => { | ||
try { | ||
const { data } = await axiosInstance.get<APIResponse<FetchChatRoomListResponse>>(`/chat/rooms`) | ||
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,31 @@ | ||
import { useMutation, UseMutationResult } from '@tanstack/react-query' | ||
import ChatModal from '~modals/ChatModal' | ||
import { useModalStore } from '~stores/modalStore' | ||
import { APIResponse } from '~types/api' | ||
import { createChatRoom, CreateChatRoomRequest, CreateChatRoomResponse } from './createChatRoom' | ||
|
||
export const useCreateChatRoom = (): UseMutationResult< | ||
APIResponse<CreateChatRoomResponse>, | ||
Error, | ||
CreateChatRoomRequest | ||
> & { | ||
createRoom: (req: CreateChatRoomRequest) => void | ||
} => { | ||
const { pushModal } = useModalStore() | ||
|
||
const mutation = useMutation<APIResponse<CreateChatRoomResponse>, Error, CreateChatRoomRequest>({ | ||
mutationFn: createChatRoom, | ||
onSuccess: (data, { opponentMemberId }) => { | ||
console.log(data.message || '채팅방 생성 성공') | ||
pushModal(<ChatModal chatRoomId={data.data.chatRoomId} userId={opponentMemberId} />) | ||
}, | ||
onError: error => { | ||
console.error('채팅방 생성 실패:', error.message) | ||
}, | ||
}) | ||
|
||
return { | ||
...mutation, | ||
createRoom: mutation.mutate, | ||
} | ||
} |
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,28 @@ | ||
import { useSuspenseQueries } from '@tanstack/react-query' | ||
import { fetchChatRoomList } from '~apis/chatRoom/fetchChatRoomList' | ||
import { fetchFriendList } from '~apis/friend/fetchFriendList' | ||
import { queryKey } from '~constants/queryKey' | ||
|
||
export function useSocialData() { | ||
const results = useSuspenseQueries({ | ||
queries: [ | ||
{ | ||
queryKey: queryKey.social.chatRoomList(), | ||
queryFn: () => fetchChatRoomList().then(res => res.data), | ||
}, | ||
{ | ||
queryKey: queryKey.social.friendList(), | ||
queryFn: () => fetchFriendList().then(res => res.data), | ||
}, | ||
], | ||
}) | ||
|
||
const [chatListQuery, friendListQuery] = results | ||
|
||
return { | ||
chatList: chatListQuery.data ?? [], | ||
friendList: friendListQuery.data ?? [], | ||
isLoading: chatListQuery.isLoading || friendListQuery.isLoading, | ||
isError: chatListQuery.isError || friendListQuery.isError, | ||
} | ||
} |
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,37 @@ | ||
import { AxiosError } from 'axios' | ||
import { APIResponse, CommonAPIResponse, ErrorResponse } from '~types/api' | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
|
||
export type FetchFriendListResponse = Array< | ||
Pick<CommonAPIResponse, 'memberId' | 'gender' | 'familyRole' | 'profileImg' | 'name'> | ||
> | ||
|
||
export const fetchFriendList = async (): Promise<APIResponse<FetchFriendListResponse>> => { | ||
try { | ||
const { data } = await axiosInstance.get<APIResponse<FetchFriendListResponse>>(`/friend`) | ||
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,49 @@ | ||
import { AxiosError } from 'axios' | ||
import { APIResponse, CommonAPIRequest, CommonAPIResponse, ErrorResponse } from '~types/api' | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
|
||
export type FetchProfileRequest = Pick<CommonAPIRequest, 'memberId'> | ||
|
||
export type FetchProfileResponse = Pick< | ||
CommonAPIResponse, | ||
| 'memberId' | ||
| 'name' | ||
| 'address' | ||
| 'gender' | ||
| 'familyRole' | ||
| 'profileImg' | ||
| 'totalDistance' | ||
| 'walkCount' | ||
| 'countWalksWithMember' | ||
| 'dog' | ||
> | ||
|
||
export const fetchProfile = async ({ memberId }: FetchProfileRequest): Promise<APIResponse<FetchProfileResponse>> => { | ||
try { | ||
const { data } = await axiosInstance.get<APIResponse<FetchProfileResponse>>(`/member/${memberId}`) | ||
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,11 @@ | ||
import { useSuspenseQuery, UseSuspenseQueryResult } from '@tanstack/react-query' | ||
import { fetchProfile, FetchProfileResponse } from '~apis/member/fetchProfile' | ||
import { queryKey } from '~constants/queryKey' | ||
|
||
export const useFetchProfile = (memberId: number): UseSuspenseQueryResult<FetchProfileResponse, Error> => { | ||
return useSuspenseQuery<FetchProfileResponse, Error>({ | ||
queryKey: queryKey.profile(memberId), | ||
queryFn: () => fetchProfile({ memberId }).then(data => data.data), | ||
staleTime: 1000 * 60 * 5, // 5분 | ||
}) | ||
} |
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.