update date clear when search form input type text filled
This commit is contained in:
@ -14,13 +14,11 @@ import {
|
|||||||
const ListToolbar = () => {
|
const ListToolbar = () => {
|
||||||
const { table, reload } = useDataGrid();
|
const { table, reload } = useDataGrid();
|
||||||
|
|
||||||
// State untuk filter
|
|
||||||
const [trxDate, settrxDate] = useState({ from: '', to: '' });
|
const [trxDate, settrxDate] = useState({ from: '', to: '' });
|
||||||
const [statusApproval, setStatusApproval] = useState<string>('');
|
const [statusApproval, setStatusApproval] = useState<string>('');
|
||||||
const [searchValue, setSearchValue] = useState<string>('');
|
const [searchValue, setSearchValue] = useState<string>('');
|
||||||
const [typeSearchValue, setSearchTypeValue] = useState<string>('');
|
const [typeSearchValue, setSearchTypeValue] = useState<string>('');
|
||||||
|
|
||||||
// Set tanggal default saat pertama mount
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
const formatDate = (date: Date) => date.toISOString().split('T')[0];
|
const formatDate = (date: Date) => date.toISOString().split('T')[0];
|
||||||
@ -30,28 +28,21 @@ const ListToolbar = () => {
|
|||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Fungsi untuk apply semua filter sekaligus saat tombol Filter ditekan
|
|
||||||
const handleFilterData = useCallback(() => {
|
const handleFilterData = useCallback(() => {
|
||||||
try {
|
try {
|
||||||
if (!trxDate.from || !trxDate.to) {
|
// if (!trxDate.from || !trxDate.to) {
|
||||||
toast.error('Please select From and To dates');
|
// toast.error('Please select From and To dates');
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Set filter tanggal
|
|
||||||
table.getColumn('transaction_date')?.setFilterValue(trxDate);
|
table.getColumn('transaction_date')?.setFilterValue(trxDate);
|
||||||
|
|
||||||
// Set filter status approval
|
|
||||||
table.getColumn('status_approve')?.setFilterValue(statusApproval || '');
|
table.getColumn('status_approve')?.setFilterValue(statusApproval || '');
|
||||||
|
|
||||||
// Set filter search type sebagai column filter id 'searchtype'
|
|
||||||
table.setColumnFilters((prevFilters) => {
|
table.setColumnFilters((prevFilters) => {
|
||||||
// Hapus dulu filter dengan id 'searchtype' dan 'code' agar gak duplikat
|
|
||||||
const filtered = prevFilters.filter(
|
const filtered = prevFilters.filter(
|
||||||
(f) => f.id !== 'searchtype' && f.id !== 'code'
|
(f) => f.id !== 'searchtype' && f.id !== 'code'
|
||||||
);
|
);
|
||||||
|
|
||||||
// Tambahkan filter baru jika ada
|
|
||||||
if (typeSearchValue) {
|
if (typeSearchValue) {
|
||||||
filtered.push({ id: 'searchtype', value: typeSearchValue });
|
filtered.push({ id: 'searchtype', value: typeSearchValue });
|
||||||
}
|
}
|
||||||
@ -62,7 +53,6 @@ const ListToolbar = () => {
|
|||||||
return filtered;
|
return filtered;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Reset ke halaman pertama
|
|
||||||
table.setPageIndex(0);
|
table.setPageIndex(0);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error('Error applying filter');
|
toast.error('Error applying filter');
|
||||||
@ -79,7 +69,9 @@ const ListToolbar = () => {
|
|||||||
<input
|
<input
|
||||||
type="date"
|
type="date"
|
||||||
value={trxDate.from}
|
value={trxDate.from}
|
||||||
onChange={(e) => settrxDate({ ...trxDate, from: e.target.value })}
|
onChange={(event) =>
|
||||||
|
settrxDate({ ...trxDate, from: event.target.value })
|
||||||
|
}
|
||||||
name="from"
|
name="from"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
@ -89,7 +81,9 @@ const ListToolbar = () => {
|
|||||||
<input
|
<input
|
||||||
type="date"
|
type="date"
|
||||||
value={trxDate.to}
|
value={trxDate.to}
|
||||||
onChange={(e) => settrxDate({ ...trxDate, to: e.target.value })}
|
onChange={(event) =>
|
||||||
|
settrxDate({ ...trxDate, to: event.target.value })
|
||||||
|
}
|
||||||
name="to"
|
name="to"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
@ -110,7 +104,10 @@ const ListToolbar = () => {
|
|||||||
|
|
||||||
<Select
|
<Select
|
||||||
value={typeSearchValue}
|
value={typeSearchValue}
|
||||||
onValueChange={setSearchTypeValue}
|
onValueChange={(value) => {
|
||||||
|
setSearchTypeValue(value);
|
||||||
|
settrxDate({ from: '', to: '' }); // clear tanggal saat search type dipilih
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<SelectTrigger className="input input-sm w-[250px] h-[31px]">
|
<SelectTrigger className="input input-sm w-[250px] h-[31px]">
|
||||||
<SelectValue placeholder="Select Search Type" />
|
<SelectValue placeholder="Select Search Type" />
|
||||||
@ -132,7 +129,6 @@ const ListToolbar = () => {
|
|||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
|
|
||||||
{/* Tombol Filter */}
|
|
||||||
<Button onClick={handleFilterData}>
|
<Button onClick={handleFilterData}>
|
||||||
Filter
|
Filter
|
||||||
</Button>
|
</Button>
|
||||||
@ -147,13 +143,11 @@ const ListToolbar = () => {
|
|||||||
const today = new Date();
|
const today = new Date();
|
||||||
const formatDate = (date: Date) => date.toISOString().split('T')[0];
|
const formatDate = (date: Date) => date.toISOString().split('T')[0];
|
||||||
|
|
||||||
// Reset semua filter dan tanggal ke hari ini
|
|
||||||
setSearchValue('');
|
setSearchValue('');
|
||||||
setStatusApproval('');
|
setStatusApproval('');
|
||||||
settrxDate({ from: formatDate(today), to: formatDate(today) });
|
settrxDate({ from: formatDate(today), to: formatDate(today) });
|
||||||
setSearchTypeValue('');
|
setSearchTypeValue('');
|
||||||
|
|
||||||
// Reset filter di tabel
|
|
||||||
table.getColumn('code')?.setFilterValue('');
|
table.getColumn('code')?.setFilterValue('');
|
||||||
table.getColumn('status_approve')?.setFilterValue('');
|
table.getColumn('status_approve')?.setFilterValue('');
|
||||||
table.getColumn('transaction_date')?.setFilterValue('');
|
table.getColumn('transaction_date')?.setFilterValue('');
|
||||||
|
|||||||
@ -28,31 +28,29 @@ const ListToolbar = () => {
|
|||||||
|
|
||||||
const formatDate = (date: Date): string => date.toISOString().split('T')[0];
|
const formatDate = (date: Date): string => date.toISOString().split('T')[0];
|
||||||
|
|
||||||
// Set tanggal default hari ini saat mount
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
const formatted = formatDate(today);
|
const formatted = formatDate(today);
|
||||||
settrxDate({ from: formatted, to: formatted });
|
settrxDate({ from: formatted, to: formatted });
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// **Hilangkan semua useEffect yang auto apply filter saat input berubah**
|
// ✅ Reset tanggal jika typeSearchValue dipilih
|
||||||
// Karena kamu mau filter apply hanya lewat tombol Filter
|
useEffect(() => {
|
||||||
|
if (typeSearchValue) {
|
||||||
// Fungsi untuk apply SEMUA filter sekaligus saat tombol Filter diklik
|
settrxDate({ from: '', to: '' });
|
||||||
const handleFilterData = useCallback(() => {
|
|
||||||
if (!trxDate.from || !trxDate.to) {
|
|
||||||
toast.error('Please select both From and To dates');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}, [typeSearchValue]);
|
||||||
|
|
||||||
|
const handleFilterData = useCallback(() => {
|
||||||
|
// if (!trxDate.from || !trxDate.to) {
|
||||||
|
// toast.error('Please select both From and To dates');
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
// Terapkan filter tanggal
|
|
||||||
table.getColumn('transaction_date')?.setFilterValue(trxDate);
|
table.getColumn('transaction_date')?.setFilterValue(trxDate);
|
||||||
|
|
||||||
// Terapkan filter jenis transaksi (kind)
|
|
||||||
table.setColumnFilters((prev) => {
|
table.setColumnFilters((prev) => {
|
||||||
// Hapus filter 'kind' dan 'searchtype' agar bisa set ulang
|
|
||||||
const others = prev.filter(f => f.id !== 'kind' && f.id !== 'searchtype' && f.id !== 'code');
|
const others = prev.filter(f => f.id !== 'kind' && f.id !== 'searchtype' && f.id !== 'code');
|
||||||
// Bangun array baru dengan filter yang diinginkan
|
|
||||||
const filters: any[] = [...others];
|
const filters: any[] = [...others];
|
||||||
|
|
||||||
if (typeValue) filters.push({ id: 'kind', value: typeValue });
|
if (typeValue) filters.push({ id: 'kind', value: typeValue });
|
||||||
@ -146,6 +144,7 @@ const ListToolbar = () => {
|
|||||||
name="to"
|
name="to"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
|
|
||||||
<Select value={typeValue} onValueChange={(value) => setTypeValue(value)}>
|
<Select value={typeValue} onValueChange={(value) => setTypeValue(value)}>
|
||||||
<SelectTrigger className="input input-sm w-[250px] h-[31px]">
|
<SelectTrigger className="input input-sm w-[250px] h-[31px]">
|
||||||
<SelectValue placeholder="Select Transaction Type" />
|
<SelectValue placeholder="Select Transaction Type" />
|
||||||
@ -197,7 +196,6 @@ const ListToolbar = () => {
|
|||||||
<Button className="h-7.5" onClick={handleFilterData}>
|
<Button className="h-7.5" onClick={handleFilterData}>
|
||||||
Filter
|
Filter
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="ml-auto flex gap-2">
|
<div className="ml-auto flex gap-2">
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import { useNavigate } from 'react-router';
|
|||||||
import DetailTransaction from '../blocks/DetailTransaction';
|
import DetailTransaction from '../blocks/DetailTransaction';
|
||||||
import ResendTransaction from '../blocks/ResendTransaction';
|
import ResendTransaction from '../blocks/ResendTransaction';
|
||||||
import RollbackTransaction from '../blocks/RollbackTransaction';
|
import RollbackTransaction from '../blocks/RollbackTransaction';
|
||||||
|
import { start } from 'repl';
|
||||||
|
|
||||||
interface TransactionProps {
|
interface TransactionProps {
|
||||||
id: number;
|
id: number;
|
||||||
@ -326,14 +327,14 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
searchType = f.value;
|
searchType = f.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (f.id === 'transaction_date' && f.value?.from && f.value?.to) {
|
// if (f.id === 'transaction_date' && f.value?.from && f.value?.to) {
|
||||||
startdate = f.value.from;
|
// startdate = f.value.from;
|
||||||
enddate = f.value.to;
|
// enddate = f.value.to;
|
||||||
formattedFilter["Transactions.transaction_date"] = {
|
// formattedFilter["Transactions.transaction_date"] = {
|
||||||
from: `${startdate} 00:00:00`,
|
// from: `${startdate} 00:00:00`,
|
||||||
to: `${enddate} 23:59:59`
|
// to: `${enddate} 23:59:59`
|
||||||
};
|
// };
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (f.id === 'kind' && f.value) {
|
if (f.id === 'kind' && f.value) {
|
||||||
type = f.value;
|
type = f.value;
|
||||||
|
|||||||
Reference in New Issue
Block a user