-
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.
refactor: split hook api get all chapter
- Loading branch information
Showing
5 changed files
with
56 additions
and
50 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,44 @@ | ||
import { useState } from "react"; | ||
import { IChapterRoot } from "../../types/novel"; | ||
import { getCurrentPageByChapterId } from "../../utils/helpers"; | ||
import { useSelector } from "react-redux"; | ||
import { getListChapterReaded } from "../../store/readed/selector"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { IResponse } from "../../types/response"; | ||
import { ApiGetAllChapter } from "../../api/apiNovel"; | ||
|
||
interface Props { | ||
server: string; | ||
chapterId: string | undefined; | ||
novelId: string; | ||
} | ||
|
||
const useGetAllChapter = ({ server, chapterId, novelId }: Props) => { | ||
const [chapters, setChapters] = useState<IChapterRoot[]>([]); | ||
const [currentPage, setCurrentPage] = useState<number>(getCurrentPageByChapterId(server, chapterId || "1")); | ||
const [firstSelectedPage] = useState(currentPage); | ||
|
||
const [, setPerPage] = useState<number>(0); | ||
const [totalPage, setTotalPage] = useState<number>(1); | ||
|
||
const listChapterReaded = useSelector(getListChapterReaded(novelId)); | ||
|
||
const { isFetching, isError } = useQuery({ | ||
queryKey: ["all_chapter", novelId, currentPage, server], | ||
queryFn: async () => { | ||
const data: IResponse<IChapterRoot[]> = await ApiGetAllChapter(server, novelId, currentPage); | ||
|
||
setPerPage(data.perPage); | ||
setTotalPage(data.totalPage); | ||
setChapters(data.data); | ||
|
||
return data; | ||
}, | ||
retry: 1, | ||
}); | ||
|
||
const isLoading = isFetching || chapters.length <= 0; | ||
return { isLoading, isError, totalPage, currentPage, setCurrentPage, firstSelectedPage, chapters, listChapterReaded }; | ||
}; | ||
|
||
export default useGetAllChapter; |