- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
308 lines
12 KiB
TypeScript
308 lines
12 KiB
TypeScript
import { Mail } from 'lucide-react';
|
|
import { useEffect,useState } from 'react';
|
|
import toast from 'react-hot-toast';
|
|
import Button from '../components/ui/Button';
|
|
import Modal from '../components/ui/Modal';
|
|
import api from '../services/api';
|
|
|
|
|
|
interface ContactSubmission {
|
|
id: string;
|
|
name: string;
|
|
phone: string;
|
|
email?: string;
|
|
subject?: string;
|
|
message: string;
|
|
status: string;
|
|
adminNotes?: string;
|
|
createdAt: string;
|
|
}
|
|
|
|
interface ContactInfoItem {
|
|
key: string;
|
|
title: string;
|
|
value: string;
|
|
icon?: string;
|
|
order?: number;
|
|
}
|
|
|
|
export default function ContactSubmissions() {
|
|
const [activeTab, setActiveTab] = useState<'submissions' | 'info'>('submissions');
|
|
const [submissions, setSubmissions] = useState<ContactSubmission[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [infoItems, setInfoItems] = useState<ContactInfoItem[]>([]);
|
|
const [selectedSub, setSelectedSub] = useState<ContactSubmission | null>(null);
|
|
const [adminNotes, setAdminNotes] = useState('');
|
|
const [status, setStatus] = useState('PENDING');
|
|
|
|
const fetchSubmissionsData = async () => {
|
|
try {
|
|
const res = await api.get('/contact/submissions');
|
|
setSubmissions(res.data.items || []);
|
|
} catch {
|
|
// Handled by api interceptor
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
let isSubscribed = true;
|
|
|
|
api.get('/contact/submissions').then(res => {
|
|
if (isSubscribed) setSubmissions(res.data.items || []);
|
|
}).catch(() => {
|
|
// Handled by api interceptor
|
|
}).finally(() => {
|
|
if (isSubscribed) setLoading(false);
|
|
});
|
|
|
|
api.get('/contact/info').then(res => {
|
|
if (isSubscribed) setInfoItems(res.data || []);
|
|
}).catch(() => {
|
|
// Handled by api interceptor
|
|
});
|
|
|
|
return () => {
|
|
isSubscribed = false;
|
|
};
|
|
}, []);
|
|
|
|
const handleUpdateStatus = async () => {
|
|
if (!selectedSub) return;
|
|
try {
|
|
await api.put(`/contact/submissions/${selectedSub.id}`, { status, adminNotes });
|
|
toast.success('وضعیت پیام با موفقیت بهروزرسانی شد');
|
|
setSelectedSub(null);
|
|
fetchSubmissionsData();
|
|
} catch {
|
|
// Handled by api interceptor
|
|
}
|
|
};
|
|
|
|
const handleSaveContactInfo = async () => {
|
|
try {
|
|
await api.put('/contact/info', { items: infoItems });
|
|
toast.success('اطلاعات نمایندگی و دفتر مرکزی با موفقیت ذخیره شد');
|
|
} catch {
|
|
// Handled by api interceptor
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="p-6 dir-rtl">
|
|
<div className="flex justify-between items-center mb-6">
|
|
<h1 className="text-2xl font-bold text-gray-800">مدیریت تماس با ما & اطلاعات نمایندگی</h1>
|
|
<div className="flex gap-2">
|
|
<button
|
|
onClick={() => setActiveTab('submissions')}
|
|
className={`px-4 py-2 rounded-lg font-medium transition ${
|
|
activeTab === 'submissions' ? 'bg-amber-600 text-white' : 'bg-gray-100 text-gray-700'
|
|
}`}
|
|
>
|
|
پیامهای دریافت شده
|
|
</button>
|
|
<button
|
|
onClick={() => setActiveTab('info')}
|
|
className={`px-4 py-2 rounded-lg font-medium transition ${
|
|
activeTab === 'info' ? 'bg-amber-600 text-white' : 'bg-gray-100 text-gray-700'
|
|
}`}
|
|
>
|
|
اطلاعات نمایندگی & آدرسها
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{activeTab === 'submissions' ? (
|
|
<div className="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
|
|
{loading ? (
|
|
<div className="p-8 text-center text-gray-500">در حال بارگذاری...</div>
|
|
) : submissions.length === 0 ? (
|
|
<div className="p-8 text-center text-gray-500">هیچ پیامی ثبت نشده است.</div>
|
|
) : (
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-right border-collapse">
|
|
<thead>
|
|
<tr className="bg-gray-50 text-gray-600 text-sm font-semibold border-b">
|
|
<th className="p-4">نام فرستنده</th>
|
|
<th className="p-4">شماره تماس</th>
|
|
<th className="p-4">موضوع</th>
|
|
<th className="p-4">تاریخ ثبت</th>
|
|
<th className="p-4">وضعیت</th>
|
|
<th className="p-4">عملیات</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-gray-100 text-sm">
|
|
{submissions.map((sub) => (
|
|
<tr key={sub.id} className="hover:bg-gray-50/50">
|
|
<td className="p-4 font-medium text-gray-900">{sub.name}</td>
|
|
<td className="p-4 dir-ltr text-right">{sub.phone}</td>
|
|
<td className="p-4">{sub.subject || 'بدون موضوع'}</td>
|
|
<td className="p-4">
|
|
{new Date(sub.createdAt).toLocaleDateString('fa-IR')}
|
|
</td>
|
|
<td className="p-4">
|
|
<span
|
|
className={`px-2.5 py-1 rounded-full text-xs font-medium ${
|
|
sub.status === 'RESOLVED'
|
|
? 'bg-emerald-100 text-emerald-700'
|
|
: sub.status === 'IN_PROGRESS'
|
|
? 'bg-amber-100 text-amber-700'
|
|
: 'bg-rose-100 text-rose-700'
|
|
}`}
|
|
>
|
|
{sub.status === 'RESOLVED'
|
|
? 'پاسخ داده شده'
|
|
: sub.status === 'IN_PROGRESS'
|
|
? 'در حال پیگیری'
|
|
: 'جدید (نیازمند بررسی)'}
|
|
</span>
|
|
</td>
|
|
<td className="p-4">
|
|
<Button
|
|
variant="secondary"
|
|
size="xs"
|
|
onClick={() => {
|
|
setSelectedSub(sub);
|
|
setStatus(sub.status);
|
|
setAdminNotes(sub.adminNotes || '');
|
|
}}
|
|
>
|
|
مشاهده & تغییر وضعیت
|
|
</Button>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
</div>
|
|
) : (
|
|
<div className="bg-white rounded-xl shadow-sm border border-gray-100 p-6 max-w-4xl">
|
|
<h2 className="text-lg font-bold text-gray-800 mb-4">ویرایش داینامیک اطلاعات دفتر مرکزی & نمایندگیها</h2>
|
|
<div className="space-y-6">
|
|
{infoItems.map((item, idx) => (
|
|
<div key={item.key} className="p-4 bg-gray-50 rounded-xl border border-gray-200 space-y-3">
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 mb-1">عنوان بخش</label>
|
|
<input
|
|
type="text"
|
|
value={item.title}
|
|
onChange={(e) => {
|
|
const newItems = [...infoItems];
|
|
newItems[idx].title = e.target.value;
|
|
setInfoItems(newItems);
|
|
}}
|
|
className="w-full px-3 py-2 border rounded-lg text-sm"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 mb-1">آیکون (نام آیکون یا کلید)</label>
|
|
<input
|
|
type="text"
|
|
value={item.icon || ''}
|
|
onChange={(e) => {
|
|
const newItems = [...infoItems];
|
|
newItems[idx].icon = e.target.value;
|
|
setInfoItems(newItems);
|
|
}}
|
|
className="w-full px-3 py-2 border rounded-lg text-sm"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 mb-1">مقدار / متون اطلاعات</label>
|
|
<textarea
|
|
rows={3}
|
|
value={item.value}
|
|
onChange={(e) => {
|
|
const newItems = [...infoItems];
|
|
newItems[idx].value = e.target.value;
|
|
setInfoItems(newItems);
|
|
}}
|
|
className="w-full px-3 py-2 border rounded-lg text-sm"
|
|
/>
|
|
</div>
|
|
</div>
|
|
))}
|
|
<Button
|
|
variant="primary"
|
|
size="sm"
|
|
onClick={handleSaveContactInfo}
|
|
>
|
|
ذخیره تغییرات اطلاعات نمایندگی
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Modal for Submission Detail & Status Update */}
|
|
{selectedSub && (
|
|
<Modal
|
|
isOpen={!!selectedSub}
|
|
onClose={() => setSelectedSub(null)}
|
|
title={`جزئیات پیام ${selectedSub.name}`}
|
|
icon={Mail}
|
|
maxWidth="lg"
|
|
footer={
|
|
<div className="flex justify-end gap-2.5 w-full">
|
|
<Button
|
|
variant="secondary"
|
|
size="sm"
|
|
onClick={() => setSelectedSub(null)}
|
|
>
|
|
انصراف
|
|
</Button>
|
|
<Button
|
|
variant="primary"
|
|
size="sm"
|
|
onClick={handleUpdateStatus}
|
|
>
|
|
ثبت تغییرات
|
|
</Button>
|
|
</div>
|
|
}
|
|
>
|
|
<div className="space-y-4 text-right font-vazir">
|
|
<div className="space-y-3 text-sm text-gray-700 mb-6 bg-gray-50 p-4 rounded-xl">
|
|
<div><span className="font-semibold text-gray-900">تلفن:</span> {selectedSub.phone}</div>
|
|
{selectedSub.email && <div><span className="font-semibold text-gray-900">ایمیل:</span> {selectedSub.email}</div>}
|
|
<div><span className="font-semibold text-gray-900">موضوع:</span> {selectedSub.subject || 'ندارد'}</div>
|
|
<div>
|
|
<span className="font-semibold text-gray-900 block mb-1">متن پیام:</span>
|
|
<p className="bg-white p-3 rounded-lg border text-gray-800 leading-relaxed whitespace-pre-wrap">{selectedSub.message}</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="space-y-4">
|
|
<div>
|
|
<label className="block text-xs font-semibold text-gray-700 mb-1">تغییر وضعیت پیگیری</label>
|
|
<select
|
|
value={status}
|
|
onChange={(e) => setStatus(e.target.value)}
|
|
className="w-full px-3 py-2 border rounded-lg text-sm bg-white"
|
|
>
|
|
<option value="PENDING">جدید (نیازمند بررسی)</option>
|
|
<option value="IN_PROGRESS">در حال پیگیری توسط ادمین</option>
|
|
<option value="RESOLVED">پاسخ داده شد / تکمیل پیگیری</option>
|
|
</select>
|
|
</div>
|
|
<div>
|
|
<label className="block text-xs font-semibold text-gray-700 mb-1">یادداشت مدیر (اختیاری)</label>
|
|
<textarea
|
|
rows={3}
|
|
value={adminNotes}
|
|
onChange={(e) => setAdminNotes(e.target.value)}
|
|
placeholder="یادداشتهای داخلی جهت پیگیری کارشناسان..."
|
|
className="w-full px-3 py-2 border rounded-lg text-sm"
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
)}
|
|
|
|
</div>
|
|
);
|
|
}
|