feat: add another user profile bookmarks viewing

This commit is contained in:
Kentai Radiquum 2024-08-27 16:27:55 +05:00
parent 05b4fc3325
commit b0eed16d18
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
8 changed files with 150 additions and 21 deletions

View file

@ -10,7 +10,7 @@ import { ENDPOINTS } from "#/api/config";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
export function BookmarksPage() {
export function BookmarksPage(props: { profile_id?: number }) {
const token = useUserStore((state) => state.token);
const authState = useUserStore((state) => state.state);
const router = useRouter();
@ -18,8 +18,15 @@ export function BookmarksPage() {
function useFetchReleases(listName: string) {
let url: string;
if (token) {
url = `${ENDPOINTS.user.bookmark}/all/${BookmarksList[listName]}/0?token=${token}`;
if (props.profile_id) {
url = `${ENDPOINTS.user.bookmark}/all/${props.profile_id}/${BookmarksList[listName]}/0?sort=1`;
if (token) {
url += `&token=${token}`;
}
} else {
if (token) {
url = `${ENDPOINTS.user.bookmark}/all/${BookmarksList[listName]}/0?sort=1&token=${token}`;
}
}
const { data } = useSWR(url, fetcher);
@ -33,7 +40,7 @@ export function BookmarksPage() {
const [abandonedData] = useFetchReleases("abandoned");
useEffect(() => {
if (authState === "finished" && !token) {
if (authState === "finished" && !token && !props.profile_id) {
router.push("/login?redirect=/bookmarks");
}
// eslint-disable-next-line react-hooks/exhaustive-deps
@ -56,28 +63,44 @@ export function BookmarksPage() {
watchingData.content.length > 0 && (
<ReleaseCourusel
sectionTitle="Смотрю"
showAllLink="/bookmarks/watching"
showAllLink={
!props.profile_id
? "/bookmarks/watching"
: `/profile/${props.profile_id}/bookmarks/watching`
}
content={watchingData.content}
/>
)}
{plannedData && plannedData.content && plannedData.content.length > 0 && (
<ReleaseCourusel
sectionTitle="В планах"
showAllLink="/bookmarks/planned"
showAllLink={
!props.profile_id
? "/bookmarks/planned"
: `/profile/${props.profile_id}/bookmarks/planned`
}
content={plannedData.content}
/>
)}
{watchedData && watchedData.content && watchedData.content.length > 0 && (
<ReleaseCourusel
sectionTitle="Просмотрено"
showAllLink="/bookmarks/watched"
showAllLink={
!props.profile_id
? "/bookmarks/watched"
: `/profile/${props.profile_id}/bookmarks/watched`
}
content={watchedData.content}
/>
)}
{delayedData && delayedData.content && delayedData.content.length > 0 && (
<ReleaseCourusel
sectionTitle="Отложено"
showAllLink="/bookmarks/delayed"
showAllLink={
!props.profile_id
? "/bookmarks/delayed"
: `/profile/${props.profile_id}/bookmarks/delayed`
}
content={delayedData.content}
/>
)}
@ -86,7 +109,11 @@ export function BookmarksPage() {
abandonedData.content.length > 0 && (
<ReleaseCourusel
sectionTitle="Заброшено"
showAllLink="/bookmarks/abandoned"
showAllLink={
!props.profile_id
? "/bookmarks/abandoned"
: `/profile/${props.profile_id}/bookmarks/abandoned`
}
content={abandonedData.content}
/>
)}

View file

@ -40,11 +40,22 @@ export function BookmarksCategoryPage(props: any) {
const getKey = (pageIndex: number, previousPageData: any) => {
if (previousPageData && !previousPageData.content.length) return null;
if (token) {
return `${ENDPOINTS.user.bookmark}/all/${
let url: string;
if (props.profile_id) {
url = `${ENDPOINTS.user.bookmark}/all/${props.profile_id}/${
BookmarksList[props.slug]
}/${pageIndex}?token=${token}&sort=${sort.values[selectedSort].id}`;
}/${pageIndex}?sort=${sort.values[selectedSort].id}`;
if (token) {
url += `&token=${token}`;
}
} else {
if (token) {
url = `${ENDPOINTS.user.bookmark}/all/${
BookmarksList[props.slug]
}/${pageIndex}?sort=${sort.values[selectedSort].id}&token=${token}`;
}
}
return url;
};
const { data, error, isLoading, size, setSize } = useSWRInfinite(
@ -74,7 +85,7 @@ export function BookmarksCategoryPage(props: any) {
}, [scrollPosition]);
useEffect(() => {
if (authState === "finished" && !token) {
if (authState === "finished" && !token && !props.profile_id) {
router.push(`/login?redirect=/bookmarks/${props.slug}`);
}
// eslint-disable-next-line react-hooks/exhaustive-deps

View file

@ -19,7 +19,9 @@ const fetcher = async (url: string) => {
const res = await fetch(url);
if (!res.ok) {
const error = new Error(`An error occurred while fetching the data. status: ${res.status}`);
const error = new Error(
`An error occurred while fetching the data. status: ${res.status}`
);
error.message = await res.json();
throw error;
}
@ -185,6 +187,7 @@ export const ProfilePage = (props: any) => {
]}
watched_count={user.watched_episode_count}
watched_time={user.watched_time}
profile_id={user.id}
/>
<ProfileWatchDynamic watchDynamic={user.watch_dynamics || []} />
<div className="flex flex-col gap-2 xl:hidden">