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

@ -5,7 +5,7 @@ import { Spinner } from "../Spinner/Spinner";
import useSWR from "swr";
import { ENDPOINTS } from "#/api/config";
import { useEffect, useState } from "react";
import { b64toBlob, unixToDate } from "#/api/utils";
import { b64toBlob, unixToDate, useSWRfetcher } from "#/api/utils";
import { ProfileEditPrivacyModal } from "./Profile.EditPrivacyModal";
import { ProfileEditStatusModal } from "./Profile.EditStatusModal";
import { ProfileEditSocialModal } from "./Profile.EditSocialModal";
@ -14,20 +14,6 @@ import { useSWRConfig } from "swr";
import { useUserStore } from "#/store/auth";
import { ProfileEditLoginModal } from "./Profile.EditLoginModal";
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 const ProfileEditModal = (props: {
isOpen: boolean;
setIsOpen: (isOpen: boolean) => void;
@ -70,7 +56,7 @@ export const ProfileEditModal = (props: {
};
function useFetchInfo(url: string) {
const { data, isLoading, error } = useSWR(url, fetcher);
const { data, isLoading, error } = useSWR(url, useSWRfetcher);
return [data, isLoading, error];
}

View file

@ -11,7 +11,7 @@ import type {
FlowbiteCarouselControlTheme,
} from "flowbite-react";
import Image from "next/image";
import { unixToDate } from "#/api/utils";
import { unixToDate, useSWRfetcher } from "#/api/utils";
import Link from "next/link";
import { useCallback, useEffect, useState } from "react";
import { ENDPOINTS } from "#/api/config";
@ -95,7 +95,6 @@ const ProfileReleaseRatingsModal = (props: {
profile_id: number;
token: string | null;
}) => {
const [isLoadingEnd, setIsLoadingEnd] = useState(false);
const [currentRef, setCurrentRef] = useState<any>(null);
const modalRef = useCallback((ref) => {
setCurrentRef(ref);
@ -110,23 +109,9 @@ const ProfileReleaseRatingsModal = (props: {
return url;
};
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();
};
const { data, error, isLoading, size, setSize } = useSWRInfinite(
getKey,
fetcher,
useSWRfetcher,
{ initialSize: 2 }
);
@ -138,7 +123,6 @@ const ProfileReleaseRatingsModal = (props: {
allReleases.push(...data[i].content);
}
setContent(allReleases);
setIsLoadingEnd(true);
}
}, [data]);
@ -170,8 +154,8 @@ const ProfileReleaseRatingsModal = (props: {
onScroll={handleScroll}
ref={modalRef}
>
{!isLoadingEnd && isLoading && <Spinner />}
{isLoadingEnd && !isLoading && content.length > 0 ? (
{isLoading && <Spinner />}
{content && content.length > 0 ? (
content.map((release) => {
return (
<Link

View file

@ -3,6 +3,7 @@ import { ENDPOINTS } from "#/api/config";
import Link from "next/link";
import useSWRInfinite from "swr/infinite";
import { useCallback, useEffect, useState } from "react";
import { useSWRfetcher } from "#/api/utils";
const lists = [
{ list: 0, name: "Не смотрю" },
@ -124,20 +125,6 @@ export const ReleaseInfoUserList = (props: {
);
};
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();
};
const AddReleaseToCollectionModal = (props: {
isOpen: boolean;
setIsOpen: (isopen: boolean) => void;
@ -153,7 +140,7 @@ const AddReleaseToCollectionModal = (props: {
const { data, error, isLoading, size, setSize } = useSWRInfinite(
getKey,
fetcher,
useSWRfetcher,
{ initialSize: 2 }
);