All files / libs/plan-agreements/components/src/lib/modals PlanAgreementsDeleteModal.tsx

87.14% Statements 61/70
100% Branches 2/2
100% Functions 1/1
87.14% Lines 61/70

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 711x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { IconTrash } from '@tabler/icons-react';
import { memo, useCallback } from 'react';
import { FormattedMessage } from 'react-intl';
import { generatePath, useLocation } from 'react-router-dom';
 
import { Modal, Typography } from '@allshares/studio-design-system';
import { routes } from '@amalia/core/routes';
import { useNavigate } from '@amalia/ext/react-router-dom';
import { assert } from '@amalia/ext/typescript';
import { useDeletePlanAgreement } from '@amalia/plan-agreements/state';
 
import { usePlanAgreementsModalsContext } from './PlanAgreementsModals.context';
 
export const PlanAgreementsDeleteModal = memo(function PlanAgreementsDeleteModal() {
  const location = useLocation();
  const navigate = useNavigate();
 
  const { closeModal, planAgreementForModal } = usePlanAgreementsModalsContext();
  const { mutate: deletePlanAgreement, isPending } = useDeletePlanAgreement(planAgreementForModal?.id);
 
  const handleClickDelete = useCallback(() => {
    assert(planAgreementForModal?.id, 'Plan agreement is required');

    deletePlanAgreement();
    closeModal();

    // If in plan agreement page or preview, redirect to plan agreements list
    if (location.pathname.includes(planAgreementForModal.id)) {
      navigate(generatePath(routes.PLAN_AGREEMENTS_LIST));
    }
  }, [deletePlanAgreement, closeModal, planAgreementForModal?.id, navigate, location]);
 
  return (
    <Modal
      isOpen
      variant="danger"
      onClose={closeModal}
    >
      <Modal.Content>
        <Modal.Header>
          <Modal.Title>
            <FormattedMessage defaultMessage="Delete agreement" />
          </Modal.Title>
        </Modal.Header>
 
        <Modal.Body>
          <FormattedMessage
            defaultMessage="Are you sure you want to delete <strong>{planAgreementName}</strong>?{br}You won't be able to do any actions with this agreement."
            values={{
              strong: (chunks) => <Typography variant="bodyBaseBold">{chunks}</Typography>,
              planAgreementName: planAgreementForModal?.name,
            }}
          />
        </Modal.Body>
      </Modal.Content>
 
      <Modal.Actions>
        <Modal.CancelAction />
 
        <Modal.MainAction
          icon={<IconTrash />}
          isLoading={isPending}
          onClick={handleClickDelete}
        >
          <FormattedMessage defaultMessage="Delete" />
        </Modal.MainAction>
      </Modal.Actions>
    </Modal>
  );
});