feat: Enhance useSendTestEmail hook to support testing without saving

- Add support for optional email configuration parameter
- Implement improved error handling with detailed messages
- Add internationalization support for all user feedback
- Return appropriate error information for better UX
- Maintain backward compatibility with existing code
This commit is contained in:
mohadeseh safari committed 2025-05-16 19:45:24 -04:00
1 parent 083c395f10
commit dbcdb24da6
4 files changed
+56 -14

No files matched your search

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