import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components'; import { useTransactionContext } from '../hooks/useApprovalTransactionContext'; import { Button } from '@/components/ui/button'; import { useCallback, useState, useEffect } from 'react'; import { toast } from 'sonner'; const ListToolbar = () => { const { table, reload } = useDataGrid(); // Set the initial state for trxDate const [trxDate, settrxDate] = useState({ from: '', to: '' }); // Function to format date to YYYY-MM-DD const formatDate = (date: Date): string => { return date.toISOString().split('T')[0]; }; const [searchValue, setSearchValue] = useState( (table.getColumn('code')?.getFilterValue() as string) ?? '' ); // useEffect to set the default date values useEffect(() => { const today = new Date(); const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1); settrxDate({ from: formatDate(firstDayOfMonth), to: formatDate(today), }); }, []); const handleFilterData = useCallback(() => { try { table.getColumn('transaction_date')?.setFilterValue(trxDate); } catch (error) { toast.error('Error applying filter'); console.error('Error applying filter:', error); } }, [trxDate, table]); useEffect(() => { const timer = setTimeout(() => { table.getColumn('code')?.setFilterValue(searchValue); table.setPageIndex(0); }, 200); return () => clearTimeout(timer); }, [searchValue, table]); useEffect(() => { if (trxDate.from && trxDate.to) { handleFilterData(); } }, [trxDate]); return (
); }; export default ListToolbar;