All files / publisher-admin/pages/Collaboration InviteConfirmationModal.tsx

82.75% Statements 24/29
88.88% Branches 16/18
57.14% Functions 4/7
82.75% Lines 24/29

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                                                        2x 7x                   7x 7x 7x 7x 7x 7x 7x 7x 7x 7x       7x                       7x                   7x                       1x 1x                 2x   2x             2x             2x 1x     2x 1x                                          
import { useParams } from "react-router-dom";
import { useQueryClient } from "react-query";
import { useRecoilState, useSetRecoilState, useRecoilValue } from "recoil";
import { Button, Modal as CanonicalModal } from "@canonical/react-components";
import type { Props as ModalProps } from "@canonical/react-components/dist/components/Modal/Modal";
 
import {
  activeInviteEmailState,
  invitesListState,
  inviteLinkState,
  collaboratorsListState,
  inviteEmailLinkState,
  publisherState,
} from "../../state/atoms";
import {
  filteredInvitesListState,
  filteredCollaboratorsListState,
} from "../../state/selectors";
import { useSendMutation, useRevokeMutation } from "../../hooks";
 
type Props = {
  action: string;
  setShowModal: (showModal: boolean) => void;
  setShowSuccess: (showSuccess: boolean) => void;
  setShowError: (showError: boolean) => void;
  queryKey?: string;
};
 
const Modal = (props: ModalProps): JSX.Element => {
  return CanonicalModal(props) as unknown as JSX.Element;
};
 
function InviteConfirmationModal({
  action,
  setShowModal,
  setShowSuccess,
  setShowError,
  queryKey,
}: Props) {
  const { packageName } = useParams();
  const queryClient = useQueryClient();
  const setInvitesList = useSetRecoilState(invitesListState);
  const invitesList = useRecoilValue(filteredInvitesListState);
  const setCollaboratorsList = useSetRecoilState(collaboratorsListState);
  const collaboratorsList = useRecoilValue(filteredCollaboratorsListState);
  const publisher = useRecoilValue(publisherState);
  const setInviteLink = useSetRecoilState(inviteLinkState);
  const setInviteEmailLink = useSetRecoilState(inviteEmailLinkState);
  const [activeInviteEmail, setActiveInviteEmail] = useRecoilState(
    activeInviteEmailState
  );
 
  const sendMutation = useSendMutation(
    packageName,
    publisher?.["display-name"],
    activeInviteEmail,
    setInviteLink,
    setInviteEmailLink,
    setShowSuccess,
    setShowError,
    queryClient,
    window.CSRF_TOKEN
  );
 
  const revokeMutation = useRevokeMutation(
    packageName,
    activeInviteEmail,
    queryKey || "",
    setShowSuccess,
    setShowError,
    queryClient,
    window.CSRF_TOKEN
  );
 
  return (
    <Modal
      close={() => {
        setShowModal(false);
        setActiveInviteEmail("");
      }}
      title={`${action} ${queryKey && queryKey === "collaboratorsData" ? "collaborator" : "invite"}`}
      buttonRow={
        <>
          <Button
            className="u-no-margin--bottom"
            onClick={() => {
              setShowModal(false);
              setActiveInviteEmail("");
            }}
          >
            Cancel
          </Button>
          <Button
            appearance="positive"
            className="u-no-margin--bottom"
            onClick={() => {
              setShowModal(false);
 
              Iif (queryKey && queryKey === "collaboratorsData") {
                setCollaboratorsList(
                  collaboratorsList.filter((collaborator) => {
                    return collaborator?.account?.email !== activeInviteEmail;
                  })
                );
              } else {
                setInvitesList(
                  invitesList.filter((invite) => {
                    return invite?.email !== activeInviteEmail;
                  })
                );
              }
 
              if (action === "Reopen") {
                sendMutation.mutate();
              }
 
              if (action === "Revoke") {
                revokeMutation.mutate();
              }
            }}
          >
            {action} invite
          </Button>
        </>
      }
    >
      <p>
        Are you sure you want to {action.toLowerCase()} the{" "}
        {queryKey && queryKey === "collaboratorsData"
          ? "collaborator"
          : "invite"}{" "}
        for <strong>{activeInviteEmail}</strong>?
      </p>
    </Modal>
  );
}
 
export default InviteConfirmationModal;