All files / src/stock-sources/stock-sources-delete stock-sources-delete.component.tsx

83.33% Statements 15/18
50% Branches 1/2
66.66% Functions 4/6
83.33% Lines 15/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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65                        2x 3x 3x   3x   3x 2x       1x 1x 1x     1x   1x 1x                                   1x           3x         3x        
import React, { useState } from 'react';
import { Button, InlineLoading } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { TrashCan } from '@carbon/react/icons';
import { deleteStockSource } from '../stock-sources.resource';
import { restBaseUrl, showModal, showSnackbar } from '@openmrs/esm-framework';
import { useHandleMutate } from '../../utils';
 
interface StockSourcesDeleteActionMenuProps {
  uuid: string;
}
 
const StockSourcesDeleteActionMenu: React.FC<StockSourcesDeleteActionMenuProps> = ({ uuid }) => {
  const { t } = useTranslation();
  const handleMutate = useHandleMutate();
 
  const [deletingSource, setDeletingSource] = useState(false);
 
  const handleDeleteStockSource = React.useCallback(() => {
    const close = showModal('delete-stock-modal', {
      close: () => close(),
      uuid: uuid,
      onConfirmation: () => {
        const ids = [];
        ids.push(uuid);
        deleteStockSource(ids)
          .then(
            () => {
              setDeletingSource(false);
 
              handleMutate(`${restBaseUrl}/stockmanagement/stocksource`);
              showSnackbar({
                isLowContrast: true,
                title: t('deletingSource', 'Delete Source'),
                kind: 'success',
                subtitle: t('stockSourceDeletedSuccessfully', 'Stock Source Deleted Successfully'),
              });
            },
            (error) => {
              setDeletingSource(false);
              showSnackbar({
                title: t('errorDeletingSource', 'Error deleting a source'),
                kind: 'error',
                isLowContrast: true,
                subtitle: error?.message,
              });
            },
          )
          .catch();
        close();
      },
    });
  }, [t, uuid, handleMutate]);
 
  const deleteButton = (
    <Button kind="ghost" size="md" onClick={handleDeleteStockSource} aria-label={t('deleteSource', 'Delete Source')}>
      <TrashCan size={16} />
    </Button>
  );
 
  return deletingSource ? <InlineLoading /> : deleteButton;
};
 
export default StockSourcesDeleteActionMenu;