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

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2025-05-12 11:37:28 -07:00
import { useState } from "react";
import { networkService } from "../main";
import { useSelector } from "react-redux";
import { createToast } from "../Utils/toastUtils";
import { useTranslation } from "react-i18next";
2025-05-12 11:37:28 -07:00
const useSendTestEmail = () => {
const [isSending, setIsSending] = useState(false);
const [error, setError] = useState(null);
const user = useSelector((state) => state.auth.user);
const { t } = useTranslation();
2025-05-12 11:37:28 -07:00
/**
* Send a test email with optional email configuration
* @param {Object} emailConfig - Optional email configuration parameters
*/
const sendTestEmail = async (emailConfig = null) => {
2025-05-12 11:37:28 -07:00
try {
setIsSending(true);
setError(null);
// Send the test email with or without configuration
2025-05-27 09:10:32 -07:00
const response = await networkService.sendTestEmail({
to: user.email,
2025-05-27 09:10:32 -07:00
emailConfig,
});
2025-05-27 09:10:32 -07:00
2025-05-12 11:37:28 -07:00
if (typeof response?.data?.data?.messageId !== "undefined") {
createToast({
body: t("settingsTestEmailSuccess", "Test email sent successfully"),
2025-05-12 11:37:28 -07:00
});
} else {
2025-05-27 09:10:32 -07:00
throw new Error(
response?.data?.error ||
t("settingsTestEmailFailed", "Failed to send test email")
);
2025-05-12 11:37:28 -07:00
}
} catch (error) {
2025-05-19 23:21:00 +05:30
createToast({
body: t("failedToSendEmail"),
2025-05-19 23:21:00 +05:30
});
2025-05-12 11:37:28 -07:00
setError(error);
createToast({
2025-05-27 09:10:32 -07:00
body: t(
"settingsTestEmailFailedWithReason",
"Failed to send test email: {{reason}}",
{
reason: error.message || t("settingsTestEmailUnknownError", "Unknown error"),
}
),
variant: "error",
});
2025-05-12 11:37:28 -07:00
} finally {
setIsSending(false);
}
};
return [isSending, error, sendTestEmail];
};
export { useSendTestEmail };