import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components'; import { useCallApi } from '@/hooks'; import { apiConfig } from '@/config/api.config'; import { Button } from '@/components/ui/button'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { useCallback, useEffect, useState } from 'react'; import { toast } from 'sonner'; const API_URL_WALLET = apiConfig.service_wallet; interface WalletProps { ID: string; name: string; } const ListToolbar = () => { const { table, reload } = useDataGrid(); const { GetData } = useCallApi(); const [searchValue, setSearchValue] = useState( (table.getColumn('msisdn')?.getFilterValue() as string) ?? '' ); const [dateRange, setDateRange] = useState({ from: '', to: '' }); const [walletId, setWalletId] = useState( (table.getColumn('id_wallet')?.getFilterValue() as string) ?? '' ); const [wallets, setWallets] = useState([]); const formatDate = (date: Date): string => date.toISOString().split('T')[0]; useEffect(() => { const today = new Date(); const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1); setDateRange({ from: formatDate(firstDayOfMonth), to: formatDate(today) }); }, []); useEffect(() => { const timer = setTimeout(() => { table.getColumn('msisdn')?.setFilterValue(searchValue); table.setPageIndex(0); }, 200); return () => clearTimeout(timer); }, [searchValue, table]); const handleFilterByDate = useCallback(() => { try { table.getColumn('CreatedAt')?.setFilterValue(dateRange); } catch (error) { toast.error('Error applying date filter'); console.error('Error applying date filter:', error); } }, [dateRange, table]); useEffect(() => { table.getColumn('id_wallet')?.setFilterValue(walletId); table.setPageIndex(0); }, [walletId, table]); useEffect(() => { if (dateRange.from && dateRange.to) { handleFilterByDate(); } }, [dateRange, handleFilterByDate]); const fetchWallets = async () => { try { const response = await GetData(`${API_URL_WALLET}/dashboard/wallet/`, { limit: 100, page: 1, with_deleted: false, order_field: 'created_at', order_direction: 'ASC' }); setWallets(response?.data.list || []); } catch (error) { console.error('Error fetching wallets', error); } }; useEffect(() => { fetchWallets(); }, []); const handleClearAllFilters = () => { setSearchValue(''); setWalletId(''); const today = new Date(); const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1); setDateRange({ from: formatDate(firstDayOfMonth), to: formatDate(today) }); table.getAllColumns().forEach((column) => { if (column.id !== 'CreatedAt') { column.setFilterValue(undefined); } }); table.setPageIndex(0); table.getColumn('CreatedAt')?.setFilterValue({ from: formatDate(firstDayOfMonth), to: formatDate(today) }); }; return (
); }; export default ListToolbar;