Implement search method

This commit is contained in:
2023-10-30 14:13:27 +07:00
parent b0c5805cb5
commit 96bbe99523
3 changed files with 56 additions and 14 deletions
@@ -0,0 +1,34 @@
import { useEffect, useState } from 'react';
import styled from 'styled-components';
const StyledSearch = styled.input`
border-radius: var(--radius-sm);
border: 1px solid var(--border-color);
font-size: var(--fs-sm-x);
padding: 5px 10px;
`;
interface ISearch {
setKeyPhone: React.Dispatch<React.SetStateAction<string>>;
}
const Search = ({ setKeyPhone }: ISearch) => {
const [query, setQuery] = useState('');
useEffect(() => {
const timeOut = setTimeout(() => {
setKeyPhone(query);
}, 500);
return () => clearTimeout(timeOut);
}, [setKeyPhone, query]);
return (
<StyledSearch
onChange={(e) => setQuery(e.target.value)}
placeholder="Search by phone..."
/>
);
};
export default Search;
+8 -3
View File
@@ -3,7 +3,7 @@ import { useEffect, useState } from 'react';
// Constants // Constants
import { BASE_URL } from '../constants/path'; 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 [data, setData] = useState(null);
const [isPending, setIsPending] = useState(false); const [isPending, setIsPending] = useState(false);
const [errorMsg, setErrorMsg] = useState<string | null>(null); const [errorMsg, setErrorMsg] = useState<string | null>(null);
@@ -15,8 +15,13 @@ export const useFetch = (path: string, reload?: boolean) => {
const fetchData = async () => { const fetchData = async () => {
setIsPending(true); setIsPending(true);
// prettier-ignore
const phoneSearchQuery = keyPhone
? `?phone_like=${keyPhone}`
: '';
try { try {
const response = await fetch(BASE_URL + path); const response = await fetch(BASE_URL + path + phoneSearchQuery);
const json = await response.json(); const json = await response.json();
if (!response.ok) if (!response.ok)
@@ -34,6 +39,6 @@ export const useFetch = (path: string, reload?: boolean) => {
}; };
fetchData(); fetchData();
}, [path, reload]); }, [path, reload, keyPhone]);
return { data, isPending, errorMsg }; return { data, isPending, errorMsg };
}; };
+14 -11
View File
@@ -24,6 +24,7 @@ import Spinner from '../../commons/styles/Spinner';
// Utils // Utils
import { sendRequest } from '../../helpers/sendRequest'; import { sendRequest } from '../../helpers/sendRequest';
import Search from '../../components/Search';
interface IUserRow { interface IUserRow {
user: TUser; user: TUser;
@@ -104,7 +105,8 @@ const UserTable = ({
openFormDialog, openFormDialog,
setUser, setUser,
}: IUserTable) => { }: IUserTable) => {
const { data, isPending, errorMsg } = useFetch('users', reload); const [keyPhone, setKeyPhone] = useState('');
const { data, isPending, errorMsg } = useFetch('users', keyPhone, reload);
const [users, setUsers] = useState<TUser[]>([]); const [users, setUsers] = useState<TUser[]>([]);
useEffect(() => { useEffect(() => {
@@ -121,13 +123,14 @@ const UserTable = ({
return ( return (
<> <>
{isPending && <Spinner />} <Direction>
{users.length ? ( <StyledOperationTable>
<Direction> <Search setKeyPhone={setKeyPhone} />
<StyledOperationTable> </StyledOperationTable>
<p>Sort / Search table</p>
</StyledOperationTable>
{isPending && <Spinner />}
{users.length ? (
<Menus> <Menus>
<Table columns="10% 30% 20% 20% 10% 5%"> <Table columns="10% 30% 20% 20% 10% 5%">
<Table.Header> <Table.Header>
@@ -152,10 +155,10 @@ const UserTable = ({
/> />
</Table> </Table>
</Menus> </Menus>
</Direction> ) : (
) : ( <Message>No data to show here!</Message>
<Message>No data to show here!</Message> )}
)} </Direction>
</> </>
); );
}; };