style: do inputs design modification
This commit is contained in:
@@ -2,12 +2,8 @@
|
||||
|
||||
import { useMemo } from 'react';
|
||||
import type { ColumnDef } from '@tanstack/react-table';
|
||||
import { Edit, RotateCcw } from 'lucide-react';
|
||||
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { PERMISSIONS } from '@/constants/permissions';
|
||||
import { usePermissions } from '@/hooks/usePermissions';
|
||||
import type { Client } from '@/types';
|
||||
|
||||
function formatDate(value?: string | null) {
|
||||
@@ -19,23 +15,9 @@ function formatDate(value?: string | null) {
|
||||
}).format(new Date(value));
|
||||
}
|
||||
|
||||
interface UseClientColumnsParams {
|
||||
onEdit: (client: Client) => void;
|
||||
onToggleStatus: (client: Client) => void;
|
||||
pendingClientId?: number;
|
||||
}
|
||||
|
||||
export function useClientColumns({
|
||||
onEdit,
|
||||
onToggleStatus,
|
||||
pendingClientId,
|
||||
}: UseClientColumnsParams): ColumnDef<Client>[] {
|
||||
const { hasPermission } = usePermissions();
|
||||
const canEdit = hasPermission(PERMISSIONS.CLIENT.UPDATE);
|
||||
const canDelete = hasPermission(PERMISSIONS.CLIENT.DELETE);
|
||||
|
||||
export function useClientColumns(): ColumnDef<Client>[] {
|
||||
return useMemo(() => {
|
||||
const columns: ColumnDef<Client>[] = [
|
||||
return [
|
||||
{
|
||||
accessorKey: 'name',
|
||||
header: 'Client',
|
||||
@@ -75,38 +57,5 @@ export function useClientColumns({
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
if (!canEdit && !canDelete) return columns;
|
||||
|
||||
columns.push({
|
||||
id: 'actions',
|
||||
header: () => <div className="text-right">Actions</div>,
|
||||
enableSorting: false,
|
||||
cell: ({ row }) => {
|
||||
const client = row.original;
|
||||
return (
|
||||
<div className="flex justify-end gap-2">
|
||||
{canEdit ? (
|
||||
<Button variant="outline" size="sm" onClick={() => onEdit(client)}>
|
||||
<Edit className="size-4" />
|
||||
</Button>
|
||||
) : null}
|
||||
{canDelete ? (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={pendingClientId === client.id}
|
||||
onClick={() => onToggleStatus(client)}
|
||||
>
|
||||
<RotateCcw className="mr-2 size-4" />
|
||||
{client.is_active ? 'Deactivate' : 'Activate'}
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
return columns;
|
||||
}, [canDelete, canEdit, onEdit, onToggleStatus, pendingClientId]);
|
||||
}, []);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
import type { ReactNode } from 'react';
|
||||
import type { ColumnDef, SortingState } from '@tanstack/react-table';
|
||||
import { Edit, RotateCcw } from 'lucide-react';
|
||||
|
||||
import { DataTable } from '@/components/data-table';
|
||||
import { PERMISSIONS } from '@/constants/permissions';
|
||||
import type { Client } from '@/types';
|
||||
|
||||
interface ClientTableProps {
|
||||
@@ -18,6 +20,9 @@ interface ClientTableProps {
|
||||
onPageChange: (skip: number) => void;
|
||||
onLimitChange: (limit: number) => void;
|
||||
onSortingChange: (sorting: SortingState) => void;
|
||||
onEdit: (client: Client) => void;
|
||||
onToggleStatus: (client: Client) => void;
|
||||
pendingClientId?: number;
|
||||
}
|
||||
|
||||
export function ClientTable({
|
||||
@@ -32,6 +37,9 @@ export function ClientTable({
|
||||
onPageChange,
|
||||
onLimitChange,
|
||||
onSortingChange,
|
||||
onEdit,
|
||||
onToggleStatus,
|
||||
pendingClientId,
|
||||
}: ClientTableProps) {
|
||||
return (
|
||||
<DataTable
|
||||
@@ -41,6 +49,21 @@ export function ClientTable({
|
||||
isLoading={isLoading}
|
||||
toolbar={toolbar}
|
||||
emptyTitle="No clients found."
|
||||
actions={[
|
||||
{
|
||||
label: 'Edit',
|
||||
icon: <Edit className="size-4" />,
|
||||
permission: PERMISSIONS.CLIENT.UPDATE,
|
||||
onClick: onEdit,
|
||||
},
|
||||
{
|
||||
label: (client) => (client.is_active ? 'Deactivate' : 'Activate'),
|
||||
icon: <RotateCcw className="size-4" />,
|
||||
permission: PERMISSIONS.CLIENT.DELETE,
|
||||
disabled: (client) => pendingClientId === client.id,
|
||||
onClick: onToggleStatus,
|
||||
},
|
||||
]}
|
||||
pagination={{
|
||||
skip,
|
||||
limit,
|
||||
|
||||
@@ -69,11 +69,7 @@ export default function ClientsPage() {
|
||||
[updateClientStatus],
|
||||
);
|
||||
|
||||
const columns = useClientColumns({
|
||||
onEdit: openEdit,
|
||||
onToggleStatus: toggleStatus,
|
||||
pendingClientId,
|
||||
});
|
||||
const columns = useClientColumns();
|
||||
|
||||
const total = clientsQuery.data?.total ?? 0;
|
||||
const clients = clientsQuery.data?.items ?? [];
|
||||
@@ -118,6 +114,9 @@ export default function ClientsPage() {
|
||||
onPageChange={setSkip}
|
||||
onLimitChange={setLimit}
|
||||
onSortingChange={setSorting}
|
||||
onEdit={openEdit}
|
||||
onToggleStatus={toggleStatus}
|
||||
pendingClientId={pendingClientId}
|
||||
/>
|
||||
</main>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user