From 575869c50a171c5f60a57a7e082f0332616589bf Mon Sep 17 00:00:00 2001 From: Tola Leng Date: Tue, 17 Jun 2025 21:39:00 +0800 Subject: [PATCH] Fix: Use correct API endpoint for test email --- .../settings/general/TestEmailDialog.tsx | 111 ++++++++++++++---- 1 file changed, 86 insertions(+), 25 deletions(-) diff --git a/application/src/components/settings/general/TestEmailDialog.tsx b/application/src/components/settings/general/TestEmailDialog.tsx index 08be7b6..03afddf 100644 --- a/application/src/components/settings/general/TestEmailDialog.tsx +++ b/application/src/components/settings/general/TestEmailDialog.tsx @@ -1,4 +1,3 @@ - import React, { useState } from 'react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; @@ -7,8 +6,9 @@ import { Label } from "@/components/ui/label"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { useLanguage } from "@/contexts/LanguageContext"; -import { Mail, X } from "lucide-react"; +import { Mail, X, AlertCircle, CheckCircle, Loader2 } from "lucide-react"; import { toast } from "@/hooks/use-toast"; +import { Alert, AlertDescription } from "@/components/ui/alert"; interface TestEmailDialogProps { open: boolean; @@ -33,9 +33,16 @@ const TestEmailDialog: React.FC = ({ const [email, setEmail] = useState(''); const [template, setTemplate] = useState('verification'); const [collection, setCollection] = useState('_superusers'); + const [lastResult, setLastResult] = useState<{ success: boolean; message: string } | null>(null); + const [isInternalTesting, setIsInternalTesting] = useState(false); + + const validateEmail = (email: string): boolean => { + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + return emailRegex.test(email); + }; const handleSend = async () => { - if (!email) { + if (!email.trim()) { toast({ title: "Error", description: "Please enter an email address", @@ -44,39 +51,73 @@ const TestEmailDialog: React.FC = ({ return; } + if (!validateEmail(email)) { + toast({ + title: "Error", + description: "Please enter a valid email address", + variant: "destructive", + }); + return; + } + try { + setLastResult(null); + setIsInternalTesting(true); + + console.log('Sending test email with data:', { + email, + template, + collection: template === 'verification' || template === 'password-reset' ? collection : undefined + }); + await onSendTest({ email, template, - collection: template === 'verification' ? collection : undefined + collection: template === 'verification' || template === 'password-reset' ? collection : undefined + }); + + setLastResult({ + success: true, + message: `Test email sent successfully to ${email}` }); toast({ title: "Success", - description: "Test email sent successfully", + description: `Test email sent successfully to ${email}`, variant: "default", }); - // Close dialog on success - handleClose(); } catch (error) { console.error('Error sending test email:', error); + const errorMessage = error instanceof Error ? error.message : "Failed to send test email"; + + setLastResult({ + success: false, + message: errorMessage + }); + toast({ title: "Error", - description: "Failed to send test email", + description: errorMessage, variant: "destructive", }); + } finally { + setIsInternalTesting(false); } }; const handleClose = () => { onOpenChange(false); - // Reset form + // Reset form but keep last result for reference setEmail(''); setTemplate('verification'); setCollection('_superusers'); + // Don't reset lastResult immediately to allow user to see the result + setTimeout(() => setLastResult(null), 300); }; + const isLoading = isTesting || isInternalTesting; + return ( @@ -90,16 +131,31 @@ const TestEmailDialog: React.FC = ({ size="icon" className="absolute right-4 top-4" onClick={handleClose} + disabled={isLoading} >
+ {/* Show last result */} + {lastResult && ( + + {lastResult.success ? ( + + ) : ( + + )} + + {lastResult.message} + + + )} + {/* Template Selection */}
- +
@@ -112,22 +168,14 @@ const TestEmailDialog: React.FC = ({
-
- - -
-
- - -
- {/* Auth Collection - only show for verification template */} - {template === 'verification' && ( + {/* Auth Collection - show for verification and password-reset templates */} + {(template === 'verification' || template === 'password-reset') && (
- @@ -148,21 +196,34 @@ const TestEmailDialog: React.FC = ({ onChange={(e) => setEmail(e.target.value)} placeholder={t("enterEmailAddress", "settings")} required + disabled={isLoading} />
+ + {/* Info message */} + + + + This will send a test email using your configured SMTP settings. Make sure SMTP is properly configured first. + +
-