mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-12 23:45:18 +00:00
Added InfiniteQuery support to Home & PlaylistGenreView
This commit is contained in:
parent
4b513f91d8
commit
912f5a16ba
@ -6,14 +6,26 @@ import { QueryCacheKeys } from "../conf";
|
||||
import useSpotifyQuery from "../hooks/useSpotifyQuery";
|
||||
import PlaceholderApplet from "./shared/PlaceholderApplet";
|
||||
import PlaylistCard from "./shared/PlaylistCard";
|
||||
import useSpotifyInfiniteQuery from "../hooks/useSpotifyInfiniteQuery";
|
||||
|
||||
function Home() {
|
||||
const { data: categories, isError, refetch, isLoading } = useSpotifyQuery<SpotifyApi.CategoryObject[]>(
|
||||
const { data: pagedCategories, isError, refetch, isLoading, hasNextPage, isFetchingNextPage, fetchNextPage } = useSpotifyInfiniteQuery<SpotifyApi.PagingObject<SpotifyApi.CategoryObject>>(
|
||||
QueryCacheKeys.categories,
|
||||
(spotifyApi) => spotifyApi.getCategories({ country: "US" }).then((categoriesReceived) => categoriesReceived.body.categories.items),
|
||||
{ initialData: [] }
|
||||
(spotifyApi, { pageParam }) => spotifyApi.getCategories({ country: "US", limit: 10, offset: pageParam }).then((categoriesReceived) => categoriesReceived.body.categories),
|
||||
{
|
||||
getNextPageParam(lastPage) {
|
||||
if (lastPage.next) {
|
||||
return lastPage.offset + lastPage.limit;
|
||||
}
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const categories = pagedCategories?.pages
|
||||
.map((page) => page.items)
|
||||
.filter(Boolean)
|
||||
.flat(1);
|
||||
|
||||
return (
|
||||
<ScrollArea style={`flex-grow: 1; border: none;`}>
|
||||
<View style={`flex-direction: 'column'; align-items: 'center'; flex: 1;`}>
|
||||
@ -21,6 +33,12 @@ function Home() {
|
||||
{categories?.map((category, index) => {
|
||||
return <CategoryCard key={index + category.id} id={category.id} name={category.name} />;
|
||||
})}
|
||||
{hasNextPage &&
|
||||
<Button
|
||||
on={{ clicked: () => fetchNextPage() }}
|
||||
text="Load More"
|
||||
enabled={!isFetchingNextPage}
|
||||
/>}
|
||||
</View>
|
||||
</ScrollArea>
|
||||
);
|
||||
|
@ -3,7 +3,7 @@ import { Button, ScrollArea, Text, View } from "@nodegui/react-nodegui";
|
||||
import React from "react";
|
||||
import { useLocation, useParams } from "react-router";
|
||||
import { QueryCacheKeys } from "../conf";
|
||||
import useSpotifyQuery from "../hooks/useSpotifyQuery";
|
||||
import useSpotifyInfiniteQuery from "../hooks/useSpotifyInfiniteQuery";
|
||||
import BackButton from "./BackButton";
|
||||
import PlaceholderApplet from "./shared/PlaceholderApplet";
|
||||
import PlaylistCard from "./shared/PlaylistCard";
|
||||
@ -11,13 +11,34 @@ import PlaylistCard from "./shared/PlaylistCard";
|
||||
function PlaylistGenreView() {
|
||||
const { id } = useParams<{ id: string }>();
|
||||
const location = useLocation<{ name: string }>();
|
||||
const { data: playlists, isError, isLoading, refetch } = useSpotifyQuery<SpotifyApi.PlaylistObjectSimplified[]>(
|
||||
const { data: pagedPlaylists, isError, isLoading, refetch, hasNextPage, isFetchingNextPage, fetchNextPage } = useSpotifyInfiniteQuery<SpotifyApi.PagingObject<SpotifyApi.PlaylistObjectSimplified>>(
|
||||
[QueryCacheKeys.genrePlaylists, id],
|
||||
(spotifyApi) => spotifyApi.getPlaylistsForCategory(id).then((playlistsRes) => playlistsRes.body.playlists.items),
|
||||
{ initialData: [] }
|
||||
(spotifyApi, { pageParam }) => spotifyApi.getPlaylistsForCategory(id, { limit: 20, offset: pageParam }).then((playlistsRes) => playlistsRes.body.playlists),
|
||||
{
|
||||
getNextPageParam(lastPage) {
|
||||
if (lastPage.next) {
|
||||
return lastPage.offset + lastPage.limit;
|
||||
}
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
return <GenreView isError={isError} isLoading={isLoading} refetch={refetch} heading={location.state.name} playlists={playlists ?? []} />;
|
||||
const playlists = pagedPlaylists?.pages
|
||||
.map((page) => page.items)
|
||||
.filter(Boolean)
|
||||
.flat(1);
|
||||
|
||||
return (
|
||||
<GenreView
|
||||
isError={isError}
|
||||
isLoading={isLoading || isFetchingNextPage}
|
||||
refetch={refetch}
|
||||
heading={location.state.name}
|
||||
playlists={playlists ?? []}
|
||||
loadMore={hasNextPage ? () => fetchNextPage() : undefined}
|
||||
isLoadable={!isFetchingNextPage}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default PlaylistGenreView;
|
||||
|
Loading…
Reference in New Issue
Block a user