218 lines
7.6 KiB
TypeScript
218 lines
7.6 KiB
TypeScript
import { useState } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { useTranslation } from "react-i18next";
|
|
import { DeleteDialog } from "@/components/deleteDialog";
|
|
import { DataTable, type ColumnDef } from "@/components/dataTable";
|
|
import { DataTableToolbar, type ColumnMeta } from "@/components/dataTableToolbar";
|
|
import { TableSearchInput } from "@/components/tableSearch";
|
|
import { CreateButton } from "@/components/createButton";
|
|
import { TableActions } from "@/components/tableActions";
|
|
import type { VisibilityState } from "@tanstack/react-table";
|
|
import { useCardTransactions, useDeleteCardTransaction } from "@/features/cardTransactions/hooks/useCardTransactions";
|
|
import type { Card } from "@/features/cardTransactions/api/cardTransactionsApi";
|
|
|
|
// ─── Page ─────────────────────────────────────────────────────────────────────
|
|
|
|
const PER_PAGE = 25;
|
|
|
|
export default function CardTransactionsPage() {
|
|
const { t } = useTranslation();
|
|
const navigate = useNavigate();
|
|
|
|
const [page, setPage] = useState(1);
|
|
const [perPage, setPerPage] = useState(PER_PAGE);
|
|
const [search, setSearch] = useState("");
|
|
const [deleteId, setDeleteId] = useState<number | null>(null);
|
|
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({});
|
|
const [columnOrder, setColumnOrder] = useState<string[]>([]);
|
|
|
|
const { data, isLoading } = useCardTransactions({ page, limit: perPage, search });
|
|
const deleteMutation = useDeleteCardTransaction();
|
|
|
|
// ── Columns ──────────────────────────────────────────────────────────────────
|
|
|
|
const columns: ColumnDef<Card>[] = [
|
|
{
|
|
id: "id",
|
|
accessorKey: "id",
|
|
header: t("cardTransaction.col.id", "ID"),
|
|
cell: ({ row }) => (
|
|
<button
|
|
className="text-primary font-medium hover:underline text-left"
|
|
onClick={() => navigate(`/card-transactions/${row.original.id}`)}
|
|
>
|
|
{row.original.id}
|
|
</button>
|
|
),
|
|
size: 80,
|
|
},
|
|
{
|
|
id: "name",
|
|
accessorKey: "name",
|
|
header: t("cardTransaction.col.name", "Ady"),
|
|
cell: ({ row }) => <span className="text-sm text-foreground">{row.original.name}</span>,
|
|
},
|
|
{
|
|
id: "number",
|
|
accessorKey: "number",
|
|
header: t("cardTransaction.col.number", "Kart belgisi"),
|
|
cell: ({ row }) => <span className="font-mono text-sm">{row.original.number}</span>,
|
|
},
|
|
{
|
|
id: "type",
|
|
accessorKey: "type",
|
|
header: t("cardTransaction.col.type", "Görnüşi"),
|
|
cell: ({ row }) => <span className="text-sm text-foreground">{row.original.type}</span>,
|
|
},
|
|
{
|
|
id: "expiry",
|
|
accessorKey: "year",
|
|
header: t("cardTransaction.col.expiry", "Möhleti"),
|
|
cell: ({ row }) => (
|
|
<span className="text-sm text-muted-foreground whitespace-nowrap">
|
|
{row.original.month}/{row.original.year}
|
|
</span>
|
|
),
|
|
size: 100,
|
|
},
|
|
{
|
|
id: "user",
|
|
accessorFn: (row) => row.user?.name ?? "",
|
|
header: t("cardTransaction.col.user", "Ulanyjy"),
|
|
cell: ({ row }) => {
|
|
const user = row.original.user;
|
|
if (!user) return <span className="text-sm text-muted-foreground">—</span>;
|
|
return (
|
|
<button className="text-sm text-primary hover:underline text-left" onClick={() => navigate(`/all-users/${user.id}`)}>
|
|
{user.name}
|
|
</button>
|
|
);
|
|
},
|
|
size: 150,
|
|
},
|
|
{
|
|
id: "createdAt",
|
|
accessorKey: "createdAt",
|
|
header: t("cardTransaction.col.createdAt", "Döredilen wagty"),
|
|
cell: ({ row }) => (
|
|
<span className="text-sm text-muted-foreground whitespace-nowrap">
|
|
{row.original.createdAt ? new Date(row.original.createdAt).toLocaleString() : "—"}
|
|
</span>
|
|
),
|
|
size: 180,
|
|
},
|
|
{
|
|
id: "actions",
|
|
header: "",
|
|
enableHiding: false,
|
|
cell: ({ row }) => (
|
|
<TableActions
|
|
onView={() => navigate(`/card-transactions/${row.original.id}`)}
|
|
onEdit={() => navigate(`/card-transactions/${row.original.id}/edit`)}
|
|
onDelete={() => setDeleteId(row.original.id)}
|
|
isDeleting={deleteMutation.isPending}
|
|
/>
|
|
),
|
|
size: 100,
|
|
},
|
|
];
|
|
|
|
// ── Column meta for toolbar ───────────────────────────────────────────────────
|
|
|
|
const columnMeta: ColumnMeta[] = columns
|
|
.filter((c) => c.id !== "actions")
|
|
.map((c) => ({
|
|
id: c.id!,
|
|
label: typeof c.header === "string" ? c.header : c.id!,
|
|
}));
|
|
|
|
// ── Pagination ────────────────────────────────────────────────────────────────
|
|
|
|
const total = data?.count ?? 0;
|
|
const totalPages = Math.ceil(total / perPage);
|
|
|
|
const handleDelete = async () => {
|
|
if (deleteId === null) return;
|
|
await deleteMutation.mutateAsync(deleteId);
|
|
setDeleteId(null);
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
{/* Header */}
|
|
<div className="flex items-center justify-between">
|
|
<h1 className="text-xl font-semibold text-foreground">{t("Card transactions", "Kartlar")}</h1>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-3 justify-between">
|
|
<TableSearchInput
|
|
value={search}
|
|
onChange={(v) => {
|
|
setSearch(v);
|
|
setPage(1);
|
|
}}
|
|
placeholder={t("common.search", "Gözlemek")}
|
|
/>
|
|
<CreateButton label={t("Create card", "Kart dörediň")} onClick={() => navigate("/card-transactions/create")} />
|
|
</div>
|
|
|
|
<div className="bg-card border border-border rounded-xl p-4">
|
|
{/* Toolbar */}
|
|
<DataTableToolbar
|
|
searchValue={search}
|
|
onSearchChange={(v) => {
|
|
setSearch(v);
|
|
setPage(1);
|
|
}}
|
|
searchPlaceholder={t("common.search", "Gözlemek")}
|
|
columns={columnMeta}
|
|
columnVisibility={columnVisibility}
|
|
onColumnVisibilityChange={setColumnVisibility}
|
|
columnOrder={columnOrder}
|
|
onColumnOrderChange={setColumnOrder}
|
|
hideSearch
|
|
hideAction
|
|
perPageOptions={[10, 25, 50, 100]}
|
|
perPage={perPage}
|
|
onPerPageChange={(v) => {
|
|
setPerPage(v);
|
|
setPage(1);
|
|
}}
|
|
/>
|
|
|
|
{/* Table */}
|
|
<DataTable
|
|
columns={columns}
|
|
data={data?.cards ?? []}
|
|
isLoading={isLoading}
|
|
columnVisibility={columnVisibility}
|
|
onColumnVisibilityChange={setColumnVisibility}
|
|
columnOrder={columnOrder}
|
|
onColumnOrderChange={setColumnOrder}
|
|
enableRowSelection
|
|
getRowId={(row) => row.id.toString()}
|
|
currentPage={page}
|
|
totalPages={totalPages}
|
|
totalCount={total}
|
|
onPageChange={setPage}
|
|
/>
|
|
</div>
|
|
|
|
{/* Delete dialog */}
|
|
<DeleteDialog
|
|
open={deleteId !== null}
|
|
onOpenChange={(o) => {
|
|
if (!o) setDeleteId(null);
|
|
}}
|
|
title={t("Are you sure?", "Eminsiňizmi?")}
|
|
description={t(
|
|
"This action cannot be undone. This will permanently delete the card.",
|
|
"Bu amal yzyna gaýtarylmaz. Kart hemişelik öçüriler.",
|
|
)}
|
|
onConfirm={handleDelete}
|
|
isLoading={deleteMutation.isPending}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|