Files
Checkmate/client/src/Hooks/inviteHooks.js
T
2025-05-06 19:01:23 -07:00

47 lines
1.1 KiB
JavaScript

import { useState } from "react";
import { networkService } from "../main";
import { useTranslation } from "react-i18next";
const CLIENT_HOST = import.meta.env.VITE_APP_CLIENT_HOST;
const useGetInviteToken = () => {
const { t } = useTranslation();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(undefined);
const [token, setToken] = useState(undefined);
const clearToken = () => {
setToken(undefined);
};
const getInviteToken = async ({ email, role }) => {
try {
const response = await networkService.requestInvitationToken({
email,
role,
});
const token = response?.data?.data?.token;
if (typeof token === "undefined") {
throw new Error(t("inviteNoTokenFound"));
}
let inviteLink = token;
if (typeof CLIENT_HOST !== "undefined") {
inviteLink = `${CLIENT_HOST}/register/${token}`;
}
setToken(inviteLink);
} catch (error) {
setError(error);
} finally {
setIsLoading(false);
}
};
return [getInviteToken, clearToken, isLoading, error, token];
};
export { useGetInviteToken };