import { createFileRoute, useNavigate } from '@tanstack/react-router'; import * as React from 'react'; import { Search, Pencil, Trash2, Plus } from 'lucide-react'; import { Button, Card, CardContent, CardHeader, Input, } from '@imphnen-frontend-service/ui/atoms'; import { DataTable, BackofficeWrapper, } from '@imphnen-frontend-service/ui/organisms'; import { ColumnDef, getCoreRowModel, getPaginationRowModel, PaginationState, useReactTable, RowSelectionState, } from '@tanstack/react-table'; import { useGachaItemList, useDeleteGachaItem, TGachaItemDto, } from '@imphnen-frontend-service/service'; import { toast } from 'sonner'; import { SelectAllCheckbox, RowSelectCheckbox, DeleteConfirmDialog, } from '../../components/list-helpers'; export const Route = createFileRoute('/_authenticated/gacha-roll')({ component: GachaRollPage, }); function GachaRollPage() { const navigate = useNavigate(); const [search, setSearch] = React.useState(''); const [deleteId, setDeleteId] = React.useState(null); const [pagination, setPagination] = React.useState({ pageIndex: 0, pageSize: 10, }); const [rowSelection, setRowSelection] = React.useState({}); const { data: itemsData, isLoading } = useGachaItemList({ search, page: pagination.pageIndex + 1, per_page: pagination.pageSize, }); const deleteItem = useDeleteGachaItem(); const items: TGachaItemDto[] = itemsData?.data ?? []; const totalItems = itemsData?.meta?.total ?? items.length; const handleDelete = async (id: string) => { try { await deleteItem.mutateAsync(id); toast.success('Item berhasil dihapus'); setDeleteId(null); } catch (error) { console.log(error); toast.error('Item gagal dihapus'); } }; const columns: ColumnDef[] = [ { id: 'select', header: ({ table }) => , cell: ({ row }) => , }, { header: 'No', accessorKey: 'id' }, { header: 'Nama Item', accessorKey: 'name' }, { header: 'Action', cell: ({ row }) => (
), }, ]; const table = useReactTable({ data: items, columns, state: { pagination, rowSelection }, enableRowSelection: true, onRowSelectionChange: setRowSelection, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), onPaginationChange: setPagination, pageCount: Math.ceil(totalItems / pagination.pageSize), manualPagination: true, }); return (
setSearch(e.target.value)} />
{isLoading ? (
Memuat data…
) : ( setPagination((prev) => ({ ...prev, pageIndex: p - 1 })) } /> )}
!o && setDeleteId(null)} onConfirm={() => deleteId && handleDelete(deleteId)} title="Hapus item gacha ini?" />
); }