331 lines
15 KiB
TypeScript
331 lines
15 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import {
|
|
Mail,
|
|
Send,
|
|
ShieldCheck,
|
|
Server,
|
|
Lock,
|
|
RefreshCw,
|
|
CheckCircle2,
|
|
AlertCircle,
|
|
Key,
|
|
} from 'lucide-react';
|
|
import { toast } from 'sonner';
|
|
import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav';
|
|
import { integrationService } from '@/services/api/integrationService';
|
|
|
|
export default function EmailIntegrationsPage() {
|
|
const [creds, setCreds] = useState<any>({
|
|
active_provider: 'ZeptoMail',
|
|
sender_email: '',
|
|
sender_name: 'iFixKart',
|
|
smtp_server: '',
|
|
smtp_port: 587,
|
|
smtp_username: '',
|
|
smtp_password: '',
|
|
use_tls: true,
|
|
source: 'unconfigured',
|
|
is_locked_by_config: false,
|
|
});
|
|
const [loading, setLoading] = useState(true);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
// Test send state
|
|
const [testRecipient, setTestRecipient] = useState('');
|
|
const [testSubject, setTestSubject] = useState('iFixKart ERP Email Verification Test');
|
|
const [testBody, setTestBody] = useState('This is a verified test email sent from iFixKart ERP Email Gateway.');
|
|
const [sendingTest, setSendingTest] = useState(false);
|
|
|
|
const fetchEmailConfig = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const data = await integrationService.getEmailCredentials();
|
|
setCreds(data);
|
|
} catch (err: any) {
|
|
toast.error('Failed to load email settings');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchEmailConfig();
|
|
}, []);
|
|
|
|
const handleSaveCredentials = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setSaving(true);
|
|
try {
|
|
await integrationService.saveEmailCredentials(creds);
|
|
toast.success('Email settings saved successfully');
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to save email settings');
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
const handleSendTest = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!testRecipient.trim() || !testRecipient.includes('@')) {
|
|
toast.error('Please enter a valid recipient email address');
|
|
return;
|
|
}
|
|
setSendingTest(true);
|
|
try {
|
|
const res = await integrationService.sendTestEmail({
|
|
recipient: testRecipient.trim(),
|
|
subject: testSubject.trim(),
|
|
body: testBody.trim(),
|
|
});
|
|
if (res.status === 'SUCCESS') {
|
|
toast.success(`Verification email dispatched to ${testRecipient}!`);
|
|
} else {
|
|
toast.error(`Email send failed: ${res.detail || JSON.stringify(res)}`);
|
|
}
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to send test email');
|
|
} finally {
|
|
setSendingTest(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="p-6 max-w-7xl mx-auto space-y-6">
|
|
<IntegrationsHeaderNav activeTab="email" />
|
|
|
|
{/* Top Banner */}
|
|
<div className="p-4 rounded-xl bg-sky-50 dark:bg-sky-950/30 border border-sky-200 dark:border-sky-900/50 flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
|
|
<div>
|
|
<h2 className="text-base font-semibold text-sky-900 dark:text-sky-100 flex items-center gap-2">
|
|
<Mail className="w-4 h-4 text-sky-600" />
|
|
Transactional Email Gateway (ZeptoMail & SMTP)
|
|
</h2>
|
|
<p className="text-xs text-sky-700 dark:text-sky-300 mt-0.5">
|
|
Manages automated order confirmations, password reset links, quotation invoices, and staff alerts.
|
|
</p>
|
|
</div>
|
|
{creds.is_locked_by_config ? (
|
|
<span className="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200 text-xs font-semibold">
|
|
<ShieldCheck className="w-3.5 h-3.5 text-blue-600" />
|
|
Active via Config.py ({creds.active_provider})
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-purple-100 dark:bg-purple-900 text-purple-800 dark:text-purple-200 text-xs font-semibold">
|
|
<Server className="w-3.5 h-3.5 text-purple-600" />
|
|
Custom Database SMTP
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 lg:grid-cols-12 gap-6">
|
|
{/* Credentials Form (Cols 1-7) */}
|
|
<div className="lg:col-span-7 rounded-xl border border-gray-200 dark:border-gray-800 bg-white dark:bg-gray-900 p-6 shadow-sm">
|
|
<div className="flex items-center justify-between mb-4">
|
|
<h3 className="font-semibold text-gray-900 dark:text-white flex items-center gap-2">
|
|
<Key className="w-4 h-4 text-sky-600" />
|
|
Provider Configuration
|
|
</h3>
|
|
<span className="text-xs font-mono px-2 py-0.5 rounded bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300">
|
|
Provider: {creds.active_provider}
|
|
</span>
|
|
</div>
|
|
|
|
<form onSubmit={handleSaveCredentials} className="space-y-4">
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
Active Provider
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.active_provider}
|
|
onChange={(e) => setCreds({ ...creds, active_provider: e.target.value })}
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 disabled:cursor-not-allowed focus:ring-2 focus:ring-sky-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
Sender Display Name
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.sender_name || 'iFixKart ERP'}
|
|
onChange={(e) => setCreds({ ...creds, sender_name: e.target.value })}
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 disabled:cursor-not-allowed focus:ring-2 focus:ring-sky-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
Sender Email Address
|
|
</label>
|
|
<input
|
|
type="email"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.sender_email || ''}
|
|
onChange={(e) => setCreds({ ...creds, sender_email: e.target.value })}
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 disabled:cursor-not-allowed focus:ring-2 focus:ring-sky-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
{creds.active_provider === 'ZeptoMail' ? (
|
|
<div className="p-4 rounded-lg bg-sky-50/50 dark:bg-sky-950/20 border border-sky-100 dark:border-sky-900/30 space-y-2">
|
|
<div className="flex items-center gap-2 text-xs font-semibold text-sky-800 dark:text-sky-200">
|
|
<CheckCircle2 className="w-4 h-4 text-sky-600" />
|
|
Zoho ZeptoMail CPaaS Active
|
|
</div>
|
|
<p className="text-xs text-gray-600 dark:text-gray-400">
|
|
ZeptoMail API Token is loaded from production environment variable <code className="font-mono text-sky-600">ZEPTOMAIL_API_KEY</code>.
|
|
High delivery inbox rate with SSL/TLS encryption.
|
|
</p>
|
|
<div className="text-[11px] font-mono text-gray-500">
|
|
Endpoint: {creds.endpoint || 'https://api.zeptomail.in/v1.1/email'}
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<div className="space-y-4 pt-2 border-t border-gray-100 dark:border-gray-800">
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
SMTP Host Server
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.smtp_server || ''}
|
|
onChange={(e) => setCreds({ ...creds, smtp_server: e.target.value })}
|
|
placeholder="e.g. smtp.gmail.com"
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
SMTP Port
|
|
</label>
|
|
<input
|
|
type="number"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.smtp_port || 587}
|
|
onChange={(e) => setCreds({ ...creds, smtp_port: parseInt(e.target.value) || 587 })}
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
SMTP Username
|
|
</label>
|
|
<input
|
|
type="text"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.smtp_username || ''}
|
|
onChange={(e) => setCreds({ ...creds, smtp_username: e.target.value })}
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
SMTP Password
|
|
</label>
|
|
<input
|
|
type="password"
|
|
disabled={creds.is_locked_by_config}
|
|
value={creds.smtp_password || ''}
|
|
onChange={(e) => setCreds({ ...creds, smtp_password: e.target.value })}
|
|
placeholder="••••••••••••"
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60"
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{!creds.is_locked_by_config && (
|
|
<button
|
|
type="submit"
|
|
disabled={saving}
|
|
className="w-full py-2.5 px-4 rounded-lg bg-sky-600 hover:bg-sky-700 text-white text-xs font-semibold shadow-sm transition"
|
|
>
|
|
{saving ? 'Saving...' : 'Save Email Configuration'}
|
|
</button>
|
|
)}
|
|
</form>
|
|
</div>
|
|
|
|
{/* Live Test Email Sender (Cols 8-12) */}
|
|
<div className="lg:col-span-5 rounded-xl border border-gray-200 dark:border-gray-800 bg-white dark:bg-gray-900 p-6 shadow-sm flex flex-col justify-between">
|
|
<div>
|
|
<h3 className="font-semibold text-gray-900 dark:text-white flex items-center gap-2 mb-2">
|
|
<Send className="w-4 h-4 text-sky-600" />
|
|
Live Test Email Dispatch
|
|
</h3>
|
|
<p className="text-xs text-gray-500 dark:text-gray-400 mb-4">
|
|
Sends an immediate verification email to verify SMTP handshake or ZeptoMail CPaaS API delivery.
|
|
</p>
|
|
|
|
<form onSubmit={handleSendTest} className="space-y-4">
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
Recipient Email Address
|
|
</label>
|
|
<input
|
|
type="email"
|
|
placeholder="admin@ifixkart.com"
|
|
value={testRecipient}
|
|
onChange={(e) => setTestRecipient(e.target.value)}
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white focus:ring-2 focus:ring-sky-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
Subject Line
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={testSubject}
|
|
onChange={(e) => setTestSubject(e.target.value)}
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white focus:ring-2 focus:ring-sky-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
|
Email Message Body
|
|
</label>
|
|
<textarea
|
|
rows={4}
|
|
value={testBody}
|
|
onChange={(e) => setTestBody(e.target.value)}
|
|
className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white focus:ring-2 focus:ring-sky-500 focus:outline-none"
|
|
/>
|
|
</div>
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={sendingTest}
|
|
className="w-full py-2.5 px-4 rounded-lg bg-sky-600 hover:bg-sky-700 text-white text-xs font-semibold shadow-sm transition flex items-center justify-center gap-2"
|
|
>
|
|
<Send className={`w-3.5 h-3.5 ${sendingTest ? 'animate-pulse' : ''}`} />
|
|
{sendingTest ? 'Dispatching Test Email...' : 'Send Test Email'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
<div className="mt-6 pt-4 border-t border-gray-100 dark:border-gray-800 text-xs text-gray-500">
|
|
TLS Enabled: <span className="font-semibold text-emerald-600">Yes (Strict Port 587/465)</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|