Files
imphnen-frontend-service/apps/backoffice/src/routes/_authenticated/gacha-roll.tsx
T

182 lines
5.0 KiB
TypeScript
Raw Normal View History

import { createFileRoute, useNavigate } from '@tanstack/react-router';
import * as React from 'react';
import { Search, Pencil, Trash2, Plus } from 'lucide-react';
2025-04-05 06:07:26 +07:00
import {
Button,
Card,
CardContent,
CardHeader,
Input,
} from '@imphnen-frontend-service/ui/atoms';
import {
DataTable,
BackofficeWrapper,
} from '@imphnen-frontend-service/ui/organisms';
2025-04-05 06:07:26 +07:00
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';
2025-04-05 06:07:26 +07:00
export const Route = createFileRoute('/_authenticated/gacha-roll')({
component: GachaRollPage,
});
function GachaRollPage() {
const navigate = useNavigate();
const [search, setSearch] = React.useState('');
const [deleteId, setDeleteId] = React.useState<string | null>(null);
2025-04-05 06:07:26 +07:00
const [pagination, setPagination] = React.useState<PaginationState>({
pageIndex: 0,
pageSize: 10,
});
const [rowSelection, setRowSelection] =
React.useState<RowSelectionState>({});
2025-04-05 06:07:26 +07:00
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<TGachaItemDto>[] = [
2025-04-05 06:07:26 +07:00
{
id: 'select',
header: ({ table }) => <SelectAllCheckbox table={table} />,
cell: ({ row }) => <RowSelectCheckbox row={row} />,
2025-04-05 06:07:26 +07:00
},
{ header: 'No', accessorKey: 'id' },
{ header: 'Nama Item', accessorKey: 'name' },
2025-04-05 06:07:26 +07:00
{
header: 'Action',
cell: ({ row }) => (
<div className="flex items-center gap-2">
2025-04-05 06:07:26 +07:00
<Button
variant="secondary"
2025-04-05 06:07:26 +07:00
size="sm"
onClick={(e) => {
e.stopPropagation();
navigate({
to: '/gacha-roll/$id',
params: { id: row.original.id },
});
2025-04-05 06:07:26 +07:00
}}
>
<Pencil className="size-3.5" />
Update
</Button>
<Button
variant="danger"
size="sm"
onClick={(e) => {
e.stopPropagation();
setDeleteId(row.original.id);
}}
>
<Trash2 className="size-3.5" />
Delete
2025-04-05 06:07:26 +07:00
</Button>
</div>
),
},
];
2025-04-05 06:07:26 +07:00
const table = useReactTable({
data: items,
2025-04-05 06:07:26 +07:00
columns,
state: { pagination, rowSelection },
2025-04-05 06:07:26 +07:00
enableRowSelection: true,
onRowSelectionChange: setRowSelection,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
onPaginationChange: setPagination,
pageCount: Math.ceil(totalItems / pagination.pageSize),
manualPagination: true,
});
2025-04-05 06:07:26 +07:00
return (
<BackofficeWrapper
title="Gacha Roll"
description="Kelola item hadiah gacha"
>
<Card>
<CardHeader>
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
<div className="relative w-full sm:max-w-sm">
<Search className="absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
2025-04-05 06:07:26 +07:00
<Input
placeholder="Cari nama item…"
className="pl-9"
value={search}
onChange={(e) => setSearch(e.target.value)}
2025-04-05 06:07:26 +07:00
/>
</div>
<Button
onClick={() => navigate({ to: '/gacha-roll/create' })}
size="md"
>
<Plus className="size-4" />
Tambah Item
</Button>
2025-04-05 06:07:26 +07:00
</div>
</CardHeader>
<CardContent>
{isLoading ? (
<div className="py-10 text-center text-sm text-muted-foreground">
Memuat data
</div>
) : (
<DataTable
data={items}
columns={columns}
table={table}
manualPagination
pageCount={Math.ceil(totalItems / pagination.pageSize)}
currentPage={pagination.pageIndex + 1}
onPageChange={(p) =>
setPagination((prev) => ({ ...prev, pageIndex: p - 1 }))
}
/>
)}
</CardContent>
</Card>
<DeleteConfirmDialog
open={!!deleteId}
onOpenChange={(o) => !o && setDeleteId(null)}
onConfirm={() => deleteId && handleDelete(deleteId)}
title="Hapus item gacha ini?"
/>
</BackofficeWrapper>
);
}