fix: bug
This commit is contained in:
@@ -24,8 +24,8 @@ export const useUserFavorites = (initialParams?: GetUserFavoritesParams) => {
|
||||
const [loadingMore, setLoadingMore] = useState(false)
|
||||
const [error, setError] = useState<ApiError | null>(null)
|
||||
const [data, setData] = useState<GetUserFavoritesResponse>()
|
||||
const [hasMore, setHasMore] = useState(true)
|
||||
const currentPageRef = useRef(1)
|
||||
const hasMoreRef = useRef(true)
|
||||
|
||||
const execute = useCallback(async (params?: GetUserFavoritesParams) => {
|
||||
setLoading(true)
|
||||
@@ -55,14 +55,15 @@ export const useUserFavorites = (initialParams?: GetUserFavoritesParams) => {
|
||||
const total = data?.total || 0
|
||||
const limit = requestParams.limit || 20
|
||||
const totalPages = Math.ceil(total / limit)
|
||||
hasMoreRef.current = currentPage < totalPages
|
||||
const hasMoreValue = currentPage < totalPages
|
||||
setHasMore(hasMoreValue)
|
||||
setData(data)
|
||||
setLoading(false)
|
||||
return { data, error: null }
|
||||
}, [initialParams])
|
||||
|
||||
const loadMore = useCallback(async () => {
|
||||
if (loadingMore || loading || !hasMoreRef.current) return { data: undefined, error: null }
|
||||
if (loadingMore || loading || !hasMore) return { data: undefined, error: null }
|
||||
|
||||
setLoadingMore(true)
|
||||
const nextPage = currentPageRef.current + 1
|
||||
@@ -87,7 +88,8 @@ export const useUserFavorites = (initialParams?: GetUserFavoritesParams) => {
|
||||
const total = newData?.total || 0
|
||||
const limit = requestParams.limit || 20
|
||||
const totalPages = Math.ceil(total / limit)
|
||||
hasMoreRef.current = nextPage < totalPages
|
||||
const hasMoreValue = nextPage < totalPages
|
||||
setHasMore(hasMoreValue)
|
||||
currentPageRef.current = nextPage
|
||||
|
||||
setData((prev) => ({
|
||||
@@ -98,10 +100,10 @@ export const useUserFavorites = (initialParams?: GetUserFavoritesParams) => {
|
||||
}))
|
||||
setLoadingMore(false)
|
||||
return { data: newData, error: null }
|
||||
}, [loading, loadingMore, initialParams])
|
||||
}, [loading, loadingMore, hasMore, initialParams])
|
||||
|
||||
const refetch = useCallback(() => {
|
||||
hasMoreRef.current = true
|
||||
setHasMore(true)
|
||||
return execute()
|
||||
}, [execute])
|
||||
|
||||
@@ -110,11 +112,11 @@ export const useUserFavorites = (initialParams?: GetUserFavoritesParams) => {
|
||||
favorites: data?.favorites || [],
|
||||
loading,
|
||||
loadingMore,
|
||||
hasMore,
|
||||
error,
|
||||
execute,
|
||||
refetch,
|
||||
loadMore,
|
||||
hasMore: hasMoreRef.current,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user