All files / src/stock-operations/stock-operations-forms/steps stock-operation-item-cell.component.tsx

4.54% Statements 1/22
0% Branches 0/24
0% Functions 0/4
5.55% Lines 1/18

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                        3x                                                                          
import { InlineLoading } from '@carbon/react';
import { ConfigurableLink, showSnackbar, useConfig } from '@openmrs/esm-framework';
import React, { useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { type ConfigObject } from '../../../config-schema';
import { URL_STOCK_ITEM } from '../../../constants';
import { useStockItem } from '../../../stock-items/stock-items.resource';
 
type StockOperationItemCellProps = {
  stockItemUuid: string;
};
 
const StockOperationItemCell: React.FC<StockOperationItemCellProps> = ({ stockItemUuid }) => {
  const { isLoading, error, item } = useStockItem(stockItemUuid);
  const { t } = useTranslation();
  const { useItemCommonNameAsDisplay } = useConfig<ConfigObject>();
 
  const commonName = useMemo(() => {
    if (!useItemCommonNameAsDisplay) return;
    const drugName = item?.drugName ? `(Drug name: ${item.drugName})` : undefined;
    return `${item?.commonName || t('noCommonNameAvailable', 'No common name available') + (drugName ?? '')}`;
  }, [item, useItemCommonNameAsDisplay, t]);
 
  const drugName = useMemo(() => {
    if (useItemCommonNameAsDisplay) return;
    const commonName = item?.commonName ? `(Common name: ${item.commonName})` : undefined;
    return `${item?.drugName || t('noDrugNameAvailable', 'No drug name available') + (commonName ?? '')}`;
  }, [item, useItemCommonNameAsDisplay, t]);
  useEffect(() => {
    if (error) {
      showSnackbar({
        kind: 'error',
        title: t('stockItemError', 'Error loading stock item'),
        subtitle: error?.message,
      });
    }
  }, [error, t]);
 
  if (isLoading) return <InlineLoading status="active" iconDescription="Loading" />;
  if (error) return <>--</>;
 
  return (
    <ConfigurableLink target={'_blank'} to={window.spaBase + URL_STOCK_ITEM(stockItemUuid)}>
      {useItemCommonNameAsDisplay ? commonName : drugName}
    </ConfigurableLink>
  );
};
 
export default StockOperationItemCell;