305 lines
11 KiB
TypeScript
305 lines
11 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import { Send, Key } from 'lucide-react';
|
|
import { toast } from 'sonner';
|
|
import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav';
|
|
import { integrationService } from '@/services/api/integrationService';
|
|
|
|
const fieldClass =
|
|
'w-full h-9 px-3 bg-card border border-border crm-radius-control text-[13px] text-foreground focus:outline-none focus:border-primary disabled:opacity-60';
|
|
const areaClass =
|
|
'w-full px-3 py-2 bg-card border border-border crm-radius-control text-[13px] text-foreground focus:outline-none focus:border-primary disabled:opacity-60';
|
|
const primaryButton =
|
|
'inline-flex items-center justify-center gap-1.5 h-9 px-3 crm-radius-control bg-primary hover:bg-primary/90 text-white text-[13px] font-semibold cursor-pointer disabled:opacity-50';
|
|
const secondaryButton =
|
|
'inline-flex items-center justify-center gap-1.5 h-9 px-3 crm-radius-control border border-border bg-card text-foreground text-[13px] font-medium hover:bg-muted cursor-pointer disabled:opacity-50';
|
|
const shell =
|
|
'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0';
|
|
const labelClass = 'block text-[11px] font-semibold text-muted-foreground uppercase mb-1.5';
|
|
|
|
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="flex flex-col gap-5 min-w-0">
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<h1 className="text-lg font-semibold text-foreground">Email</h1>
|
|
<span className={`inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold ${creds.is_locked_by_config ? 'bg-primary/10 text-primary' : 'bg-muted text-foreground'}`}>
|
|
{creds.is_locked_by_config ? `Config · ${creds.active_provider}` : 'Database'}
|
|
</span>
|
|
</div>
|
|
<IntegrationsHeaderNav activeTab="email" />
|
|
|
|
<div className="grid grid-cols-1 lg:grid-cols-12 gap-6">
|
|
{/* Credentials Form (Cols 1-7) */}
|
|
<div className={`${shell} p-4 lg:col-span-7`}>
|
|
<div className="flex items-center justify-between mb-4">
|
|
<h3 className="font-semibold text-foreground flex items-center gap-2">
|
|
<Key className="w-4 h-4 text-primary" />
|
|
Provider Configuration
|
|
</h3>
|
|
<span className={secondaryButton}>
|
|
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={labelClass}>
|
|
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={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>
|
|
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={fieldClass}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>
|
|
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={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
{creds.active_provider === 'ZeptoMail' ? (
|
|
<div className="p-4 crm-radius-control border border-border bg-muted/40 space-y-1">
|
|
<p className="text-[13px] font-semibold text-foreground">ZeptoMail</p>
|
|
<p className="text-[12px] text-muted-foreground">
|
|
Token is read from ZEPTOMAIL_API_KEY.
|
|
</p>
|
|
{creds.endpoint && (
|
|
<p className="text-[11px] font-mono text-muted-foreground">{creds.endpoint}</p>
|
|
)}
|
|
</div>
|
|
) : (
|
|
<div className="space-y-4 pt-2 border-t border-border">
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className={labelClass}>
|
|
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="Host"
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>
|
|
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={fieldClass}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className={labelClass}>
|
|
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={fieldClass}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className={labelClass}>
|
|
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={fieldClass}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{!creds.is_locked_by_config && (
|
|
<button
|
|
type="submit"
|
|
disabled={saving}
|
|
className={`${primaryButton} w-full`}
|
|
>
|
|
{saving ? 'Saving...' : 'Save Email Configuration'}
|
|
</button>
|
|
)}
|
|
</form>
|
|
</div>
|
|
|
|
{/* Live Test Email Sender (Cols 8-12) */}
|
|
<div className={`${shell} p-4 lg:col-span-5 flex flex-col justify-between`}>
|
|
<div>
|
|
<h3 className="font-semibold text-foreground flex items-center gap-2 mb-4">
|
|
<Send className="w-4 h-4 text-primary" />
|
|
Live Test Email Dispatch
|
|
</h3>
|
|
|
|
<form onSubmit={handleSendTest} className="space-y-4">
|
|
<div>
|
|
<label className={labelClass}>
|
|
Recipient Email Address
|
|
</label>
|
|
<input
|
|
type="email"
|
|
placeholder="Email"
|
|
value={testRecipient}
|
|
onChange={(e) => setTestRecipient(e.target.value)}
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>
|
|
Subject Line
|
|
</label>
|
|
<input
|
|
type="text"
|
|
value={testSubject}
|
|
onChange={(e) => setTestSubject(e.target.value)}
|
|
className={fieldClass}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className={labelClass}>
|
|
Email Message Body
|
|
</label>
|
|
<textarea
|
|
rows={4}
|
|
value={testBody}
|
|
onChange={(e) => setTestBody(e.target.value)}
|
|
className={areaClass}
|
|
/>
|
|
</div>
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={sendingTest}
|
|
className={`${primaryButton} w-full`}
|
|
>
|
|
<Send className={`w-3.5 h-3.5 ${sendingTest ? 'animate-pulse' : ''}`} />
|
|
{sendingTest ? 'Dispatching Test Email...' : 'Send Test Email'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|