All files / src/pages/suppliers/components SuppliersTable.tsx

96.41% Statements 188/195
75% Branches 3/4
25% Functions 2/8
96.41% Lines 188/195

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 1961x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x               6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x  
/**
 * @file SuppliersTable.tsx
 * @module pages/suppliers/components/SuppliersTable
 *
 * @summary
 * Data grid component for suppliers board.
 * Displays suppliers list with pagination, sorting, and row selection.
 *
 * @enterprise
 * - MUI DataGrid with server-side pagination and sorting
 * - Row click selection handler
 * - Loading indicator
 * - Empty state handling
 * - i18n support for column headers
 * - Responsive and customizable
 */
 
import * as React from 'react';
import {
  Paper,
  Box,
  Typography,
  LinearProgress,
} from '@mui/material';
import {
  DataGrid,
  type GridPaginationModel,
  type GridSortModel,
  type GridColDef,
} from '@mui/x-data-grid';
import { useTranslation } from 'react-i18next';
import { useSettings } from '../../../hooks/useSettings';
import { formatDate } from '../../../utils/formatters';
import type { SupplierRow } from '../../../api/suppliers';
 
/**
 * Suppliers Table component props.
 *
 * @interface SuppliersTableProps
 */
export interface SuppliersTableProps {
  /** Suppliers data to display */
  rows: SupplierRow[];
  /** Total row count (for server-side pagination) */
  rowCount: number;
  /** Current pagination model */
  paginationModel: GridPaginationModel;
  /** Handler for pagination changes */
  onPaginationChange: (model: GridPaginationModel) => void;
  /** Current sort model */
  sortModel: GridSortModel;
  /** Handler for sort changes */
  onSortChange: (model: GridSortModel) => void;
  /** Whether data is loading */
  isLoading: boolean;
  /** Handler for row click (selection) */
  onRowClick: (params: { id: string | number }) => void;
}
 
/**
 * Suppliers table component.
 *
 * Features:
 * - Server-side pagination and sorting
 * - Row click selection
 * - Loading indicator
 * - Empty state message
 * - Formatted date columns
 * - Responsive table density
 *
 * @component
 * @example
 * ```tsx
 * <SuppliersTable
 *   rows={suppliers}
 *   rowCount={total}
 *   paginationModel={pagination}
 *   onPaginationChange={handlePagination}
 *   sortModel={sort}
 *   onSortChange={handleSort}
 *   isLoading={loading}
 *   onRowClick={handleRowClick}
 * />
 * ```
 */
export const SuppliersTable: React.FC<SuppliersTableProps> = ({
  rows,
  rowCount,
  paginationModel,
  onPaginationChange,
  sortModel,
  onSortChange,
  isLoading,
  onRowClick,
}) => {
  const { t } = useTranslation(['common', 'suppliers']);
  const { userPreferences } = useSettings();
 
  // Define grid columns (no memoization needed - same as InventoryTable)
  const columns: GridColDef<SupplierRow>[] = [
    {
      field: 'name',
      headerName: t('suppliers:table.name', 'Supplier Name'),
      flex: 1,
      minWidth: 200,
    },
    {
      field: 'contactName',
      headerName: t('suppliers:table.contactName', 'Contact'),
      width: 150,
      valueGetter: (_value: unknown, row: SupplierRow | null) => row?.contactName ?? '—',
    },
    {
      field: 'phone',
      headerName: t('suppliers:table.phone', 'Phone'),
      width: 140,
      valueGetter: (_value: unknown, row: SupplierRow | null) => row?.phone ?? '—',
    },
    {
      field: 'email',
      headerName: t('suppliers:table.email', 'Email'),
      width: 180,
      valueGetter: (_value: unknown, row: SupplierRow | null) => row?.email ?? '—',
    },
    {
      field: 'createdAt',
      headerName: t('suppliers:table.createdAt', 'Created'),
      width: 180,
      valueGetter: (_value: unknown, row: SupplierRow | null) => row?.createdAt ?? null,
      valueFormatter: (value: unknown) => {
        if (!value) return '—';
        try {
          return formatDate(new Date(String(value)), userPreferences.dateFormat);
        } catch {
          return String(value);
        }
      },
    },
  ];
 
  return (
    <Paper
      variant="outlined"
      sx={{
        flex: 1,
        position: 'relative',
        minHeight: 320,
        display: 'flex',
        flexDirection: 'column',
      }}
    >
      {isLoading && (
        <LinearProgress sx={{ position: 'absolute', left: 0, right: 0, top: 0 }} />
      )}
 
      <DataGrid
        rows={rows}
        columns={columns}
        rowCount={rowCount}
        paginationMode="server"
        sortingMode="server"
        paginationModel={paginationModel}
        onPaginationModelChange={onPaginationChange}
        pageSizeOptions={[6]}
        sortModel={sortModel}
        onSortModelChange={onSortChange}
        getRowId={(r) => r.id}
        density={
          userPreferences.tableDensity === 'compact' ? 'compact' : 'comfortable'
        }
        rowHeight={44}
        columnHeaderHeight={48}
        onRowClick={onRowClick}
        slots={{
          noRowsOverlay: () => (
            <Box
              sx={{
                p: 2,
                textAlign: 'center',
                display: 'grid',
                placeItems: 'center',
                height: '100%',
              }}
            >
              <Typography variant="body2" color="text.secondary">
                {t('suppliers:empty.default', 'No suppliers found')}
              </Typography>
            </Box>
          ),
        }}
        sx={{ '& .MuiDataGrid-cell': { overflow: 'visible' } }}
      />
    </Paper>
  );
};