feat: Implement DataTable to backoffice pages

- Halaman Data Akun
- Halaman Validasi Transaksi
This commit is contained in:
Hafid Nur
2025-03-28 18:50:54 +07:00
parent ac2ff2ec92
commit 5b3b9e44fa
2 changed files with 149 additions and 203 deletions
+31 -100
View File
@@ -5,22 +5,19 @@ import {
FilterOutlined, FilterOutlined,
SearchOutlined, SearchOutlined,
EditOutlined, EditOutlined,
ArrowRightOutlined,
ArrowLeftOutlined,
} from '@ant-design/icons'; } from '@ant-design/icons';
import { Button, Input } from '@imphnen-frontend-service/ui/atoms'; import { Button, Input } from '@imphnen-frontend-service/ui/atoms';
import { DataTable } from '@imphnen-frontend-service/ui/organisms'; import { DataTable } from '@imphnen-frontend-service/ui/organisms';
import { import {
ColumnDef, ColumnDef,
flexRender,
getCoreRowModel, getCoreRowModel,
getPaginationRowModel, getPaginationRowModel,
PaginationState, PaginationState,
useReactTable, useReactTable,
RowSelectionState,
} from '@tanstack/react-table'; } from '@tanstack/react-table';
// Define account interface
interface Account { interface Account {
id: number; id: number;
name: string; name: string;
@@ -38,7 +35,26 @@ const mockData: Account[] = Array.from({ length: 90 }, (_, i) => ({
address: 'Jl. Pantai Cibaduyut Indonesia', address: 'Jl. Pantai Cibaduyut Indonesia',
})); }));
const columns: ColumnDef<any>[] = [ const columns: ColumnDef<Account>[] = [
{
id: 'select',
header: ({ table }) => (
<input
type="checkbox"
className="rounded"
checked={table.getIsAllRowsSelected()}
onChange={table.getToggleAllRowsSelectedHandler()}
/>
),
cell: ({ row }) => (
<input
type="checkbox"
className="rounded"
checked={row.getIsSelected()}
onChange={row.getToggleSelectedHandler()}
/>
),
},
{ {
header: 'No', header: 'No',
accessorKey: 'id', accessorKey: 'id',
@@ -61,8 +77,7 @@ const columns: ColumnDef<any>[] = [
}, },
{ {
header: 'Action', header: 'Action',
cell: ({ row }) => { cell: ({ row }) => (
return (
<Button <Button
variant="primary" variant="primary"
size="sm" size="sm"
@@ -74,8 +89,7 @@ const columns: ColumnDef<any>[] = [
> >
<EditOutlined /> Edit <EditOutlined /> Edit
</Button> </Button>
); ),
},
}, },
]; ];
@@ -85,15 +99,22 @@ export const Components: FC = (): ReactElement => {
pageSize: 9, pageSize: 9,
}); });
const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({});
const table = useReactTable({ const table = useReactTable({
data: mockData, data: mockData,
columns, columns,
state: { state: {
pagination, pagination,
rowSelection,
}, },
enableRowSelection: true,
onRowSelectionChange: setRowSelection,
getCoreRowModel: getCoreRowModel(), getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(), getPaginationRowModel: getPaginationRowModel(),
onPaginationChange: setPagination, onPaginationChange: setPagination,
pageCount: Math.ceil(mockData.length / pagination.pageSize),
manualPagination: false,
}); });
return ( return (
@@ -129,97 +150,7 @@ export const Components: FC = (): ReactElement => {
</div> </div>
{/* Table */} {/* Table */}
<DataTable data={mockData} columns={columns} /> <DataTable data={mockData} columns={columns} table={table} />
{/* Pagination */}
{/* <Pagination
currentPage={currentPage}
totalPages={Math.ceil(filteredData.length / itemsPerPage)}
onPageChange={handlePageChange}
/> */}
<div className="flex items-center justify-center gap-[40px]">
<button
className="disabled:opacity-50 cursor-pointer"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
aria-label="Previous page"
>
<ArrowLeftOutlined className="text-[16px] text-neutral-800" />
</button>
<div className="flex gap-4 items-baseline">
{table.getPageCount() <= 8 ? (
Array.from({ length: table.getPageCount() }, (_, index) => (
<button
className={`size-[30px] py-[8px] flex items-center justify-center rounded-md cursor-pointer ${
table.getState().pagination.pageIndex === index
? 'bg-primary-500 text-white'
: 'bg-primary-100 hover:bg-primary-200'
}`}
onClick={() => table.setPageIndex(index)}
>
{index + 1}
</button>
))
) : (
// Render ellipsis jika total halaman lebih dari 8
<>
<button
onClick={() => table.setPageIndex(0)}
className={`size-[30px] py-[8px] flex items-center justify-center rounded-md cursor-pointer ${
table.getState().pagination.pageIndex === 0
? 'bg-primary-500 text-white'
: 'bg-primary-100 hover:bg-primary-200'
}`}
>
1
</button>
{table.getState().pagination.pageIndex > 3 && <span>...</span>}
{Array.from(
{ length: 5 },
(_, index) =>
table.getState().pagination.pageIndex - 2 + index
)
.filter((page) => page > 0 && page < table.getPageCount() - 1)
.map((page) => (
<button
key={page}
onClick={() => table.setPageIndex(page)}
className={`size-[30px] py-[8px] flex items-center justify-center rounded-md cursor-pointer ${
table.getState().pagination.pageIndex === page
? 'bg-primary-500 text-white'
: 'bg-primary-100 hover:bg-primary-200'
}`}
>
{page + 1}
</button>
))}
{table.getState().pagination.pageIndex <
table.getPageCount() - 4 && <span>...</span>}
<button
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
className={`size-[30px] py-[8px] flex items-center justify-center rounded-md cursor-pointer ${
table.getState().pagination.pageIndex ===
table.getPageCount() - 1
? 'bg-primary-500 text-white'
: 'bg-primary-100 hover:bg-primary-200'
}`}
>
{table.getPageCount()}
</button>
</>
)}
</div>
<button
className="disabled:opacity-50 cursor-pointer"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
aria-label="Next page"
>
<ArrowRightOutlined className="text-[16px] text-neutral-800" />
</button>
</div>
</section> </section>
</main> </main>
); );
+106 -91
View File
@@ -1,18 +1,25 @@
import { FC, ReactElement, useState } from 'react'; import * as React from 'react';
import { FC, ReactElement } from 'react';
import { import {
FilterOutlined, FilterOutlined,
SearchOutlined, SearchOutlined,
FileTextOutlined,
AuditOutlined, AuditOutlined,
} from '@ant-design/icons'; } from '@ant-design/icons';
import { Button, Input } from '@imphnen-frontend-service/ui/atoms'; import { Button, Input } from '@imphnen-frontend-service/ui/atoms';
import { Pagination } from '@imphnen-frontend-service/ui/molecules';
import { DataTable } from '@imphnen-frontend-service/ui/organisms'; import { DataTable } from '@imphnen-frontend-service/ui/organisms';
// Define status type for better type safety import {
ColumnDef,
getCoreRowModel,
getPaginationRowModel,
PaginationState,
useReactTable,
RowSelectionState,
} from '@tanstack/react-table';
type TransactionStatus = 'valid' | 'invalid' | 'unchecked'; type TransactionStatus = 'valid' | 'invalid' | 'unchecked';
// Define transaction interface
interface Transaction { interface Transaction {
id: number; id: number;
name: string; name: string;
@@ -32,36 +39,103 @@ const mockTransactions: Transaction[] = Array.from({ length: 20 }, (_, i) => ({
: 'valid') as TransactionStatus, : 'valid') as TransactionStatus,
})); }));
export const Components: FC = (): ReactElement => { const columns: ColumnDef<Account>[] = [
const [searchQuery, setSearchQuery] = useState(''); {
const [currentPage, setCurrentPage] = useState(1); id: 'select',
const itemsPerPage = 9; header: ({ table }) => (
<input
// Filter data based on search query type="checkbox"
const filteredData = mockTransactions.filter( className="rounded"
(item) => checked={table.getIsAllRowsSelected()}
item.name.toLowerCase().includes(searchQuery.toLowerCase()) || onChange={table.getToggleAllRowsSelectedHandler()}
item.transactionNumber.toLowerCase().includes(searchQuery.toLowerCase()) />
),
cell: ({ row }) => (
<input
type="checkbox"
className="rounded"
checked={row.getIsSelected()}
onChange={row.getToggleSelectedHandler()}
/>
),
},
{
header: 'No',
accessorKey: 'id',
},
{
header: 'Nama Lengkap',
accessorKey: 'name',
},
{
header: 'Nomor Transaksi',
accessorKey: 'transactionNumber',
},
{
header: 'Order Valid?',
accessorKey: 'status',
cell: ({ row }) => {
const status = row.original.status;
const statusColors: Record<TransactionStatus, string> = {
valid: 'bg-success-500 text-white',
invalid: 'bg-danger-500 text-white',
unchecked: 'bg-yellow-400 text-black',
};
const statusText: Record<TransactionStatus, string> = {
valid: 'Valid',
invalid: 'Invalid',
unchecked: 'Unchecked',
};
return (
<div
className={`py-1 px-3 rounded-md text-center ${statusColors[status]}`}
>
{statusText[status]}
</div>
); );
},
},
{
header: 'Action',
cell: ({ row }) => (
<Button
variant="primary"
size="sm"
onClick={(e) => {
e.stopPropagation();
// handleUpdate(row.id);
}}
className="flex items-center gap-2 w-full"
>
<AuditOutlined className="text-[16px]" /> Update
</Button>
),
},
];
// Paginate data export const Components: FC = (): ReactElement => {
const indexOfLastItem = currentPage * itemsPerPage; const [pagination, setPagination] = React.useState<PaginationState>({
const indexOfFirstItem = indexOfLastItem - itemsPerPage; pageIndex: 0,
const currentItems = filteredData.slice(indexOfFirstItem, indexOfLastItem); pageSize: 9,
});
const handleValidate = (id: number) => { const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({});
console.log(`Validate transaction with id: ${id}`);
// Implement validation functionality
};
const handlePageChange = (pageNumber: number) => { const table = useReactTable({
setCurrentPage(pageNumber); data: mockTransactions,
}; columns,
state: {
const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => { pagination,
setSearchQuery(e.target.value); rowSelection,
setCurrentPage(1); // Reset to first page when searching },
}; enableRowSelection: true,
onRowSelectionChange: setRowSelection,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
onPaginationChange: setPagination,
pageCount: Math.ceil(mockTransactions.length / pagination.pageSize),
manualPagination: false,
});
return ( return (
<main className="w-full px-[48px] py-[40px] flex flex-col gap-8"> <main className="w-full px-[48px] py-[40px] flex flex-col gap-8">
@@ -77,8 +151,6 @@ export const Components: FC = (): ReactElement => {
<div className="relative w-full"> <div className="relative w-full">
<Input <Input
placeholder="Cari berdasarkan nama lengkap, nomor order Shopee" placeholder="Cari berdasarkan nama lengkap, nomor order Shopee"
value={searchQuery}
onChange={handleSearch}
className="pl-12 w-full max-h-full" className="pl-12 w-full max-h-full"
/> />
<div className="absolute left-3 top-1/2 transform -translate-y-1/2 text-[16px]"> <div className="absolute left-3 top-1/2 transform -translate-y-1/2 text-[16px]">
@@ -97,64 +169,7 @@ export const Components: FC = (): ReactElement => {
</div> </div>
{/* Table */} {/* Table */}
<DataTable <DataTable data={mockTransactions} columns={columns} table={table} />
data={currentItems}
headers={[
{
label: 'No.',
render: (_, index) => index + 1 + indexOfFirstItem,
},
{ label: 'Nama Lengkap', key: 'name' },
{ label: 'Nomor Transaksi', key: 'transactionNumber' },
{
label: 'Order Valid?',
render: (item: Transaction) => {
const statusColors: Record<TransactionStatus, string> = {
valid: 'bg-success-500 text-white',
invalid: 'bg-danger-500 text-white',
unchecked: 'bg-yellow-400 text-black',
};
const statusText: Record<TransactionStatus, string> = {
valid: 'Valid',
invalid: 'Invalid',
unchecked: 'Unchecked',
};
return (
<div
className={`py-1 px-3 rounded-md text-center ${
statusColors[item.status]
}`}
>
{statusText[item.status]}
</div>
);
},
},
{
label: 'Action',
render: (item: Transaction) => (
<Button
variant="primary"
size="sm"
onClick={(e) => {
e.stopPropagation();
handleValidate(item.id);
}}
className="flex items-center gap-2 w-full"
>
<AuditOutlined className="text-[16px]" /> Validate
</Button>
),
},
]}
/>
{/* Pagination */}
<Pagination
currentPage={currentPage}
totalPages={Math.ceil(filteredData.length / itemsPerPage)}
onPageChange={handlePageChange}
/>
</section> </section>
</main> </main>
); );