refactord some files
This commit is contained in:
1 parent
3d1dc86cab
commit
caae941346
30 files changed
+2761
-3039
No files matched your search
@@ -1,75 +1,69 @@
|
||||
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 { Button } from '@/components/ui/button'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import { useRoles, useDeleteRole } from '@/features/roles/hooks/useRoles'
|
||||
import type { Role } from '@/features/roles/api/rolesApi'
|
||||
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 { 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])
|
||||
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)
|
||||
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()
|
||||
const { data, isLoading } = useRoles({ search, page, per_page: perPage });
|
||||
const deleteRole = useDeleteRole();
|
||||
|
||||
// ── Columns ────────────────────────────────────────────────────────────────
|
||||
const columns: ColumnDef<Role>[] = [
|
||||
{
|
||||
accessorKey: 'id',
|
||||
header: t('common.id', 'ID'),
|
||||
accessorKey: "id",
|
||||
header: t("common.id", "ID"),
|
||||
size: 70,
|
||||
cell: ({ row }) => (
|
||||
<span className="text-primary font-semibold">{row.original.id}</span>
|
||||
),
|
||||
cell: ({ row }) => <span className="text-primary font-semibold">{row.original.id}</span>,
|
||||
},
|
||||
{
|
||||
accessorKey: 'code',
|
||||
header: t('roles.fields.code', 'Kod'),
|
||||
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: "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'),
|
||||
accessorKey: "guard_name",
|
||||
header: t("roles.fields.guardName", "Guard name"),
|
||||
},
|
||||
{
|
||||
id: 'actions',
|
||||
header: '',
|
||||
id: "actions",
|
||||
header: "",
|
||||
size: 120,
|
||||
enableSorting: false,
|
||||
enableHiding: false,
|
||||
@@ -102,93 +96,87 @@ export default function RolesPage() {
|
||||
</div>
|
||||
),
|
||||
},
|
||||
]
|
||||
];
|
||||
|
||||
// ── Handlers ───────────────────────────────────────────────────────────────
|
||||
const handleConfirmDelete = () => {
|
||||
if (deleteId === null) return
|
||||
if (deleteId === null) return;
|
||||
deleteRole.mutate(deleteId, {
|
||||
onSuccess: () => setDeleteId(null),
|
||||
onError: () => setDeleteId(null),
|
||||
})
|
||||
}
|
||||
onError: () => setDeleteId(null),
|
||||
});
|
||||
};
|
||||
|
||||
// ── Render ─────────────────────────────────────────────────────────────────
|
||||
return (
|
||||
<div>
|
||||
<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>
|
||||
|
||||
<h1 className="text-2xl font-bold text-foreground">{t("roles.title", "Rollar")}</h1>
|
||||
</div>
|
||||
<div className="bg-card border border-border rounded-xl p-4">
|
||||
<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
|
||||
/>
|
||||
|
||||
{/* 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) }}
|
||||
actionLabel={t('roles.actions.create', 'Rol dörediň')}
|
||||
onAction={() => navigate('/settings/users/roles/create')}
|
||||
/>
|
||||
|
||||
{/* 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>
|
||||
{/* 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 */}
|
||||
<AlertDialog
|
||||
<DeleteDialog
|
||||
open={deleteId !== null}
|
||||
onOpenChange={(open) => { if (!open) setDeleteId(null) }}
|
||||
>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>
|
||||
{t('roles.deleteDialog.title', 'Roly pozmak')}
|
||||
</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
{t(
|
||||
'roles.deleteDialog.description',
|
||||
'Bu roly pozmak isleýärsiňizmi? Bu amal yzyna gaýtarylmaz.',
|
||||
)}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>{t('common.cancel', 'Ýatyr')}</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={handleConfirmDelete}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{deleteRole.isPending
|
||||
? t('common.deleting', 'Pozulýar...')
|
||||
: t('common.delete', 'Poz')}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
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>
|
||||
)
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user