|
Server : LiteSpeed System : Linux server104.web-hosting.com 4.18.0-513.24.1.lve.1.el8.x86_64 #1 SMP Thu May 9 15:10:09 UTC 2024 x86_64 User : saleoqej ( 6848) PHP Version : 8.0.30 Disable Function : NONE Directory : /home/saleoqej/public_html/wp-content/plugins/extendify/src/Library/hooks/ |
import { useState } from '@wordpress/element';
import useSWRImmutable from 'swr/immutable';
import { PATTERNS_HOST } from '../../constants';
const fetcher = async () => {
const urlParams = new URLSearchParams({
wpVersion: window.extLibraryData.wpVersion || null,
lang: window.extLibraryData.wpLanguage || null,
});
return await fetch(
`${PATTERNS_HOST}/api/categories?${urlParams.toString()}`,
).then((res) => res.json());
};
export const useCategories = () => {
const [errorCount, setErrorCount] = useState(0);
const lang = window.extLibraryData?.wpLanguage ?? 'en_US';
const { data, error, isLoading } = useSWRImmutable(
`categories-${lang}`,
fetcher,
{
onError: () => setErrorCount((prev) => prev + 1),
onSuccess: () => setErrorCount(0),
},
);
return {
data,
errorCount: errorCount > 1 ? errorCount : error ? 1 : 0,
isLoading,
};
};