Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#537 Improving table loading state PR #548

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Skeletons/SkeletonTable.tsx
niyobertin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import React from 'react';

interface SkeletonTableProps {
columns: Array<any>;
}

function SkeletonTable({ columns }: SkeletonTableProps) {
return (
<table className="min-w-full leading-normal">
<thead>
<tr>
{columns.map((column: any, index: React.Key | null | undefined) => (
<th key={column.id || column.name} className="px-6 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
<div className="h-4 bg-gray-300 rounded w-3/4" aria-label="Loading skeleton" />
</th>
))}
</tr>
</thead>
<tbody>
{[...Array(5)].map((_, rowIndex:any) => (
<tr key={rowIndex.id || rowIndex.name}>
{columns.map((_, colIndex:any) => (
<td key={colIndex.id || colIndex.name} className="px-6 py-4 border-b border-gray-200 bg-white text-sm">
<div className="h-4 bg-gray-300 rounded w-full" aria-label="Loading skeleton" />
</td>
))}
</tr>
))}
</tbody>
</table>
);
}

export default SkeletonTable;
183 changes: 84 additions & 99 deletions src/components/InvitationTable.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// @ts-nocheck
import React, { useState,useEffect, useMemo } from 'react';
import React, { useState, useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import {
useGlobalFilter,
usePagination,
useSortBy,
useTable,
} from 'react-table';
import { useGlobalFilter, usePagination, useSortBy, useTable } from 'react-table';
import { toast } from 'react-toastify';
import DataPagination from './DataPagination';
import SkeletonTable from '../Skeletons/SkeletonTable';

interface TableData {
data: any[];
Expand All @@ -31,7 +28,7 @@ function DataTableStats({ data, columns, error, loading }: TableData) {
{
data: memoizedData,
columns: memoizedColumns,
initialState: {pageIndex, pageSize: 3, globalFilter: filterInput },
initialState: { pageIndex, pageSize: 3, globalFilter: filterInput },
},
useGlobalFilter,
useSortBy,
Expand All @@ -55,10 +52,17 @@ function DataTableStats({ data, columns, error, loading }: TableData) {
prepareRow,
state: { pageIndex: currentPageIndex, pageSize },
} = tableInstance;

useEffect(() => {
setPageIndex(currentPageIndex);
}, [currentPageIndex]);

useEffect(() => {
if (error) {
toast.error('Network error. Please, try again later.');
}
}, [error]);

const handleFilterChange = (e) => {
const value = e.target.value || '';
setGlobalFilter(value);
Expand All @@ -69,96 +73,77 @@ function DataTableStats({ data, columns, error, loading }: TableData) {
<div className="">
<div className="flex items-center justify-between pb-6 " />
<div style={{ overflowX: 'auto' }}>
<table className="min-w-full leading-normal" {...getTableProps()}>
<thead>
{headerGroups.map((headerGroup) => (
<tr key={headerGroup.id} {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => (
<th
key={column.id}
className={column.isSorted ? 'sort-asc thead' : ' thead'}
{...column.getHeaderProps(column.getSortByToggleProps())}
>
{column.render('Header')}
</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{!loading && memoizedData.length === 0 ? (
<tr>
<td
colSpan={columns.length}
className="px-6 py-4 text-sm text-center text-gray-500 dark:text-gray-300"
aria-label="Empty cell" // Added for accessibility
>
&nbsp;{' '}
{/* Non-breaking space to ensure it's not an empty tag */}
</td>
</tr>
) : (
page.map((row) => {
prepareRow(row);
return (
<tr
key={row.id}
className={`border-b dark:border-gray-700 ${
row.index % 2 === 0
? 'bg-light-bg dark:bg-neutral-600'
: 'bg-white dark:bg-dark-bg'
}`}
{...row.getRowProps()}
{loading ? (
<SkeletonTable columns={columns} />
) : (
<table className="min-w-full leading-normal" {...getTableProps()}>
<thead>
{headerGroups.map((headerGroup) => (
<tr key={headerGroup.id} {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => (
<th
key={column.id}
className={column.isSorted ? 'sort-asc thead' : ' thead'}
{...column.getHeaderProps(column.getSortByToggleProps())}
>
{column.render('Header')}
</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{memoizedData.length === 0 && (
<tr>
<td
colSpan={columns.length}
className="px-6 py-4 text-sm text-center text-gray-500 dark:text-gray-300"
aria-label="Empty cell" // Added for accessibility
>
{row.cells.map((cell) => (
<td
key={cell.id}
className="data-cell "
{...cell.getCellProps()}
>
{cell.render('Cell')}
</td>
))}
</tr>
);
})
)}
{loading && (
<tr>
<td
colSpan={columns.length}
className="px-6 py-4 text-sm text-center text-gray-500 dark:text-gray-300 "
>
Loading...
</td>
</tr>
)}
{error && (
<tr>
<td
colSpan={columns.length}
className="px-6 py-4 text-sm text-center text-gray-500 dark:text-gray-300 "
>
Error occurred
</td>
</tr>
)}
{!loading && !error && data.length === 0 && (
<tr>
{' '}
<td colSpan={columns.length || 100} className="text-center p-4">
<div className="flex flex-col items-center justify-center space-y-4">
{' '}
<p className="text-gray-600 dark:text-gray-400 text-lg font-medium">
{' '}
No records available{' '}
</p>
</div>{' '}
</td>{' '}
</tr>
)}
</tbody>
</table>
&nbsp;{' '}
{/* Non-breaking space to ensure it's not an empty tag */}
</td>
</tr>
)}
{memoizedData.length > 0 &&
page.map((row) => {
prepareRow(row);
return (
<tr
key={row.id}
className={`border-b dark:border-gray-700 ${
row.index % 2 === 0
? 'bg-light-bg dark:bg-neutral-600'
: 'bg-white dark:bg-dark-bg'
}`}
{...row.getRowProps()}
>
{row.cells.map((cell) => (
<td
key={cell.id}
className="data-cell "
{...cell.getCellProps()}
>
{cell.render('Cell')}
</td>
))}
</tr>
);
})}
{!loading && !error && data.length === 0 && (
<tr>
<td colSpan={columns.length || 100} className="text-center p-4">
<div className="flex flex-col items-center justify-center space-y-4">
<p className="text-gray-600 dark:text-gray-400 text-lg font-medium">
No records available
</p>
</div>
</td>
</tr>
)}
</tbody>
</table>
)}
</div>
<div className="px-6 py-4">
<DataPagination
Expand All @@ -179,4 +164,4 @@ function DataTableStats({ data, columns, error, loading }: TableData) {
);
}

export default DataTableStats;
export default DataTableStats;
1 change: 1 addition & 0 deletions src/pages/invitation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ function Invitation() {
<InvitationCardSkeleton />
<InvitationCardSkeleton />
<InvitationCardSkeleton />
<InvitationCardSkeleton />
</>
) : (
<>
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"src/tests/About.test.tsx",
"src/components/TraineeDashboardChart.tsx",
"src/pages/TraineeRatingDashboard.tsx"
],
, "src/constants/SkeletonTable.tsx" ],
"exclude": ["node_modules"]
}
Loading