mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 13:38:51 +00:00
Implement search method
This commit is contained in:
@@ -3,7 +3,7 @@ import { useEffect, useState } from 'react';
|
||||
// Constants
|
||||
import { BASE_URL } from '../constants/path';
|
||||
|
||||
export const useFetch = (path: string, reload?: boolean) => {
|
||||
export const useFetch = (path: string, keyPhone: string, reload?: boolean) => {
|
||||
const [data, setData] = useState(null);
|
||||
const [isPending, setIsPending] = useState(false);
|
||||
const [errorMsg, setErrorMsg] = useState<string | null>(null);
|
||||
@@ -15,8 +15,13 @@ export const useFetch = (path: string, reload?: boolean) => {
|
||||
const fetchData = async () => {
|
||||
setIsPending(true);
|
||||
|
||||
// prettier-ignore
|
||||
const phoneSearchQuery = keyPhone
|
||||
? `?phone_like=${keyPhone}`
|
||||
: '';
|
||||
|
||||
try {
|
||||
const response = await fetch(BASE_URL + path);
|
||||
const response = await fetch(BASE_URL + path + phoneSearchQuery);
|
||||
const json = await response.json();
|
||||
|
||||
if (!response.ok)
|
||||
@@ -34,6 +39,6 @@ export const useFetch = (path: string, reload?: boolean) => {
|
||||
};
|
||||
|
||||
fetchData();
|
||||
}, [path, reload]);
|
||||
}, [path, reload, keyPhone]);
|
||||
return { data, isPending, errorMsg };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user