-
Notifications
You must be signed in to change notification settings - Fork 3
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 #67 from DEPthes/dev
Dev
- Loading branch information
Showing
14 changed files
with
219 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,39 @@ | ||
import ieumAxios from './ieumAxios'; | ||
import { authToken } from '@/class/authToken'; | ||
|
||
export type GetLettersReqParams = { | ||
type: LetterType; | ||
page: number; | ||
size: number; | ||
}; | ||
|
||
export type LettersResponse = { | ||
check: boolean; | ||
information: { | ||
envelopType: number; | ||
letterId: number; | ||
senderNickname: string; | ||
title: string; | ||
modifiedAt: string; | ||
}[]; | ||
content: { | ||
envelopType: number; | ||
letterId: number; | ||
senderNickname: string | null; | ||
title: string; | ||
modifiedAt: string; | ||
}[]; | ||
pageable: { | ||
pageNumber: number; | ||
pageSize: number; | ||
}; | ||
last: boolean; | ||
}; | ||
}; | ||
export type LetterType = 'read' | 'unread'; | ||
export async function getLetters(type: LetterType) { | ||
export async function getLetters({ type, page, size }: GetLettersReqParams) { | ||
const accessToken = authToken.getToken(); | ||
const url = type === 'read' ? '/api/mailbox/read' : '/api/mailbox'; | ||
const params = { | ||
page, | ||
size, | ||
}; | ||
return await ieumAxios.get<LettersResponse>(url, { | ||
params, | ||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${accessToken}` }, | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,18 +1,27 @@ | ||
import { LetterType, LettersResponse, getLetters } from '@/apis/getLetters'; | ||
import { useQuery } from 'react-query'; | ||
import { GetLettersReqParams, LetterType, LettersResponse, getLetters } from '@/apis/getLetters'; | ||
import { useInfiniteQuery, useQuery } from 'react-query'; | ||
import { withVerify } from '@/apis/withVerify'; | ||
|
||
// 우체통 | ||
export const LETTERS_QUERY_KEY = 'lettersQuery'; | ||
export default function useLettersQuery(type: LetterType) { | ||
export default function useLettersQuery(params: GetLettersReqParams) { | ||
const { | ||
data: letters, | ||
data: lettersInfiniteData, | ||
isLoading, | ||
isError, | ||
} = useQuery({ | ||
queryKey: [LETTERS_QUERY_KEY, type], | ||
queryFn: () => withVerify(() => getLetters(type)), | ||
select: (res) => res.data.information, | ||
fetchNextPage: getNextLetters, | ||
isSuccess: getLettersIsSuccess, | ||
hasNextPage, | ||
} = useInfiniteQuery({ | ||
queryKey: [LETTERS_QUERY_KEY, params], | ||
queryFn: ({ pageParam = 0 }) => withVerify(() => getLetters({ ...params, page: pageParam })), | ||
getNextPageParam: (lastPage) => { | ||
if (lastPage.data.information.last) { | ||
return undefined; | ||
} | ||
return lastPage.data.information.pageable.pageNumber + 1; | ||
}, | ||
}); | ||
return { letters, isLoading, isError }; | ||
const letters = lettersInfiniteData?.pages.map((letter) => letter.data.information.content).flat(); | ||
return { letters, isLoading, isError, getNextLetters, getLettersIsSuccess, hasNextPage }; | ||
} |
Oops, something went wrong.