All files / publisher-admin/hooks useRevokeMutation.ts

88.88% Statements 16/18
83.33% Branches 5/6
100% Functions 4/4
88.88% Lines 16/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                      6x   3x 1x     2x   2x 2x   2x         2x 1x 1x     1x   1x         1x       1x     3x 3x                      
import { QueryClient, useMutation } from "react-query";
 
function useRevokeMutation(
  packageName: string | undefined,
  activeInviteEmail: string | undefined,
  queryKey: string,
  setShowRevokeSuccess: (value: boolean) => void,
  setShowRevokeError: (value: boolean) => void,
  queryClient: QueryClient,
  csrfToken: string
) {
  return useMutation(
    async () => {
      if (!activeInviteEmail) {
        return;
      }
 
      const formData = new FormData();
 
      formData.set("csrf_token", csrfToken);
      formData.set("collaborator", activeInviteEmail);
 
      const response = await fetch(`/api/packages/${packageName}/invites`, {
        method: "DELETE",
        body: formData,
      });
 
      if (!response.ok) {
        setShowRevokeError(true);
        throw new Error(response.statusText);
      }
 
      const responseData = await response.json();
 
      Iif (!responseData.success) {
        setShowRevokeError(true);
        throw new Error(responseData.message);
      }
 
      setShowRevokeSuccess(true);
    },
    {
      onError: ({ context }) => {
        queryClient.setQueryData(queryKey, context?.previousInvites);
      },
      onSettled: () => {
        queryClient.invalidateQueries();
        window.scrollTo({
          top: 0,
          left: 0,
          behavior: "smooth",
        });
      },
    }
  );
}
 
export default useRevokeMutation;