183 lines
7.1 KiB
TypeScript
183 lines
7.1 KiB
TypeScript
import { useState, useMemo } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Eye, Pencil, Trash2 } from "lucide-react";
|
|
import type { ColumnDef, VisibilityState } from "@tanstack/react-table";
|
|
import { DataTable } from "@/components/dataTable";
|
|
import { DataTableToolbar } from "@/components/dataTableToolbar";
|
|
import { TableSearchInput } from "@/components/tableSearch";
|
|
import { CreateButton } from "@/components/createButton";
|
|
import { Button } from "@/components/ui/button";
|
|
import { DeleteDialog } from "@/components/deleteDialog";
|
|
import { useRoles, useDeleteRole } from "@/features/roles/hooks/useRoles";
|
|
import type { Role } from "@/features/roles/api/rolesApi";
|
|
|
|
// ─── RolesPage ────────────────────────────────────────────────────────────────
|
|
|
|
export default function RolesPage() {
|
|
const { t } = useTranslation();
|
|
const navigate = useNavigate();
|
|
|
|
const columnMeta = useMemo(
|
|
() => [
|
|
{ id: "id", label: t("common.id", "ID") },
|
|
{ id: "code", label: t("roles.fields.code", "Kod") },
|
|
{ id: "name", label: t("roles.fields.name", "Ady") },
|
|
{ id: "guard_name", label: t("roles.fields.guardName", "Guard name") },
|
|
],
|
|
[t],
|
|
);
|
|
|
|
// ── State ──────────────────────────────────────────────────────────────────
|
|
const [search, setSearch] = useState("");
|
|
const [page, setPage] = useState(1);
|
|
const [perPage, setPerPage] = useState(25);
|
|
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({});
|
|
const [columnOrder, setColumnOrder] = useState<string[]>([]);
|
|
const [deleteId, setDeleteId] = useState<number | null>(null);
|
|
|
|
// ── Data ───────────────────────────────────────────────────────────────────
|
|
const { data, isLoading } = useRoles({ search, page, per_page: perPage });
|
|
const deleteRole = useDeleteRole();
|
|
|
|
// ── Columns ────────────────────────────────────────────────────────────────
|
|
const columns: ColumnDef<Role>[] = [
|
|
{
|
|
accessorKey: "id",
|
|
header: t("common.id", "ID"),
|
|
size: 70,
|
|
cell: ({ row }) => <span className="text-primary font-semibold">{row.original.id}</span>,
|
|
},
|
|
{
|
|
accessorKey: "code",
|
|
header: t("roles.fields.code", "Kod"),
|
|
},
|
|
{
|
|
accessorKey: "name",
|
|
header: t("roles.fields.name", "Ady"),
|
|
cell: ({ row }) => <span>{row.original.name?.tk ?? "—"}</span>,
|
|
},
|
|
{
|
|
accessorKey: "guard_name",
|
|
header: t("roles.fields.guardName", "Guard name"),
|
|
},
|
|
{
|
|
id: "actions",
|
|
header: "",
|
|
size: 120,
|
|
enableSorting: false,
|
|
enableHiding: false,
|
|
cell: ({ row }) => (
|
|
<div className="flex items-center justify-end gap-1">
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
className="h-8 w-8 text-muted-foreground hover:text-primary"
|
|
onClick={() => navigate(`/settings/users/roles/${row.original.id}`)}
|
|
>
|
|
<Eye size={15} />
|
|
</Button>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
className="h-8 w-8 text-muted-foreground hover:text-blue-500"
|
|
onClick={() => navigate(`/settings/users/roles/${row.original.id}/edit`)}
|
|
>
|
|
<Pencil size={15} />
|
|
</Button>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
className="h-8 w-8 text-muted-foreground hover:text-destructive"
|
|
onClick={() => setDeleteId(row.original.id)}
|
|
>
|
|
<Trash2 size={15} />
|
|
</Button>
|
|
</div>
|
|
),
|
|
},
|
|
];
|
|
|
|
// ── Handlers ───────────────────────────────────────────────────────────────
|
|
const handleConfirmDelete = () => {
|
|
if (deleteId === null) return;
|
|
deleteRole.mutate(deleteId, {
|
|
onSuccess: () => setDeleteId(null),
|
|
onError: () => setDeleteId(null),
|
|
});
|
|
};
|
|
|
|
// ── Render ─────────────────────────────────────────────────────────────────
|
|
return (
|
|
<div className="space-y-4">
|
|
{/* Page heading */}
|
|
<div className="flex items-center justify-between mb-6">
|
|
<h1 className="text-2xl font-bold text-foreground">{t("roles.title", "Rollar")}</h1>
|
|
</div>
|
|
<div className="flex flex-wrap items-center gap-3 justify-between">
|
|
<TableSearchInput
|
|
value={search}
|
|
onChange={(v) => {
|
|
setSearch(v);
|
|
setPage(1);
|
|
}}
|
|
placeholder={t("roles.searchPlaceholder", "Gözlemek...")}
|
|
/>
|
|
<CreateButton label={t("roles.actions.create", "Rol dörediň")} onClick={() => navigate("/settings/users/roles/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("roles.searchPlaceholder", "Gözlemek...")}
|
|
columns={columnMeta}
|
|
columnVisibility={columnVisibility}
|
|
onColumnVisibilityChange={setColumnVisibility}
|
|
columnOrder={columnOrder}
|
|
onColumnOrderChange={setColumnOrder}
|
|
perPageOptions={[10, 25, 50, 100]}
|
|
perPage={perPage}
|
|
onPerPageChange={(v) => {
|
|
setPerPage(v);
|
|
setPage(1);
|
|
}}
|
|
hideSearch
|
|
hideAction
|
|
/>
|
|
|
|
{/* Table */}
|
|
<DataTable
|
|
columns={columns}
|
|
data={data?.data ?? []}
|
|
isLoading={isLoading}
|
|
columnVisibility={columnVisibility}
|
|
onColumnVisibilityChange={setColumnVisibility}
|
|
columnOrder={columnOrder}
|
|
onColumnOrderChange={setColumnOrder}
|
|
enableRowSelection
|
|
getRowId={(row) => String(row.id)}
|
|
currentPage={data?.meta?.current_page ?? 1}
|
|
totalPages={data?.meta?.last_page ?? 1}
|
|
totalCount={data?.meta?.total ?? 0}
|
|
onPageChange={setPage}
|
|
/>
|
|
</div>
|
|
{/* Delete confirmation dialog */}
|
|
<DeleteDialog
|
|
open={deleteId !== null}
|
|
onOpenChange={(o) => {
|
|
if (!o) setDeleteId(null);
|
|
}}
|
|
title={t("roles.deleteDialog.title", "Roly pozmak")}
|
|
description={t("roles.deleteDialog.description", "Bu roly pozmak isleýärsiňizmi? Bu amal yzyna gaýtarylmaz.")}
|
|
onConfirm={handleConfirmDelete}
|
|
isLoading={deleteRole.isPending}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|