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

88.88% Statements 56/63
100% Branches 2/2
100% Functions 1/1
88.88% Lines 56/63

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 641x 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 { memo, useCallback } from 'react';
import { FormattedMessage } from 'react-intl';
import { useLocation } from 'react-router-dom';
 
import { Modal, Typography } from '@allshares/studio-design-system';
import { useNavigate } from '@amalia/ext/react-router-dom';
import { useFinalizePlanAgreement } from '@amalia/plan-agreements/state';
 
import { usePlanAgreementsModalsContext } from './PlanAgreementsModals.context';
 
export const PlanAgreementsFinalizeModal = memo(function PlanAgreementsFinalizeModal() {
  const location = useLocation();
  const navigate = useNavigate();
 
  const { closeModal, planAgreementForModal } = usePlanAgreementsModalsContext();
 
  const { mutate: finalizePlanAgreement, isPending } = useFinalizePlanAgreement(planAgreementForModal?.id);
  const handleClickFinalize = useCallback(() => {
    finalizePlanAgreement();
    closeModal();

    // If in preview page, redirect to plan agreement page
    if (location.pathname.includes('preview')) {
      navigate(-1);
    }
  }, [finalizePlanAgreement, closeModal, navigate, location]);
 
  return (
    <Modal
      isOpen
      onClose={closeModal}
    >
      <Modal.Content>
        <Modal.Header>
          <Modal.Title>
            <FormattedMessage defaultMessage="Finalize plan agreement" />
          </Modal.Title>
        </Modal.Header>
 
        <Modal.Body>
          <FormattedMessage
            defaultMessage="Are you sure you want to finalize <strong>{planAgreementName}</strong>?{br}{br}By finalizing the agreement, you will generate all plan agreements for assigned users. You will no longer be able to edit the settings."
            values={{
              strong: (chunks) => <Typography variant="bodyBaseBold">{chunks}</Typography>,
              planAgreementName: planAgreementForModal?.name,
            }}
          />
        </Modal.Body>
      </Modal.Content>
 
      <Modal.Actions>
        <Modal.CancelAction disabled={isPending} />
 
        <Modal.MainAction
          disabled={isPending}
          onClick={handleClickFinalize}
        >
          <FormattedMessage defaultMessage="Finalize" />
        </Modal.MainAction>
      </Modal.Actions>
    </Modal>
  );
});