From 40ebdcc37fb7b74c4e1a67d7420691b2ff3a1716 Mon Sep 17 00:00:00 2001 From: Wikzyy Date: Wed, 16 Apr 2025 22:26:21 +0700 Subject: [PATCH] update search filter - add debounce - set default value page to 1 each time search --- .../master/aldeias/blocks/ListToolbar.tsx | 22 ++- src/pages/master/municipios/Municipios.tsx | 2 - .../master/municipios/blocks/ListToolbar.tsx | 29 ++- .../master/municipios/blocks/SearchDialog.tsx | 177 ------------------ .../master/postoadms/PostoAdmsMaster.tsx | 2 - .../master/postoadms/blocks/ListToolbar.tsx | 42 ++--- .../master/postoadms/blocks/SearchDialog.tsx | 177 ------------------ .../master/products/blocks/ListToolbar.tsx | 31 ++- .../master/profession/blocks/ListToolbar.tsx | 34 ++-- .../master/provider/blocks/ListToolbar.tsx | 33 ++-- .../master/reward/blocks/ListToolbar.tsx | 21 ++- src/pages/master/sucos/blocks/EditDialog.tsx | 144 +++++++------- src/pages/master/sucos/blocks/ListToolbar.tsx | 36 ++-- .../master/wallet/blocks/ListToolbar.tsx | 29 ++- .../menu/manage-menu/blocks/ListToolbar.tsx | 30 ++- 15 files changed, 211 insertions(+), 598 deletions(-) delete mode 100644 src/pages/master/municipios/blocks/SearchDialog.tsx delete mode 100644 src/pages/master/postoadms/blocks/SearchDialog.tsx diff --git a/src/pages/master/aldeias/blocks/ListToolbar.tsx b/src/pages/master/aldeias/blocks/ListToolbar.tsx index f627d3f..26401b0 100644 --- a/src/pages/master/aldeias/blocks/ListToolbar.tsx +++ b/src/pages/master/aldeias/blocks/ListToolbar.tsx @@ -6,7 +6,9 @@ import React, { useEffect, useState } from 'react'; const ListToolbar = () => { const { table, reload } = useDataGrid(); const { handleAddDialog, handleSearchDialog } = useManageAldeiasContext(); - const [searchValue, setSearchValue] = useState((table.getColumn('name')?.getFilterValue() as string) ?? ''); + const [searchValue, setSearchValue] = useState( + (table.getColumn('name')?.getFilterValue() as string) ?? '' + ); const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter') { @@ -14,18 +16,20 @@ const ListToolbar = () => { } }; - useEffect(() => { - const timer = setTimeout(() => { - table.getColumn('name')?.setFilterValue(searchValue); - }, 200); - - return () => clearTimeout(timer); - }, [searchValue, table]); const handleSearch = () => { table.getColumn('name')?.setFilterValue(searchValue); table.setPageIndex(0); }; + useEffect(() => { + const timer = setTimeout(() => { + table.getColumn('name')?.setFilterValue(searchValue); + table.setPageIndex(0); + }, 200); + + return () => clearTimeout(timer); + }, [searchValue, table]); + return (
@@ -35,7 +39,7 @@ const ListToolbar = () => { setSearchValue(event.target.value)} /> diff --git a/src/pages/master/municipios/Municipios.tsx b/src/pages/master/municipios/Municipios.tsx index 253ff86..9d548e6 100644 --- a/src/pages/master/municipios/Municipios.tsx +++ b/src/pages/master/municipios/Municipios.tsx @@ -1,7 +1,6 @@ import { Container, DataGridInner } from '@/components'; import { ManageMunicipiosProvider } from './hooks/ManageMunicipiosContext'; import AddDialog from './blocks/AddDialog'; -import SearchDialog from './blocks/SearchDialog'; import EditDialog from './blocks/EditDialog'; import DeleteDialog from './blocks/DeleteDialog'; import { Breadcrumbs, Link } from '@mui/material'; @@ -35,7 +34,6 @@ const Municipios = () => { - diff --git a/src/pages/master/municipios/blocks/ListToolbar.tsx b/src/pages/master/municipios/blocks/ListToolbar.tsx index f5f046d..62092ea 100644 --- a/src/pages/master/municipios/blocks/ListToolbar.tsx +++ b/src/pages/master/municipios/blocks/ListToolbar.tsx @@ -9,23 +9,18 @@ const ListToolbar = () => { const { handleAddDialog, handleSearchDialog } = useManageMunicipiosContext(); const [searchName, setSearchName] = useState(''); const [isLoading, setIsLoading] = useState(false); - const [searchValue, setSearchValue] = useState((table.getColumn('name')?.getFilterValue() as string) ?? ''); + const [searchValue, setSearchValue] = useState( + (table.getColumn('name')?.getFilterValue() as string) ?? '' + ); - useEffect(() => { - const timer = setTimeout(() => { - table.getColumn('name')?.setFilterValue(searchValue); - }, 200); - - return () => clearTimeout(timer); - }, [searchValue, table]); - const handleFilterData = useCallback(() => { - try { - table.getColumn('name')?.setFilterValue(searchName); - } catch (error) { - toast.error('Error applying filter'); - console.error('Error applying filter:', error); - } - }, [searchName, table]); + useEffect(() => { + const timer = setTimeout(() => { + table.getColumn('name')?.setFilterValue(searchValue); + table.setPageIndex(0); + }, 200); + + return () => clearTimeout(timer); + }, [searchValue, table]); return (
@@ -36,7 +31,7 @@ const ListToolbar = () => { setSearchValue(event.target.value)} /> diff --git a/src/pages/master/municipios/blocks/SearchDialog.tsx b/src/pages/master/municipios/blocks/SearchDialog.tsx deleted file mode 100644 index 1b0142f..0000000 --- a/src/pages/master/municipios/blocks/SearchDialog.tsx +++ /dev/null @@ -1,177 +0,0 @@ -import { useRef, useState } from 'react'; -import { - Dialog, - DialogBody, - DialogContent, - DialogDescription, - DialogHeader, - DialogTitle -} from '@/components/ui/dialog'; -import { Alert, KeenIcon } from '@/components'; -import { Input } from '@/components/ui/input'; -import { Button } from '@/components/ui/button'; -import { useManageMunicipiosContext } from '../hooks/useManageMunicipiosContext'; -import { apiConfig } from '@/config/api.config'; -import axios from 'axios'; -import { - Select, - SelectContent, - SelectItem, - SelectTrigger, - SelectValue -} from '@/components/ui/select'; - -interface PostoAdmsProps { - id: number; - name: string; -} - -const API_URL = apiConfig.service_master_data; - -const SearchDialog = () => { - const parentRef = useRef(null); - const { showSearchDialog, handleSearchDialog, municipios } = useManageMunicipiosContext(); - const [alert, setAlert] = useState({ - show: false, - message: '' - }); - const initialState = { - id: 0, - name: '' - }; - - const [formField, setFormField] = useState(initialState); - const resetForm = () => { - setFormField(initialState); - }; - const [postoadms, setPostoadms] = useState([]); - const [isFound, setIsFound] = useState(false); - - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - const id = Number(formField.id); - - if (formField.id === 0) { - setAlert({ show: true, message: 'Please fill name field.' }); - return; - } - - try { - const response = await axios.get(`${API_URL}/municipios/postoadms/${id}`); - - if (response.data.status) { - setPostoadms(response.data.data); - setIsFound(true); - // console.log('Found postoadms: ', response.data.data); - } else { - setPostoadms([]); - setIsFound(false); - setAlert({ show: true, message: 'No postoadms found.' }); - } - } catch (error) { - console.error('Error fetching postoadms', error); - setAlert({ show: true, message: 'Failed to fetch postoadms. Please try again.' }); - } - setAlert({ show: false, message: '' }); - }; - - const handleReset = () => { - setFormField(initialState); - setIsFound(false); - setPostoadms([]); - }; - // console.log(municipios); - return ( - handleSearchDialog(open)}> - - - - -
-
-

- Search Postoadms -

-
-
-
{ - handleSearchDialog(false); - resetForm(); - }} - > - -
-
-
- -
- {alert.show && ( - - {alert.message} - - )} -
-
-
- - - -
- - {isFound && postoadms.length > 0 && ( -
-

Postu Administravo:

-
-
- - {postoadms.map((posto) => posto.name).join(', ')} - -
-
- )} - -
- - -
-
-
-
-
-
-
- ); -}; - -export default SearchDialog; diff --git a/src/pages/master/postoadms/PostoAdmsMaster.tsx b/src/pages/master/postoadms/PostoAdmsMaster.tsx index 90823ab..275760a 100644 --- a/src/pages/master/postoadms/PostoAdmsMaster.tsx +++ b/src/pages/master/postoadms/PostoAdmsMaster.tsx @@ -1,7 +1,6 @@ import AddDialog from './blocks/AddDialog'; import DeleteDialog from './blocks/DeleteDialog'; import EditDialog from './blocks/EditDialog'; -import SearchDialog from './blocks/SearchDialog'; import { ManagePostoAdmsContextProvider } from './hooks/ManagePostoAdmsContext'; import { Container, DataGridInner } from '@/components'; import { Breadcrumbs, Link } from '@mui/material'; @@ -37,7 +36,6 @@ const PostoAdmsMaster = () => { - diff --git a/src/pages/master/postoadms/blocks/ListToolbar.tsx b/src/pages/master/postoadms/blocks/ListToolbar.tsx index 6d2e7ca..e181848 100644 --- a/src/pages/master/postoadms/blocks/ListToolbar.tsx +++ b/src/pages/master/postoadms/blocks/ListToolbar.tsx @@ -2,12 +2,14 @@ import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components'; import { Button } from '@/components/ui/button'; import { useManagePostoAdmsContext } from '../hooks/useManagePostoAdmsContext'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; const ListToolbar = () => { const { table, reload } = useDataGrid(); const { handleAddDialog, handleSearchDialog } = useManagePostoAdmsContext(); - const [searchPosto, setSearchPosto] = useState(''); + const [searchValue, setSearchValue] = useState( + (table.getColumn('name')?.getFilterValue() as string) ?? '' + ); const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter') { @@ -16,10 +18,19 @@ const ListToolbar = () => { }; const handleSearch = () => { - table.getColumn('name')?.setFilterValue(searchPosto); + table.getColumn('name')?.setFilterValue(searchValue); table.setPageIndex(0); }; + useEffect(() => { + const timer = setTimeout(() => { + table.getColumn('name')?.setFilterValue(searchValue); + table.setPageIndex(0); + }, 200); + + return () => clearTimeout(timer); + }, [searchValue, table]); + return (
@@ -30,34 +41,15 @@ const ListToolbar = () => { setSearchPosto(event.target.value)} - onKeyDown={handleKeyDown} + value={searchValue} + onChange={(event) => setSearchValue(event.target.value)} /> - + {/* - - {/* - */} - {/* */}
- - - - - - No PostoAdms found. - - {postoAdms.map((postoAdm) => ( - { - setFormField({ - id: postoAdm.PostoAdms_id, - name: postoAdm.PostoAdms_name - }); - setOpen(false); - }} - > - {postoAdm.PostoAdms_name} - - ))} - - - - - -
- - {isFound && sucos.length > 0 && ( -
-

Sucos:

-
- - {sucos.map((suco) => suco.name).join(', ')} - -
-
- )} - -
- - -
- - - - - ); -}; - -export default SearchDialog; diff --git a/src/pages/master/products/blocks/ListToolbar.tsx b/src/pages/master/products/blocks/ListToolbar.tsx index a56506f..c8620f6 100644 --- a/src/pages/master/products/blocks/ListToolbar.tsx +++ b/src/pages/master/products/blocks/ListToolbar.tsx @@ -6,16 +6,18 @@ import { useEffect, useState } from 'react'; const ListToolbar = () => { const { table, reload } = useDataGrid(); const { handleAddDialog } = useManageProductsContext(); - const [searchValue, setSearchValue] = useState((table.getColumn('name')?.getFilterValue() as string) ?? ''); + const [searchValue, setSearchValue] = useState( + (table.getColumn('name')?.getFilterValue() as string) ?? '' + ); - useEffect(() => { - const timer = setTimeout(() => { - table.getColumn('name')?.setFilterValue(searchValue); - }, 200); - - return () => clearTimeout(timer); - }, [searchValue, table]); + useEffect(() => { + const timer = setTimeout(() => { + table.getColumn('name')?.setFilterValue(searchValue); + table.setPageIndex(0); + }, 200); + return () => clearTimeout(timer); + }, [searchValue, table]); return (
@@ -26,22 +28,11 @@ const ListToolbar = () => { setSearchValue(event.target.value)} /> - {/* - - */}
- */}
- */}
- - - - - - No Postu Administrativo Found. - - {postoadms.map((posto) => ( - { - setFormField({ - ...formField, - postoId: posto.PostoAdms_id - }); - setOpen(false); - }} - > - {posto.PostoAdms_name} - - ))} - - - - - -
-
- -
- - -
+

Loading Sucos Details...

- + ) : ( +
+
+
+
+ + setFormField({ ...formField, name: e.target.value })} + /> +
+
+ +
+
+ + + + + + + + + + No Postu Administrativo Found. + + {postoadms.map((posto) => ( + { + setFormField({ + ...formField, + postoId: posto.PostoAdms_id + }); + setOpen(false); + }} + > + {posto.PostoAdms_name} + + ))} + + + + + +
+
+ +
+ + +
+
+
+ )}
diff --git a/src/pages/master/sucos/blocks/ListToolbar.tsx b/src/pages/master/sucos/blocks/ListToolbar.tsx index 75e52e4..0ed4cab 100644 --- a/src/pages/master/sucos/blocks/ListToolbar.tsx +++ b/src/pages/master/sucos/blocks/ListToolbar.tsx @@ -1,12 +1,14 @@ import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components'; import { Button } from '@/components/ui/button'; import { useManageSucosContext } from '../hooks/useManageSucosContext'; -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; const ListToolbar = () => { const { table, reload } = useDataGrid(); const { handleAddDialog, handleSearchDialog } = useManageSucosContext(); - const [searchValue, setSearchValue] = useState(''); + const [searchValue, setSearchValue] = useState( + (table.getColumn('sucos_name')?.getFilterValue() as string) ?? '' + ); const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter') { @@ -19,6 +21,15 @@ const ListToolbar = () => { table.setPageIndex(0); }; + useEffect(() => { + const timer = setTimeout(() => { + table.getColumn('sucos_name')?.setFilterValue(searchValue); + table.setPageIndex(0); + }, 200); + + return () => clearTimeout(timer); + }, [searchValue, table]); + return (
@@ -31,32 +42,13 @@ const ListToolbar = () => { placeholder="Search Sucos" value={searchValue} onChange={(event) => setSearchValue(event.target.value)} - onKeyDown={handleKeyDown} /> - + {/* - - {/* - */} - {/* */}
- - {/* - */}
diff --git a/src/pages/menu/manage-menu/blocks/ListToolbar.tsx b/src/pages/menu/manage-menu/blocks/ListToolbar.tsx index 0820e05..91425b8 100644 --- a/src/pages/menu/manage-menu/blocks/ListToolbar.tsx +++ b/src/pages/menu/manage-menu/blocks/ListToolbar.tsx @@ -1,12 +1,14 @@ import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components'; import { useManageMenusContext } from '../hooks/useManageMenusContext'; import { Button } from '@/components/ui/button'; -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; const ListToolbar = () => { const { table, reload } = useDataGrid(); const { handleAddDialog } = useManageMenusContext(); - const [searchValue, setSearchValue] = useState(''); + const [searchValue, setSearchValue] = useState( + (table.getColumn('name')?.getFilterValue() as string) ?? '' + ); const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter') { @@ -18,6 +20,15 @@ const ListToolbar = () => { table.getColumn('name')?.setFilterValue(searchValue); }; + useEffect(() => { + const timer = setTimeout(() => { + table.getColumn('name')?.setFilterValue(searchValue); + table.setPageIndex(0); + }, 200); + + return () => clearTimeout(timer); + }, [searchValue, table]); + return (
@@ -30,25 +41,12 @@ const ListToolbar = () => { placeholder="Search Menu" value={searchValue} onChange={(event) => setSearchValue(event.target.value)} - onKeyDown={handleKeyDown} /> - + {/* - - {/* - */}