2026-04-12 23:01:47 +07:00
|
|
|
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 {
|
2026-04-12 23:01:47 +07:00
|
|
|
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,
|
2026-04-12 23:01:47 +07:00
|
|
|
} from '@tanstack/react-table';
|
2026-04-05 17:08:42 +07:00
|
|
|
import {
|
|
|
|
|
useGachaItemList,
|
|
|
|
|
useDeleteGachaItem,
|
|
|
|
|
TGachaItemDto,
|
2026-04-12 23:01:47 +07:00
|
|
|
} 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
|
|
|
|
2026-04-11 17:04:14 +07:00
|
|
|
export const Route = createFileRoute('/_authenticated/gacha-roll')({
|
|
|
|
|
component: GachaRollPage,
|
2026-04-12 23:01:47 +07:00
|
|
|
});
|
2026-04-11 17:04:14 +07:00
|
|
|
|
|
|
|
|
function GachaRollPage() {
|
2026-04-12 23:01:47 +07:00
|
|
|
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,
|
2026-04-12 23:01:47 +07:00
|
|
|
pageSize: 10,
|
|
|
|
|
});
|
|
|
|
|
const [rowSelection, setRowSelection] =
|
|
|
|
|
React.useState<RowSelectionState>({});
|
2025-04-05 06:07:26 +07:00
|
|
|
|
2026-04-05 17:08:42 +07:00
|
|
|
const { data: itemsData, isLoading } = useGachaItemList({
|
|
|
|
|
search,
|
|
|
|
|
page: pagination.pageIndex + 1,
|
|
|
|
|
per_page: pagination.pageSize,
|
2026-04-12 23:01:47 +07:00
|
|
|
});
|
|
|
|
|
const deleteItem = useDeleteGachaItem();
|
2026-04-05 17:08:42 +07:00
|
|
|
|
2026-04-12 23:01:47 +07:00
|
|
|
const items: TGachaItemDto[] = itemsData?.data ?? [];
|
|
|
|
|
const totalItems = itemsData?.meta?.total ?? items.length;
|
2026-04-05 17:08:42 +07:00
|
|
|
|
2026-04-11 22:02:57 +07:00
|
|
|
const handleDelete = async (id: string) => {
|
|
|
|
|
try {
|
2026-04-12 23:01:47 +07:00
|
|
|
await deleteItem.mutateAsync(id);
|
|
|
|
|
toast.success('Item berhasil dihapus');
|
|
|
|
|
setDeleteId(null);
|
2026-04-11 22:02:57 +07:00
|
|
|
} catch (error) {
|
2026-04-12 23:01:47 +07:00
|
|
|
console.log(error);
|
|
|
|
|
toast.error('Item gagal dihapus');
|
2026-04-05 17:08:42 +07:00
|
|
|
}
|
2026-04-12 23:01:47 +07:00
|
|
|
};
|
2026-04-05 17:08:42 +07:00
|
|
|
|
|
|
|
|
const columns: ColumnDef<TGachaItemDto>[] = [
|
2025-04-05 06:07:26 +07:00
|
|
|
{
|
|
|
|
|
id: 'select',
|
2026-04-12 23:01:47 +07:00
|
|
|
header: ({ table }) => <SelectAllCheckbox table={table} />,
|
|
|
|
|
cell: ({ row }) => <RowSelectCheckbox row={row} />,
|
2025-04-05 06:07:26 +07:00
|
|
|
},
|
2026-04-12 23:01:47 +07:00
|
|
|
{ header: 'No', accessorKey: 'id' },
|
|
|
|
|
{ header: 'Nama Item', accessorKey: 'name' },
|
2025-04-05 06:07:26 +07:00
|
|
|
{
|
|
|
|
|
header: 'Action',
|
2026-04-05 17:08:42 +07:00
|
|
|
cell: ({ row }) => (
|
2026-04-12 23:01:47 +07:00
|
|
|
<div className="flex items-center gap-2">
|
2025-04-05 06:07:26 +07:00
|
|
|
<Button
|
2026-04-12 23:01:47 +07:00
|
|
|
variant="secondary"
|
2025-04-05 06:07:26 +07:00
|
|
|
size="sm"
|
|
|
|
|
onClick={(e) => {
|
2026-04-12 23:01:47 +07:00
|
|
|
e.stopPropagation();
|
|
|
|
|
navigate({
|
|
|
|
|
to: '/gacha-roll/$id',
|
|
|
|
|
params: { id: row.original.id },
|
|
|
|
|
});
|
2025-04-05 06:07:26 +07:00
|
|
|
}}
|
|
|
|
|
>
|
2026-04-12 23:01:47 +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>
|
|
|
|
|
),
|
|
|
|
|
},
|
2026-04-12 23:01:47 +07:00
|
|
|
];
|
2025-04-05 06:07:26 +07:00
|
|
|
|
|
|
|
|
const table = useReactTable({
|
2026-04-05 17:08:42 +07:00
|
|
|
data: items,
|
2025-04-05 06:07:26 +07:00
|
|
|
columns,
|
2026-04-12 23:01:47 +07:00
|
|
|
state: { pagination, rowSelection },
|
2025-04-05 06:07:26 +07:00
|
|
|
enableRowSelection: true,
|
|
|
|
|
onRowSelectionChange: setRowSelection,
|
|
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
|
|
getPaginationRowModel: getPaginationRowModel(),
|
|
|
|
|
onPaginationChange: setPagination,
|
2026-04-05 17:08:42 +07:00
|
|
|
pageCount: Math.ceil(totalItems / pagination.pageSize),
|
|
|
|
|
manualPagination: true,
|
2026-04-12 23:01:47 +07:00
|
|
|
});
|
2025-04-05 06:07:26 +07:00
|
|
|
|
|
|
|
|
return (
|
2026-04-12 23:01:47 +07:00
|
|
|
<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
|
2026-04-12 23:01:47 +07:00
|
|
|
placeholder="Cari nama item…"
|
|
|
|
|
className="pl-9"
|
2026-04-05 17:08:42 +07:00
|
|
|
value={search}
|
|
|
|
|
onChange={(e) => setSearch(e.target.value)}
|
2025-04-05 06:07:26 +07:00
|
|
|
/>
|
|
|
|
|
</div>
|
2026-04-12 23:01:47 +07:00
|
|
|
<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>
|
2026-04-12 23:01:47 +07:00
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
2026-04-05 17:08:42 +07:00
|
|
|
{isLoading ? (
|
2026-04-12 23:01:47 +07:00
|
|
|
<div className="py-10 text-center text-sm text-muted-foreground">
|
|
|
|
|
Memuat data…
|
|
|
|
|
</div>
|
2026-04-05 17:08:42 +07:00
|
|
|
) : (
|
2026-04-12 23:01:47 +07:00
|
|
|
<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 }))
|
|
|
|
|
}
|
|
|
|
|
/>
|
2026-04-05 17:08:42 +07:00
|
|
|
)}
|
2026-04-12 23:01:47 +07:00
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
|
|
<DeleteConfirmDialog
|
|
|
|
|
open={!!deleteId}
|
|
|
|
|
onOpenChange={(o) => !o && setDeleteId(null)}
|
|
|
|
|
onConfirm={() => deleteId && handleDelete(deleteId)}
|
|
|
|
|
title="Hapus item gacha ini?"
|
|
|
|
|
/>
|
|
|
|
|
</BackofficeWrapper>
|
|
|
|
|
);
|
2026-04-11 17:04:14 +07:00
|
|
|
}
|