Files
Checkmate/client/src/Hooks/inviteHooks.js
T

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2025-03-26 15:27:59 -07:00
import { useState } from "react";
import { networkService } from "../main";
2025-03-26 15:31:38 -07:00
import { useTranslation } from "react-i18next";
2025-05-06 19:01:23 -07:00
const CLIENT_HOST = import.meta.env.VITE_APP_CLIENT_HOST;
2025-03-26 15:27:59 -07:00
const useGetInviteToken = () => {
2025-03-26 15:31:38 -07:00
const { t } = useTranslation();
2025-03-26 15:27:59 -07:00
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") {
2025-03-26 15:31:38 -07:00
throw new Error(t("inviteNoTokenFound"));
2025-03-26 15:27:59 -07:00
}
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 };