All files / src/pages/Login InviteMembers.tsx

0% Statements 0/17
0% Branches 0/18
0% Functions 0/4
0% Lines 0/17

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                                                                                                                                             
import LoadingSpinner from "@components/Snipper/LoadingSpinner";
import { customInstance } from "@services/api";
import React, { useState } from "react";
import { useQuery } from "react-query";
import { useLocation } from "react-router-dom";
import ExpiredPasswordReset from "./ExpiredPasswordReset";
import { showMessage } from "@common/Toast";
 
function InviteMembers() {
  const location = useLocation();
 
  const queryParams = new URLSearchParams(location.search);
 
  // Extract the values of the query parameters
  const accountId = queryParams.get("accountID");
  const inviteId = queryParams.get("inviteID");
  const token = queryParams.get("token");
  const email = queryParams.get("email");
  const allDataAvailable = !!accountId && !!inviteId && !!token && !!email;
  const [{ tokenExpired, title, message }, setData] = useState({
    tokenExpired: false,
    title: "Missing Parameters",
    message: `Sorry, some parameters required to invite ${
      email || "the user"
    } are missing`,
  });
 
  useQuery(
    ["invite_user", accountId, inviteId, token, email],
    async () => {
      return customInstance({
        url: `/accounts/${accountId}/invites/${inviteId}/accept?token=${token}&email=${email}`,
      });
    },
    {
      onSuccess(data) {
        showMessage("Success", `${email} successfully invited `);
      },
      onError(err: any) {
        const errMessage =
          err.response?.data?.message ||
          `an error occurred while trying to invite ${email || "user"}`;
        setData({
          title: "Error Inviting user",
          message: errMessage,
          tokenExpired: true,
        });
 
        showMessage("Error", errMessage);
      },
      enabled: allDataAvailable,
    },
  );
  if (tokenExpired || !allDataAvailable)
    return (
      <ExpiredPasswordReset
        {...((!allDataAvailable || title || message) && {
          title,
          message,
        })}
      />
    );
  return (
    <div>
      <LoadingSpinner />
    </div>
  );
}
 
export default InviteMembers;