diff --git a/src/pages/transaction/approval-transaction/hooks/ApprovalTransactionContext.tsx b/src/pages/transaction/approval-transaction/hooks/ApprovalTransactionContext.tsx index 3022287..39b0d25 100644 --- a/src/pages/transaction/approval-transaction/hooks/ApprovalTransactionContext.tsx +++ b/src/pages/transaction/approval-transaction/hooks/ApprovalTransactionContext.tsx @@ -95,7 +95,22 @@ const ApprovalTransactionProvider = ({ children }: { children: React.ReactNode } }, { accessorKey: 'origin_customer.fullname', - header: ({ column }) => , + header: ({ column }) => , + enableSorting: false, + enableHiding: false, + meta: { + headerClassName: 'w-[250px]', + }, + }, + { + accessorFn: (row) => { + const purchase = row?.purchase?.destination_customer.fullname; + const transfer = row?.transfer?.destination_customer.fullname; + + return purchase ?? transfer ?? "-"; + }, + id: 'destination_customer.fullname', + header: ({ column }) => , enableSorting: false, enableHiding: false, meta: { @@ -270,7 +285,7 @@ const ApprovalTransactionProvider = ({ children }: { children: React.ReactNode } }); } - + // ✅ Declare formattedFilter early const formattedFilter: any = { "Transactions.transaction_date": { diff --git a/src/pages/transaction/history-transaction/blocks/ListToolbar.tsx b/src/pages/transaction/history-transaction/blocks/ListToolbar.tsx index 9829ff1..9ec8eda 100644 --- a/src/pages/transaction/history-transaction/blocks/ListToolbar.tsx +++ b/src/pages/transaction/history-transaction/blocks/ListToolbar.tsx @@ -10,35 +10,33 @@ import { SelectTrigger, SelectValue, } from '@/components/ui/select'; +import { apiConfig } from '@/config/api.config'; +import { useCallApi } from '@/hooks'; +import { getAuth } from '@/auth'; const ListToolbar = () => { const { table, reload } = useDataGrid(); - const [trxDate, settrxDate] = useState({ from: '', to: '' }); - const [searchValue, setSearchValue] = useState(); - - // const [statusValue, setStatusValue] = useState( - // (table.getColumn('status')?.getFilterValue() as string) ?? '' - // ); - + const [searchValue, setSearchValue] = useState(''); // default: empty string + const [typeSearchValue, setSearchTypeValue] = useState(''); // default: empty string const [typeValue, setTypeValue] = useState( (table.getState().columnFilters.find(f => f.id === 'kind')?.value as string) ?? '' ); - const [typeSearchValue, setSearchTypeValue] = useState(); + const { GetData } = useCallApi(); + const API_URL = apiConfig.transaction; const formatDate = (date: Date): string => { return date.toISOString().split('T')[0]; }; - - // useEffect(() => { - // const timer = setTimeout(() => { - // table.getColumn('status')?.setFilterValue(statusValue); - // table.setPageIndex(0); - // }, 200); - // return () => clearTimeout(timer); - // }, [statusValue, table]); + useEffect(() => { + const today = new Date(); + settrxDate({ + from: formatDate(today), + to: formatDate(today), + }); + }, []); useEffect(() => { const timer = setTimeout(() => { @@ -51,10 +49,8 @@ const ListToolbar = () => { return () => clearTimeout(timer); }, [typeValue, 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 }, @@ -64,26 +60,14 @@ const ListToolbar = () => { return () => clearTimeout(timer); }, [typeSearchValue, table]); - useEffect(() => { const timer = setTimeout(() => { table.getColumn('code')?.setFilterValue(searchValue); table.setPageIndex(0); }, 200); - return () => clearTimeout(timer); }, [searchValue, table]); - - useEffect(() => { - const today = new Date(); - // const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1); - settrxDate({ - from: formatDate(today), - to: formatDate(today), - }); - }, []); - const handleFilterData = useCallback(() => { try { table.getColumn('transaction_date')?.setFilterValue(trxDate); @@ -99,6 +83,58 @@ const ListToolbar = () => { } }, [trxDate]); + const exporDataToExcel = async ( + typeSearchValue: string, + searchValue: string, + trxDate: any, + typeValue: string + ) => { + try { + const formattedFilter: any = { + "Transactions.transaction_date": { + from: `${trxDate.from} 00:00:00`, + to: `${trxDate.to} 23:59:59` + } + }; + + if (typeSearchValue === 'msisdn') { + formattedFilter['origin_customer.msisdn'] = searchValue; + } else if (typeSearchValue === 'fullname') { + formattedFilter['origin_customer.fullname'] = searchValue; + } else if (typeSearchValue === 'trxid') { + formattedFilter['Transactions.code'] = searchValue; + } + + if (typeValue) { + formattedFilter["Transactions.kind"] = typeValue; + } + + const filterParam = encodeURIComponent(JSON.stringify(formattedFilter)); + + const response = await fetch(`${API_URL}/transaction/export?filter=${filterParam}`, { + method: 'GET', + headers: { + Authorization: `Bearer ${getAuth()?.access_token}`, // ganti dengan token kamu + } + }); + + if (!response.ok) { + throw new Error('Failed to fetch file'); + } + + const blob = await response.blob(); + const contentDisposition = response.headers.get('content-disposition'); + const filenameMatch = contentDisposition?.match(/filename="?(.+)"?/); + const filename = filenameMatch ? filenameMatch[1] : 'transaction_history.xlsx'; + + return { blob, filename }; + } catch (error) { + console.error('Error exporting data:', error); + return null; + } + }; + + return ( @@ -107,7 +143,6 @@ const ListToolbar = () => { From settrxDate({ ...trxDate, from: event.target.value }) @@ -120,7 +155,6 @@ const ListToolbar = () => { To settrxDate({ ...trxDate, to: event.target.value }) @@ -129,27 +163,9 @@ const ListToolbar = () => { /> - {/* { - setStatusValue(value); - }} - > - - - - - COMPLETE - FAILED - ON PROCESS - - */} - { - setTypeValue(value); - }} + onValueChange={(value) => setTypeValue(value)} > @@ -160,15 +176,13 @@ const ListToolbar = () => { WITHDRAW TOP UP RETURN - TOP UP PARTNER + TOP UP PARTNER { - setSearchTypeValue(value); - }} + onValueChange={(value) => setSearchTypeValue(value)} > @@ -184,37 +198,59 @@ const ListToolbar = () => { setSearchValue(event.target.value)} /> - + + { + const result = await exporDataToExcel( + typeSearchValue || '', + searchValue || '', + trxDate || {}, + typeValue || '' + ); + + if (result) { + const url = window.URL.createObjectURL(result.blob); + const a = document.createElement('a'); + a.href = url; + a.download = result.filename; + a.click(); + window.URL.revokeObjectURL(url); + } else { + toast.error('Failed to export data'); + } + }} + > + Export Data + + + + { - // Preserve trxDate values (from, to) and reset others const today = new Date(); - // const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1); - // Only reset filters excluding from and to setSearchValue(''); setSearchTypeValue(''); - // setStatusValue(''); setTypeValue(''); settrxDate({ from: formatDate(today), to: formatDate(today), }); - // Reset other filters except for date range table.setColumnFilters([ { id: 'code', value: '' }, - // { id: 'status', value: '' }, { id: 'kind', value: '' }, ]); diff --git a/src/pages/transaction/history-transaction/hooks/TransactionContext.tsx b/src/pages/transaction/history-transaction/hooks/TransactionContext.tsx index 8e78c1c..39bc60f 100644 --- a/src/pages/transaction/history-transaction/hooks/TransactionContext.tsx +++ b/src/pages/transaction/history-transaction/hooks/TransactionContext.tsx @@ -107,13 +107,28 @@ const TransactionProvider = ({ children }: { children: React.ReactNode }) => { }, { accessorKey: 'origin_customer.fullname', - header: ({ column }) => , + header: ({ column }) => , enableSorting: false, enableHiding: false, meta: { headerClassName: 'w-[250px]' } }, + { + accessorFn: (row) => { + const purchase = row?.purchase?.destination_customer.fullname; + const transfer = row?.transfer?.destination_customer.fullname; + + return purchase ?? transfer ?? "-"; + }, + id: 'destination_customer.fullname', + header: ({ column }) => , + enableSorting: false, + enableHiding: false, + meta: { + headerClassName: 'w-[250px]', + }, + }, { accessorFn: (row) => { const purchaseAmount = row?.purchase?.amount;