refactor: fetcher -> useSWRfetcher

This commit is contained in:
Kentai Radiquum 2025-03-20 23:15:58 +05:00
parent 8e56a39fe1
commit b10a4fabb0
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
6 changed files with 17 additions and 101 deletions

View file

@ -9,6 +9,7 @@ import { Dropdown, Button } from "flowbite-react";
import { sort } from "./common";
import { ENDPOINTS } from "#/api/config";
import { useRouter } from "next/navigation";
import { useSWRfetcher } from "#/api/utils";
const DropdownTheme = {
floating: {
@ -16,25 +17,10 @@ const DropdownTheme = {
},
};
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}`
);
error.message = await res.json();
throw error;
}
return res.json();
};
export function FavoritesPage() {
const token = useUserStore((state) => state.token);
const authState = useUserStore((state) => state.state);
const [selectedSort, setSelectedSort] = useState(0);
const [isLoadingEnd, setIsLoadingEnd] = useState(false);
const router = useRouter();
const [searchVal, setSearchVal] = useState("");
@ -47,7 +33,7 @@ export function FavoritesPage() {
const { data, error, isLoading, size, setSize } = useSWRInfinite(
getKey,
fetcher,
useSWRfetcher,
{ initialSize: 2 }
);
@ -59,7 +45,6 @@ export function FavoritesPage() {
allReleases.push(...data[i].content);
}
setContent(allReleases);
setIsLoadingEnd(true);
}
}, [data]);
@ -156,7 +141,7 @@ export function FavoritesPage() {
</div>
{content && content.length > 0 ? (
<ReleaseSection content={content} />
) : !isLoadingEnd || isLoading ? (
) : isLoading ? (
<div className="flex flex-col items-center justify-center min-w-full min-h-screen">
<Spinner />
</div>

View file

@ -8,25 +8,12 @@ import { useUserStore } from "../store/auth";
import { ENDPOINTS } from "#/api/config";
import { Button } from "flowbite-react";
import { useRouter } from "next/navigation";
import { useSWRfetcher } from "#/api/utils";
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}`
);
error.message = await res.json();
throw error;
}
return res.json();
};
export function HistoryPage() {
const token = useUserStore((state) => state.token);
const authState = useUserStore((state) => state.state);
const [isLoadingEnd, setIsLoadingEnd] = useState(false);
const router = useRouter();
const [searchVal, setSearchVal] = useState("");
@ -39,7 +26,7 @@ export function HistoryPage() {
const { data, error, isLoading, size, setSize } = useSWRInfinite(
getKey,
fetcher,
useSWRfetcher,
{ initialSize: 2 }
);
@ -51,7 +38,6 @@ export function HistoryPage() {
allReleases.push(...data[i].content);
}
setContent(allReleases);
setIsLoadingEnd(true);
}
}, [data]);
@ -136,7 +122,7 @@ export function HistoryPage() {
</Button>
)}
</>
) : !isLoadingEnd || isLoading ? (
) : isLoading ? (
<div className="flex flex-col items-center justify-center min-w-full min-h-[100dvh]">
<Spinner />
</div>

View file

@ -6,22 +6,11 @@ import { useScrollPosition } from "#/hooks/useScrollPosition";
import { useUserStore } from "../store/auth";
import { ENDPOINTS } from "#/api/config";
import { ReleaseLink169Related } from "#/components/ReleaseLink/ReleaseLink.16_9Related";
import { useSWRfetcher } from "#/api/utils";
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}`);
error.message = await res.json();
throw error;
}
return res.json();
};
export function RelatedPage(props: {id: number|string, title: string}) {
const token = useUserStore((state) => state.token);
const [isLoadingEnd, setIsLoadingEnd] = useState(false);
const getKey = (pageIndex: number, previousPageData: any) => {
if (previousPageData && !previousPageData.content.length) return null;
@ -33,7 +22,7 @@ export function RelatedPage(props: {id: number|string, title: string}) {
const { data, error, isLoading, size, setSize } = useSWRInfinite(
getKey,
fetcher,
useSWRfetcher,
{ initialSize: 1 }
);
@ -45,7 +34,6 @@ export function RelatedPage(props: {id: number|string, title: string}) {
allReleases.push(...data[i].content);
}
setContent(allReleases);
setIsLoadingEnd(true);
}
}, [data]);
@ -70,7 +58,7 @@ export function RelatedPage(props: {id: number|string, title: string}) {
return <ReleaseLink169Related {...release} key={release.id} _position={index + 1} />
})}
</div>
) : !isLoadingEnd || isLoading ? (
) : isLoading ? (
<div className="flex flex-col items-center justify-center min-w-full min-h-screen">
<Spinner />
</div>