All files / src/stock-sources stock-sources-items-table.component.tsx

93.33% Statements 28/30
69.56% Branches 16/23
92.3% Functions 12/13
93.1% Lines 27/29

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                                                              1x 7x 7x 7x   7x         7x           7x 5x 3x                                   7x 1x     7x 6x 5x   2x     7x 1x     5x             8x                                                               12x                   12x 12x                           9x         18x                                                     1x     1x 1x                    
import React, { useMemo } from 'react';
import {
  DataTable,
  DataTableSkeleton,
  Pagination,
  Table,
  TableBody,
  TableCell,
  TableContainer,
  TableHead,
  TableHeader,
  TableRow,
  TableToolbar,
  TableToolbarAction,
  TableToolbarContent,
  TableToolbarMenu,
  TableToolbarSearch,
  Tile,
} from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { isDesktop, restBaseUrl } from '@openmrs/esm-framework';
import { useHandleMutate } from '../utils';
import { ResourceRepresentation } from '../core/api/api';
import { type CustomTableHeader } from '../core/components/table/types';
import AddStockSourceActionButton from './add-stock-source-button.component';
import EditStockSourceActionsMenu from './edit-stock-source/edit-stock-source.component';
import StockSourcesDeleteActionMenu from './stock-sources-delete/stock-sources-delete.component';
import StockSourcesFilter from './stock-sources-filter/stock-sources-filter.component';
import useStockSourcesPage from './stock-sources-items-table.resource';
import styles from './stock-sources.scss';
 
const StockSourcesItems: React.FC = () => {
  const { t } = useTranslation();
  const handleMutate = useHandleMutate();
  const [selectedSourceType, setSelectedSourceType] = React.useState('');
 
  const handleRefresh = () => {
    handleMutate(`${restBaseUrl}/stockmanagement/stocksource`);
  };
 
  // get sourcess
  const { items, totalItems, tableHeaders, currentPage, pageSizes, goTo, currentPageSize, setPageSize, isLoading } =
    useStockSourcesPage({
      v: ResourceRepresentation.Default,
      totalCount: true,
    });
 
  const tableRows = useMemo(() => {
    return items?.map((entry, index) => {
      return {
        ...entry,
        id: entry?.uuid,
        key: `key-${entry?.uuid}`,
        uuid: entry?.uuid,
        name: entry?.name,
        acronym: entry?.acronym,
        sourceType: entry?.sourceType?.display,
        actions: (
          <>
            <EditStockSourceActionsMenu data={items[index]} />
            <StockSourcesDeleteActionMenu uuid={items[index].uuid} />
          </>
        ),
      };
    });
  }, [items]);
 
  const handleFilterChange = (selectedSourceType: string) => {
    setSelectedSourceType(selectedSourceType);
  };
 
  const filteredTableRows = useMemo(() => {
    if (!selectedSourceType) {
      return tableRows;
    }
    return tableRows.filter((row) => row.sourceType === selectedSourceType);
  }, [tableRows, selectedSourceType]);
 
  if (isLoading) {
    return <DataTableSkeleton role="progressbar" />;
  }
 
  return (
    <div className={styles.tableOverride}>
      <h2 className={styles.tableHeader}>
        {t('stockSourcesTableHeader', 'List of partners who provide stock to the facility.')}
      </h2>
      <DataTable rows={filteredTableRows} headers={tableHeaders} isSortable useZebraStyles>
        {({ rows, headers, getHeaderProps, getTableProps, getRowProps, onInputChange }) => (
          <TableContainer>
            <TableToolbar
              style={{
                position: 'static',
                overflow: 'visible',
                backgroundColor: 'color',
              }}
            >
              <TableToolbarContent className={styles.toolbarContent}>
                <TableToolbarSearch persistent onChange={onInputChange} />
                <div
                  style={{
                    display: 'flex',
                    alignItems: 'center',
                  }}
                >
                  <StockSourcesFilter onFilterChange={handleFilterChange} />
                </div>
                <TableToolbarMenu>
                  <TableToolbarAction className={styles.toolbarMenuAction} onClick={handleRefresh}>
                    {t('refresh', 'Refresh')}
                  </TableToolbarAction>
                </TableToolbarMenu>
 
                <AddStockSourceActionButton />
              </TableToolbarContent>
            </TableToolbar>
            <Table {...getTableProps()}>
              <TableHead>
                <TableRow>
                  {headers.map(
                    (header) =>
                      header.key !== 'details' && (
                        <TableHeader
                          {...getHeaderProps({
                            header,
                            isSortable: (header as CustomTableHeader).isSortable,
                          })}
                          className={isDesktop ? styles.desktopHeader : styles.tabletHeader}
                          key={`${header.key}`}
                        >
                          {(() => {
                            const customHeader = header as CustomTableHeader;
                            return typeof customHeader.header === 'object' &&
                              customHeader.header !== null &&
                              'content' in customHeader.header
                              ? (customHeader.header.content as React.ReactNode)
                              : (customHeader.header as React.ReactNode);
                          })()}
                        </TableHeader>
                      ),
                  )}
                  <TableHeader></TableHeader>
                </TableRow>
              </TableHead>
              <TableBody>
                {rows.map((row) => {
                  return (
                    <React.Fragment key={row.id}>
                      <TableRow className={isDesktop ? styles.desktopRow : styles.tabletRow} {...getRowProps({ row })}>
                        {row.cells.map(
                          (cell) =>
                            cell?.info?.header !== 'details' && <TableCell key={cell.id}>{cell.value}</TableCell>,
                        )}
                      </TableRow>
                    </React.Fragment>
                  );
                })}
              </TableBody>
            </Table>
            {rows.length === 0 ? (
              <div className={styles.tileContainer}>
                <Tile className={styles.tile}>
                  <div className={styles.tileContent}>
                    <p className={styles.content}>{t('noSourcesToDisplay', 'No stock sources to display')}</p>
                    <p className={styles.helper}>{t('checkFilters', 'Check the filters above')}</p>
                  </div>
                </Tile>
              </div>
            ) : null}
          </TableContainer>
        )}
      </DataTable>
      <Pagination
        page={currentPage}
        pageSize={currentPageSize}
        pageSizes={pageSizes}
        totalItems={totalItems}
        onChange={({ pageSize, page }) => {
          Iif (pageSize !== currentPageSize) {
            setPageSize(pageSize);
          }
          Eif (page !== currentPage) {
            goTo(page);
          }
        }}
        className={styles.paginationOverride}
      />
    </div>
  );
};
 
export default StockSourcesItems;