import { ReactNode, Dispatch, SetStateAction } from 'react'; import classNames from 'classnames'; import { Banner, Loading, SearchField } from 'react-basics'; import { useMessages } from 'components/hooks'; import Empty from 'components/common/Empty'; import Pager from 'components/common/Pager'; import styles from './DataTable.module.css'; const DEFAULT_SEARCH_DELAY = 600; export interface DataTableProps { result: { page: number; pageSize: number; count: number; data: any[]; }; params: { query: string; page: number; }; setParams: Dispatch>; isLoading: boolean; error: unknown; searchDelay?: number; showSearch?: boolean; showPaging?: boolean; children: ReactNode | ((data: any) => ReactNode); } export function DataTable({ result, params, setParams, isLoading, error, searchDelay, showSearch = true, showPaging = true, children, }: DataTableProps) { const { formatMessage, labels, messages } = useMessages(); const { pageSize, count } = result || {}; const { query, page } = params || {}; const hasData = Boolean(!isLoading && result?.data?.length); const noResults = Boolean(!isLoading && query && !hasData); const handleSearch = query => { setParams({ ...params, query }); }; const handlePageChange = page => { setParams({ ...params, page }); }; if (error) { return {formatMessage(messages.error)}; } return ( <> {(hasData || query || isLoading) && showSearch && ( )}
{hasData ? (typeof children === 'function' ? children(result) : children) : null} {isLoading && } {!isLoading && !hasData && !query && ( )} {noResults && }
{showPaging && ( )} ); } export default DataTable;