import React, { useMemo, useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { DataTable, DataTableSkeleton, IconButton, Pagination, Table, TableBatchActions, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow, TableToolbar, TableToolbarAction, TableToolbarContent, TableToolbarMenu, TableToolbarSearch, Tile, } from '@carbon/react'; import { Edit } from '@carbon/react/icons'; import { isDesktop, restBaseUrl } from '@openmrs/esm-framework'; import { handleMutate } from '../utils'; import { launchAddOrEditStockItemWorkspace } from './stock-item.utils'; import { ResourceRepresentation } from '../core/api/api'; import { useDebounce } from '../core/hooks/debounce-hook'; import { useStockItemsPages } from './stock-items-table.resource'; import AddStockItemActionButton from './add-stock-item/add-stock-action-button.component'; import AddStockItemsBulktImportActionButton from './add-bulk-stock-item/add-stock-items-bulk-import-action-button.component'; import EditStockItemActionsMenu from './edit-stock-item/edit-stock-item-action-menu.component'; import FilterStockItems from './components/filter-stock-items/filter-stock-items.component'; import { type CustomTableHeader } from '../core/components/table/types'; import styles from './stock-items-table.scss'; interface StockItemsTableProps { from?: string; } const StockItemsTableComponent: React.FC = () => { const { t } = useTranslation(); const [searchInput, setSearchInput] = useState(''); const handleRefresh = () => { handleMutate(`${restBaseUrl}/stockmanagement/stockitem`); }; const { currentPage, currentPageSize, isDrug, isLoading, items, pageSizes, setCurrentPage, setDrug, setPageSize, setSearchString, totalCount, } = useStockItemsPages(ResourceRepresentation.Full); const handleSearch = (query: string) => { setSearchInput(query); }; const debouncedSearch = useDebounce((query: string) => { setSearchString(query); }, 1000); useEffect(() => { debouncedSearch(searchInput); }, [searchInput, debouncedSearch]); const tableHeaders = useMemo( () => [ { id: 0, header: t('type', 'Type'), key: 'type', }, { id: 1, header: t('genericName', 'Generic name'), key: 'genericName', }, { id: 2, header: t('commonName', 'Common name'), key: 'commonName', }, { id: 3, header: t('tradeName', 'Trade name'), key: 'tradeName', }, { id: 4, header: t('dispensingUnitName', 'Dispensing UoM'), key: 'dispensingUnitName', }, { id: 5, header: t('defaultStockOperationsUoMName', 'Bulk packaging'), key: 'defaultStockOperationsUoMName', }, { id: 6, header: t('reorderLevel', 'Reorder level'), key: 'reorderLevel', }, { id: 7, header: t('actions', 'Actions'), key: 'actions', }, ], [t], ); const tableRows = useMemo(() => { return items?.map((stockItem, index) => ({ ...stockItem, id: stockItem?.uuid, key: `key-${stockItem?.uuid}`, uuid: `${stockItem?.uuid}`, type: stockItem?.drugUuid ? t('drug', 'Drug') : t('other', 'Other'), genericName: , commonName: stockItem?.commonName, tradeName: stockItem?.drugUuid ? stockItem?.conceptName : '', preferredVendorName: stockItem?.preferredVendorName, dispensingUoM: stockItem?.defaultStockOperationsUoMName, dispensingUnitName: stockItem?.dispensingUnitName, defaultStockOperationsUoMName: stockItem?.defaultStockOperationsUoMName, reorderLevel: stockItem?.reorderLevelUoMName && stockItem?.reorderLevel ? `${stockItem?.reorderLevel?.toLocaleString()} ${stockItem?.reorderLevelUoMName}` : '', actions: ( { stockItem.isDrug = !!stockItem.drugUuid; launchAddOrEditStockItemWorkspace(t, stockItem); }} > ), })); }, [items, t]); if (isLoading) { return ; } return ( <>

{t('stockItemsTableHeader', 'Drugs and other stock items managed by the system.')}

{({ rows, headers, getHeaderProps, getTableProps, getRowProps, getBatchActionProps }) => ( handleSearch(typeof e === 'string' ? e : (e as React.ChangeEvent).target.value) } persistent placeholder={t('searchStockItems', 'Search stock items')} value={searchInput} /> {t('refresh', 'Refresh')} {headers.map( (header) => header.key !== 'details' && ( {(() => { 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); })()} ), )} {rows.map((row) => { return ( {row.cells.map( (cell) => cell?.info?.header !== 'details' && {cell.value}, )} ); })}
{rows.length === 0 ? (

{t('noStockItemsToDisplay', 'No stock items to display')}

{t('checkFilters', 'Check the filters above')}

) : null}
)}
{ setCurrentPage(page); setPageSize(pageSize); }} page={currentPage} pageSize={currentPageSize} pageSizes={pageSizes} totalItems={totalCount} /> ); }; export default StockItemsTableComponent;