All files / components/accounts AccountsTable.tsx

71.42% Statements 10/14
100% Branches 0/0
77.77% Functions 7/9
71.42% Lines 10/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                                                          20x 40x     20x                                           40x                     40x           40x                                                     1x 1x                       20x       40x                                      
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Eye, Pencil, Trash2 } from 'lucide-react';
import type { AccountListItem } from '@/services/accounts.service';
import { DataTable, type Column } from '@/components/common/DataTable';
 
interface AccountsTableProps {
    accounts: AccountListItem[];
    totalItems: number;
    page: number;
    pageSize: number;
    onPageChange: (page: number) => void;
    onPageSizeChange: (pageSize: number) => void;
    sortBy: string;
    sortOrder: 'asc' | 'desc';
    onSort: (column: string) => void;
    onView: (account: AccountListItem) => void;
    onEdit: (account: AccountListItem) => void;
    onDelete: (account: AccountListItem) => void;
    isLoading?: boolean;
}
 
export default function AccountsTable({
    accounts,
    totalItems,
    page,
    pageSize,
    onPageChange,
    onPageSizeChange,
    sortBy,
    sortOrder,
    onSort,
    onView,
    onEdit,
    onDelete,
    isLoading
}: AccountsTableProps) {
    const formatDate = (dateString: string) => {
        return new Date(dateString).toLocaleDateString();
    };
 
    const columns: Column<AccountListItem>[] = [
        {
            header: 'Code',
            accessorKey: 'account_code',
            sortable: true,
            className: 'font-mono text-sm'
        },
        {
            header: 'Slug',
            accessorKey: 'slug',
            sortable: true,
            className: 'font-medium'
        },
        {
            header: 'Name',
            accessorKey: 'name',
            sortable: true
        },
        {
            header: 'Created At',
            accessorKey: 'created_at',
            sortable: true,
            render: (account) => <span className="text-muted-foreground">{formatDate(account.created_at)}</span>
        },
        {
            header: 'User Count',
            accessorKey: 'user_count',
            sortable: false
        },
        {
            header: 'Status',
            accessorKey: 'status',
            sortable: true,
            render: (account) => <Badge variant="default">{account.status}</Badge>
        },
        {
            header: 'Actions',
            className: 'text-right',
            render: (account) => (
                <div className="flex justify-end gap-2">
                    <Button
                        variant="ghost"
                        size="icon"
                        onClick={(e) => {
                            e.stopPropagation();
                            onView(account);
                        }}
                        title="View details"
                    >
                        <Eye className="h-4 w-4" />
                    </Button>
                    <Button
                        variant="ghost"
                        size="icon"
                        onClick={(e) => {
                            e.stopPropagation();
                            onEdit(account);
                        }}
                        title="Edit account"
                    >
                        <Pencil className="h-4 w-4" />
                    </Button>
                    <Button
                        variant="ghost"
                        size="icon"
                        onClick={(e) => {
                            e.stopPropagation();
                            onDelete(account);
                        }}
                        title="Delete account"
                        className="text-destructive hover:text-destructive"
                    >
                        <Trash2 className="h-4 w-4" />
                    </Button>
                </div>
            )
        }
    ];
 
    return (
        <DataTable
            data={accounts}
            columns={columns}
            keyExtractor={(account) => account.id}
            isLoading={isLoading}
            totalItems={totalItems}
            pagination={{
                page,
                pageSize,
                onPageChange,
                onPageSizeChange
            }}
            sorting={{
                sortBy,
                sortOrder,
                onSort
            }}
            onRowClick={onView}
            noDataMessage="No accounts found."
        />
    );
}