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";
|
2025-05-16 19:45:24 -04:00
|
|
|
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);
|
2025-05-16 19:45:24 -04:00
|
|
|
const { t } = useTranslation();
|
2025-05-12 11:37:28 -07:00
|
|
|
|
2025-05-16 19:45:24 -04: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);
|
|
|
|
|
|
2025-05-16 19:45:24 -04:00
|
|
|
// Send the test email with or without configuration
|
|
|
|
|
const response = await networkService.sendTestEmail({
|
|
|
|
|
to: user.email,
|
|
|
|
|
emailConfig
|
|
|
|
|
});
|
|
|
|
|
|
2025-05-12 11:37:28 -07:00
|
|
|
if (typeof response?.data?.data?.messageId !== "undefined") {
|
|
|
|
|
createToast({
|
2025-05-16 19:45:24 -04:00
|
|
|
body: t("settingsTestEmailSuccess", "Test email sent successfully"),
|
2025-05-12 11:37:28 -07:00
|
|
|
});
|
|
|
|
|
} else {
|
2025-05-16 19:45:24 -04:00
|
|
|
throw new Error(response?.data?.error || t("settingsTestEmailFailed", "Failed to send test email"));
|
2025-05-20 13:26:53 -07:00
|
|
|
|
2025-05-12 11:37:28 -07:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2025-05-19 23:21:00 +05:30
|
|
|
createToast({
|
2025-05-19 23:25:36 +05:30
|
|
|
body: t("failedToSendEmail"),
|
2025-05-19 23:21:00 +05:30
|
|
|
});
|
2025-05-12 11:37:28 -07:00
|
|
|
setError(error);
|
2025-05-16 19:45:24 -04:00
|
|
|
createToast({
|
|
|
|
|
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 };
|