update method search

This commit is contained in:
wayanrivan
2025-05-06 17:45:25 +07:00
parent 4d0d6fd039
commit f0ed1cebe6
4 changed files with 162 additions and 61 deletions

View File

@ -33,9 +33,10 @@ const ListToolbar = () => {
return date.toISOString().split('T')[0];
};
const [searchValue, setSearchValue] = useState<string>(
(table.getColumn('code')?.getFilterValue() as string) ?? ''
);
const [searchValue, setSearchValue] = useState<string>();
const [typeSearchValue, setSearchTypeValue] = useState<string>();
// useEffect to set the default date values
useEffect(() => {
@ -56,6 +57,19 @@ const ListToolbar = () => {
}
}, [trxDate, table]);
useEffect(() => {
const timer = setTimeout(() => {
// Add search type filter to column filters
table.setColumnFilters((prev) => [
...prev.filter((f) => f.id !== 'searchtype'),
{ id: 'searchtype', value: typeSearchValue },
]);
table.setPageIndex(0);
}, 200);
return () => clearTimeout(timer);
}, [typeSearchValue, table]);
useEffect(() => {
const timer = setTimeout(() => {
table.getColumn('code')?.setFilterValue(searchValue);
@ -117,11 +131,27 @@ const ListToolbar = () => {
</SelectContent>
</Select>
<Select
value={typeSearchValue}
onValueChange={(value) => {
setSearchTypeValue(value);
}}
>
<SelectTrigger className="input input-sm w-[250px] h-[31px]">
<SelectValue placeholder="Select Search Type" />
</SelectTrigger>
<SelectContent>
<SelectItem value="msisdn">MSISDN</SelectItem>
<SelectItem value="fullname">FULLNAME</SelectItem>
<SelectItem value="trxid">TRANSACTION ID</SelectItem>
</SelectContent>
</Select>
<label className="input input-sm w-1/3">
<KeenIcon icon="magnifier" />
<input
type="text"
placeholder="Search Transaction ID, MSISDN, Or Full Name Customer"
placeholder={`Search ${typeSearchValue || ''}`} // Use typeSearchValue if it's set, else fallback to an empty string
value={searchValue}
onChange={(event) => setSearchValue(event.target.value)}
/>
@ -146,6 +176,8 @@ const ListToolbar = () => {
to: formatDate(today),
});
setSearchTypeValue('');
table.getColumn('code')?.setFilterValue('');
table.getColumn('status_approve')?.setFilterValue('');
table.setPageIndex(0);

View File

@ -270,19 +270,49 @@ const ApprovalTransactionProvider = ({ children }: { children: React.ReactNode }
});
}
// ✅ Declare formattedFilter early
const formattedFilter: any = {
"Transactions.transaction_date": {
from: `${startdate} 00:00:00`,
to: `${enddate} 23:59:59`
}
};
if (transactioncode) {
formattedFilter["$or"] = [
{ "Transactions.code": { like: `%${transactioncode}%` } },
{ "Transactions.msisdn": { like: `${transactioncode}` } },
{ "origin_customer.fullname": { like: `${transactioncode}` } }
];
if (Array.isArray(filter)) {
let searchType = '';
let codeValue = '';
// Ambil code dan searchtype dulu
filter.forEach((f: any) => {
if (f.id === 'code' && f.value) {
codeValue = f.value;
}
if (f.id === 'searchtype' && f.value) {
searchType = f.value;
}
if (f.id === 'transaction_date' && f.value?.from && f.value?.to) {
startdate = f.value.from;
enddate = f.value.to;
formattedFilter["Transactions.transaction_date"] = {
from: `${startdate} 00:00:00`,
to: `${enddate} 23:59:59`
};
}
});
// Terapkan berdasarkan searchtype + code
if (searchType && codeValue) {
if (searchType === "msisdn") {
formattedFilter["origin_customer.msisdn"] = { like: `%${codeValue}%` };
} else if (searchType === "fullname") {
formattedFilter["origin_customer.fullname"] = { like: `%${codeValue}%` };
} else if (searchType === "trxid") {
formattedFilter["Transactions.code"] = { like: `%${codeValue}%` };
}
}
}
// Handle approval status filter