feat: add logged in user favorite and owned collection fetching

This commit is contained in:
Kentai Radiquum 2024-08-13 14:20:28 +05:00
parent 9f3e1b951a
commit b6878a0386
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
6 changed files with 84 additions and 151 deletions

View file

@ -5,96 +5,64 @@ import { Spinner } from "#/components/Spinner/Spinner";
const fetcher = (...args: any) =>
fetch([...args] as any).then((res) => res.json());
import { useUserStore } from "#/store/auth";
import { BookmarksList } from "#/api/utils";
import { ENDPOINTS } from "#/api/config";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
export function CollectionsPage() {
const token = useUserStore((state) => state.token);
const authState = useUserStore((state) => state.state);
const userStore = useUserStore();
const router = useRouter();
// function useFetchReleases(listName: string) {
// let url: string;
function useFetchReleases(section: string) {
let url: string;
// if (token) {
// url = `${ENDPOINTS.user.bookmark}/all/${BookmarksList[listName]}/0?token=${token}`;
// }
if (userStore.token && userStore.user) {
if (section == "userCollections") {
url = `${ENDPOINTS.collection.userCollections}/${userStore.user.id}/0?token=${userStore.token}`;
} else if (section == "userFavoriteCollections") {
url = `${ENDPOINTS.collection.favoriteCollections}/all/0?token=${userStore.token}`;
}
}
// const { data } = useSWR(url, fetcher);
// return [data];
// }
const { data } = useSWR(url, fetcher);
return [data];
}
// const [watchingData] = useFetchReleases("watching");
// const [plannedData] = useFetchReleases("planned");
// const [watchedData] = useFetchReleases("watched");
// const [delayedData] = useFetchReleases("delayed");
// const [abandonedData] = useFetchReleases("abandoned");
const [userCollections] = useFetchReleases("userCollections");
const [favoriteCollections] = useFetchReleases("userFavoriteCollections");
useEffect(() => {
if (authState === "finished" && !token) {
if (userStore.state === "finished" && !userStore.token) {
router.push("/login?redirect=/collections");
}
}, [authState, token]);
}, [userStore.state, userStore.token]);
return (
<main className="container flex flex-col pt-2 pb-16 mx-auto sm:pt-4 sm:pb-0">
<CollectionCourusel
sectionTitle="Мои коллекции"
// showAllLink="/bookmarks/watching"
content={[]}
isMyCollections={true}
/>
{/* {authState === "loading" &&
(!watchingData ||
!plannedData ||
!watchedData ||
!delayedData ||
!abandonedData) && (
{userStore.state === "loading" &&
(!userCollections || !favoriteCollections) && (
<div className="flex items-center justify-center min-w-full min-h-screen">
<Spinner />
</div>
)} */}
{/* {watchingData &&
watchingData.content &&
watchingData.content.length > 0 && (
<ReleaseCourusel
sectionTitle="Смотрю"
showAllLink="/bookmarks/watching"
content={watchingData.content}
)}
{userCollections && userCollections.content && (
<CollectionCourusel
sectionTitle="Мои коллекции"
showAllLink={`/profile/${userStore.user.id}/collections`}
content={userCollections.content}
isMyCollections={true}
/>
)}
{favoriteCollections &&
favoriteCollections.content &&
favoriteCollections.content.length > 0 && (
<CollectionCourusel
sectionTitle="Избранные коллекции"
showAllLink="/collections/favorites"
content={favoriteCollections.content}
/>
)}
{plannedData && plannedData.content && plannedData.content.length > 0 && (
<ReleaseCourusel
sectionTitle="В планах"
showAllLink="/bookmarks/planned"
content={plannedData.content}
/>
)}
{watchedData && watchedData.content && watchedData.content.length > 0 && (
<ReleaseCourusel
sectionTitle="Просмотрено"
showAllLink="/bookmarks/watched"
content={watchedData.content}
/>
)}
{delayedData && delayedData.content && delayedData.content.length > 0 && (
<ReleaseCourusel
sectionTitle="Отложено"
showAllLink="/bookmarks/delayed"
content={delayedData.content}
/>
)}
{abandonedData &&
abandonedData.content &&
abandonedData.content.length > 0 && (
<ReleaseCourusel
sectionTitle="Заброшено"
showAllLink="/bookmarks/abandoned"
content={abandonedData.content}
/>
)} */}
</main>
);
}