All files / SASQLookupComponent/TableBody TableBody.js

90.9% Statements 30/33
66.66% Branches 12/18
91.66% Functions 11/12
90% Lines 27/30

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                    25x                                       3245x       72x 72x 72x   72x         72x 216x       216x   72x 1949x 1949x               72x 72x 72x 216x 1944x         72x     72x 72x 72x 5x             72x 1296x     72x   72x                                                                               25x                                                        
import { useCallback, useMemo } from 'react';
import PropTypes from 'prop-types';
 
import { useHistory, useLocation } from 'react-router-dom';
 
import {
  MultiColumnList,
} from '@folio/stripes/components';
import NoResultsMessage from '../../NoResultsMessage';
 
const TableBody = ({
  data,
  error,
  fetchNextPage,
  filterPaneVisible,
  intlKey: passedIntlKey,
  intlNS: passedIntlNS,
  isError,
  isLoading,
  labelOverrides = {},
  match,
  mclProps: {
    formatter = {},
    ...mclProps
  } = {},
  noResultsProps = {},
  onSort,
  path,
  resultColumns,
  rowNavigation = true, // Default navigation onRowClick
  getNavigationIdentifier = (row) => row?.id,
  toggleFilterPane,
  query,
}) => {
  const sortOrder = query.sort ?? '';
  const history = useHistory();
  const location = useLocation();
 
  const onNeedMoreData = (_askAmount, index) => {
    fetchNextPage({ pageParam: index });
  };
 
  // Build the map of column definitions
  const columnMapping = Object.fromEntries(
    resultColumns.map(e => [e.propertyPath, e.label])
  );
 
  // Build the list of visible columns
  const visibleColumns = resultColumns.map(e => e.propertyPath);
 
  const getRowUrl = useCallback((rowData) => {
    const baseUrl = `${path}/${getNavigationIdentifier(rowData)}`;
    return {
      url: `${baseUrl}${location?.search ?? ''}`,
      path,
      baseUrl,
      location
    };
  }, [getNavigationIdentifier, location, path]);
 
  const getEnhancedFormatter = useCallback(() => {
    const enhancedFormatter = {};
    for (const [key, value] of Object.entries(formatter)) {
      if (typeof value === 'function') {
        enhancedFormatter[key] = (item) => value({ ...item, defaultRowUrl: getRowUrl(item) });
      } else E{
        enhancedFormatter[key] = value;
      }
    }
    return enhancedFormatter;
  }, [formatter, getRowUrl]);
 
  const getOnRowClick = useCallback(() => {
    Eif (rowNavigation) {
      return (_e, rowData) => {
        history.push(getRowUrl(rowData).url);
      };
    }
 
    return null;
  }, [getRowUrl, history, rowNavigation]);
 
  const isSelected = useCallback(
    ({ item }) => getNavigationIdentifier(item) === match?.params?.id,
    [getNavigationIdentifier, match?.params?.id]
  );
  const NoResultsComponent = useMemo(() => noResultsProps?.component ?? NoResultsMessage, [noResultsProps?.component]);
 
  return (
    <MultiColumnList
      autosize
      columnMapping={columnMapping}
      contentData={data?.results}
      formatter={getEnhancedFormatter()} // Pass enhanced formatter
      hasMargin
      interactive={rowNavigation}
      isEmptyMessage={
        <NoResultsComponent
          {...{
            error,
            filters: query.filters, // Separate out filters as per "searchTerm" below
            filterPaneIsVisible: filterPaneVisible,
            intlKey: passedIntlKey,
            intlNS: passedIntlNS,
            isError,
            isLoading,
            labelOverrides,
            query, // Passing the whole query object just in case
            searchTerm: query.query, // Here for compatibility, probably better to just use "query" prop
            toggleFilterPane,
            ...noResultsProps, // Anything passed in directly takes precedence
          }}
        />
      }
      isSelected={isSelected}
      onHeaderClick={onSort}
      onNeedMoreData={onNeedMoreData}
      onRowClick={getOnRowClick()}
      pagingType="click"
      sortDirection={sortOrder.startsWith('-') ? 'descending' : 'ascending'}
      sortOrder={sortOrder.replace(/^-/, '').replace(/,.*/, '')}
      totalCount={data.totalRecords}
      visibleColumns={visibleColumns}
      {...mclProps}
    />
  );
};
 
TableBody.propTypes = {
  data: PropTypes.shape({
    totalRecords: PropTypes.number,
    results: PropTypes.arrayOf(PropTypes.object)
  }),
  error: PropTypes.object,
  fetchNextPage: PropTypes.func,
  filterPaneVisible: PropTypes.bool,
  history: PropTypes.object,
  intlKey: PropTypes.string,
  intlNS: PropTypes.string,
  isError: PropTypes.bool,
  isLoading: PropTypes.bool,
  labelOverrides: PropTypes.object,
  location: PropTypes.object,
  match: PropTypes.object,
  mclProps: PropTypes.object,
  noResultsProps: PropTypes.object,
  onSort: PropTypes.func,
  path: PropTypes.string.isRequired,
  query: PropTypes.object,
  resultColumns: PropTypes.arrayOf(PropTypes.object),
  rowNavigation: PropTypes.bool,
  getNavigationIdentifier: PropTypes.func,
  toggleFilterPane: PropTypes.func
};
 
export default TableBody;