mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 13:38:51 +00:00
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
// Constants
|
|
import { BASE_URL } from '../constants/path';
|
|
import { searchQuery } from '../helpers/utils';
|
|
|
|
export const useFetch = (
|
|
path: string,
|
|
phoneNum: string,
|
|
sortBy: string,
|
|
orderBy: string,
|
|
reload?: boolean,
|
|
) => {
|
|
const [data, setData] = useState(null);
|
|
const [isPending, setIsPending] = useState(false);
|
|
const [errorMsg, setErrorMsg] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
// Clear data
|
|
setData(null);
|
|
|
|
const fetchData = async () => {
|
|
setIsPending(true);
|
|
|
|
const query = searchQuery(phoneNum, sortBy, orderBy);
|
|
|
|
try {
|
|
const response = await fetch(BASE_URL + path + '?' + query);
|
|
const json = await response.json();
|
|
|
|
if (!response.ok)
|
|
throw new Error(
|
|
`Error code: ${response.status} \n Messages: ${response.statusText}`,
|
|
);
|
|
|
|
setIsPending(false);
|
|
setData(json);
|
|
setErrorMsg(null);
|
|
} catch (error) {
|
|
setErrorMsg(`Could not fetch data.\n ${error}`);
|
|
setIsPending(false);
|
|
}
|
|
};
|
|
|
|
fetchData();
|
|
}, [path, reload, phoneNum, orderBy, sortBy]);
|
|
return { data, isPending, errorMsg };
|
|
};
|