- 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.
450 lines
18 KiB
TypeScript
450 lines
18 KiB
TypeScript
import { CheckCircle2,Edit2,MessageSquareQuote,Plus,Star,Trash2,XCircle } from 'lucide-react';
|
||
import { useEffect,useState } from 'react';
|
||
import { toast } from 'react-hot-toast';
|
||
import Button from '../components/ui/Button';
|
||
import ConfirmModal from '../components/ui/ConfirmModal';
|
||
import MediaSelector from '../components/ui/MediaSelector';
|
||
import Modal from '../components/ui/Modal';
|
||
import Spinner from '../components/ui/Spinner';
|
||
import api,{ BASE_DOMAIN } from '../services/api';
|
||
import type { Testimonial } from '../types/admin';
|
||
|
||
|
||
export default function TestimonialsManager() {
|
||
const [testimonials, setTestimonials] = useState<Testimonial[]>([]);
|
||
const [isLoading, setIsLoading] = useState(true);
|
||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||
const [isMediaSelectorOpen, setIsMediaSelectorOpen] = useState(false);
|
||
const [editingTestimonial, setEditingTestimonial] = useState<Testimonial | null>(null);
|
||
const [deleteTargetId, setDeleteTargetId] = useState<string | null>(null);
|
||
const [isSaving, setIsSaving] = useState(false);
|
||
|
||
const [formData, setFormData] = useState({
|
||
authorName: '',
|
||
roleTitle: '',
|
||
avatarUrl: '',
|
||
content: '',
|
||
rating: 5,
|
||
isFeatured: true,
|
||
isActive: true,
|
||
showOnHome: false,
|
||
order: 0,
|
||
});
|
||
|
||
const fetchTestimonials = async () => {
|
||
try {
|
||
const res = await api.get('/testimonials');
|
||
const data = Array.isArray(res.data) ? res.data : (res.data?.data || []);
|
||
setTestimonials(data.sort((a: Testimonial, b: Testimonial) => a.order - b.order));
|
||
} catch (err) {
|
||
console.error('Failed to fetch testimonials:', err);
|
||
toast.error('خطا در دریافت لیست نظرات و گواهیها');
|
||
} finally {
|
||
setIsLoading(false);
|
||
}
|
||
};
|
||
|
||
useEffect(() => {
|
||
let isMounted = true;
|
||
api.get('/testimonials')
|
||
.then(res => {
|
||
if (!isMounted) return;
|
||
const data = Array.isArray(res.data) ? res.data : (res.data?.data || []);
|
||
setTestimonials(data.sort((a: Testimonial, b: Testimonial) => a.order - b.order));
|
||
})
|
||
.catch(err => {
|
||
console.error('Failed to fetch testimonials:', err);
|
||
toast.error('خطا در دریافت لیست نظرات و گواهیها');
|
||
})
|
||
.finally(() => {
|
||
if (isMounted) setIsLoading(false);
|
||
});
|
||
return () => {
|
||
isMounted = false;
|
||
};
|
||
}, []);
|
||
|
||
const openModal = (item: Testimonial | null = null) => {
|
||
if (item) {
|
||
setEditingTestimonial(item);
|
||
setFormData({
|
||
authorName: item.authorName || '',
|
||
roleTitle: item.roleTitle || '',
|
||
avatarUrl: item.avatarUrl || '',
|
||
content: item.content || '',
|
||
rating: item.rating || 5,
|
||
isFeatured: item.isFeatured ?? true,
|
||
isActive: item.isActive ?? true,
|
||
showOnHome: (item as any).showOnHome ?? false,
|
||
order: item.order || 0,
|
||
});
|
||
} else {
|
||
setEditingTestimonial(null);
|
||
setFormData({
|
||
authorName: '',
|
||
roleTitle: '',
|
||
avatarUrl: '',
|
||
content: '',
|
||
rating: 5,
|
||
isFeatured: true,
|
||
isActive: true,
|
||
showOnHome: false,
|
||
order: testimonials.length,
|
||
});
|
||
}
|
||
setIsModalOpen(true);
|
||
};
|
||
|
||
const handleToggleActive = async (item: Testimonial) => {
|
||
try {
|
||
await api.patch('/testimonials/' + item.id, { isActive: !item.isActive });
|
||
toast.success('وضعیت نظر تغییر کرد');
|
||
fetchTestimonials();
|
||
} catch (err) {
|
||
console.error('Failed to toggle testimonial status:', err);
|
||
toast.error('خطا در تغییر وضعیت نظر');
|
||
}
|
||
};
|
||
|
||
const handleSave = async (e: React.FormEvent) => {
|
||
e.preventDefault();
|
||
if (!formData.authorName.trim() || !formData.content.trim()) {
|
||
toast.error('نام نویسنده و متن نظر اجباری هستند');
|
||
return;
|
||
}
|
||
|
||
try {
|
||
setIsSaving(true);
|
||
if (editingTestimonial) {
|
||
await api.patch(`/testimonials/${editingTestimonial.id}`, formData);
|
||
toast.success('نظر با موفقیت بروزرسانی شد');
|
||
} else {
|
||
await api.post('/testimonials', formData);
|
||
toast.success('نظر جدید با موفقیت ایجاد شد');
|
||
}
|
||
setIsModalOpen(false);
|
||
fetchTestimonials();
|
||
} catch (err) {
|
||
console.error('Failed to save testimonial:', err);
|
||
toast.error('خطا در ذخیره نظر');
|
||
} finally {
|
||
setIsSaving(false);
|
||
}
|
||
};
|
||
|
||
const confirmDelete = async () => {
|
||
if (!deleteTargetId) return;
|
||
try {
|
||
await api.delete(`/testimonials/${deleteTargetId}`);
|
||
toast.success('نظر با موفقیت حذف شد');
|
||
fetchTestimonials();
|
||
} catch (err) {
|
||
console.error('Failed to delete testimonial:', err);
|
||
toast.error('خطا در حذف نظر');
|
||
} finally {
|
||
setDeleteTargetId(null);
|
||
}
|
||
};
|
||
|
||
const getAvatarFullUrl = (url?: string) => {
|
||
if (!url) return '';
|
||
if (url.startsWith('http')) return url;
|
||
return url.startsWith('/') ? `${BASE_DOMAIN}${url}` : `${BASE_DOMAIN}/${url}`;
|
||
};
|
||
|
||
return (
|
||
<div className="space-y-6">
|
||
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
|
||
<div>
|
||
<h2 className="text-2xl font-black text-gray-900 flex items-center gap-2">
|
||
<MessageSquareQuote className="w-6 h-6 text-purple-600" />
|
||
مدیریت نظرات دامپزشکان و مشتریان (Testimonials)
|
||
</h2>
|
||
<p className="text-gray-500 font-medium mt-1">مدیریت توصیه نامهها و نظرات تایید شده جهت نمایش در صفحه اصلی و گالری دامپزشکان</p>
|
||
</div>
|
||
<Button
|
||
variant="primary"
|
||
size="sm"
|
||
startIcon={Plus}
|
||
onClick={() => openModal()}
|
||
>
|
||
افزودن نظر جدید
|
||
</Button>
|
||
</div>
|
||
|
||
<div className="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
|
||
<div className="overflow-x-auto">
|
||
<table className="w-full text-right">
|
||
<thead className="bg-gray-50 border-b border-gray-100">
|
||
<tr>
|
||
<th className="py-4 px-6 text-sm font-bold text-gray-500">تصویر</th>
|
||
<th className="py-4 px-6 text-sm font-bold text-gray-500">نویسنده و سمت</th>
|
||
<th className="py-4 px-6 text-sm font-bold text-gray-500">متن نظر</th>
|
||
<th className="py-4 px-6 text-sm font-bold text-gray-500">امتیاز</th>
|
||
<th className="py-4 px-6 text-sm font-bold text-gray-500">ویژه / فعال</th>
|
||
<th className="py-4 px-6 text-sm font-bold text-gray-500 w-28">عملیات</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody className="divide-y divide-gray-100">
|
||
{isLoading ? (
|
||
<tr>
|
||
<td colSpan={6} className="py-12 text-center">
|
||
<Spinner size="lg" className="mx-auto text-purple-600" />
|
||
</td>
|
||
</tr>
|
||
) : testimonials.length === 0 ? (
|
||
<tr>
|
||
<td colSpan={6} className="py-12 text-center text-gray-500 font-medium">هیچ نظری ثبت نشده است</td>
|
||
</tr>
|
||
) : (
|
||
testimonials.map((item) => (
|
||
<tr key={item.id} className="hover:bg-gray-50/50 transition-colors">
|
||
<td className="py-3 px-6">
|
||
<div className="w-10 h-10 rounded-full bg-gray-100 border border-gray-200 overflow-hidden flex items-center justify-center">
|
||
{item.avatarUrl ? (
|
||
<img src={getAvatarFullUrl(item.avatarUrl)} alt={item.authorName} className="w-full h-full object-cover" />
|
||
) : (
|
||
<span className="font-bold text-gray-400 text-xs">{item.authorName.slice(0, 2)}</span>
|
||
)}
|
||
</div>
|
||
</td>
|
||
<td className="py-4 px-6">
|
||
<div className="flex flex-col">
|
||
<span className="font-bold text-gray-900">{item.authorName}</span>
|
||
{item.roleTitle && <span className="text-xs text-purple-600 font-medium">{item.roleTitle}</span>}
|
||
</div>
|
||
</td>
|
||
<td className="py-4 px-6">
|
||
<p className="text-gray-600 text-sm line-clamp-2 leading-relaxed max-w-md">{item.content}</p>
|
||
</td>
|
||
<td className="py-4 px-6">
|
||
<div className="flex items-center gap-1 text-amber-500">
|
||
{Array.from({ length: 5 }).map((_, i) => (
|
||
<Star
|
||
key={i}
|
||
className={`w-3.5 h-3.5 ${i < item.rating ? 'fill-amber-400 text-amber-400' : 'text-gray-200'}`}
|
||
/>
|
||
))}
|
||
</div>
|
||
</td>
|
||
<td className="py-4 px-6">
|
||
<div className="flex items-center gap-2">
|
||
<button
|
||
onClick={() => handleToggleActive(item)}
|
||
className={`inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-bold transition-colors cursor-pointer ${
|
||
item.isActive
|
||
? 'bg-emerald-50 text-emerald-700 hover:bg-emerald-100'
|
||
: 'bg-rose-50 text-rose-700 hover:bg-rose-100'
|
||
}`}
|
||
>
|
||
{item.isActive ? <CheckCircle2 className="w-3 h-3" /> : <XCircle className="w-3 h-3" />}
|
||
{item.isActive ? 'فعال' : 'غیرفعال'}
|
||
</button>
|
||
{(item as any).showOnHome && (
|
||
<span className="px-2 py-0.5 bg-blue-50 text-blue-700 text-xs font-bold rounded-md">
|
||
خانه
|
||
</span>
|
||
)}
|
||
</div>
|
||
</td>
|
||
<td className="py-4 px-6">
|
||
<div className="flex items-center gap-2">
|
||
<button
|
||
onClick={() => openModal(item)}
|
||
className="p-2 text-blue-600 hover:bg-blue-50 rounded-lg transition-colors cursor-pointer"
|
||
>
|
||
<Edit2 className="w-4 h-4" />
|
||
</button>
|
||
<button
|
||
onClick={() => setDeleteTargetId(item.id)}
|
||
className="p-2 text-red-600 hover:bg-red-50 rounded-lg transition-colors cursor-pointer"
|
||
>
|
||
<Trash2 className="w-4 h-4" />
|
||
</button>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
))
|
||
)}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
|
||
{/* Modal */}
|
||
{isModalOpen && (
|
||
<Modal
|
||
isOpen={isModalOpen}
|
||
onClose={() => setIsModalOpen(false)}
|
||
title={editingTestimonial ? 'ویرایش نظر' : 'افزودن نظر جدید'}
|
||
icon={MessageSquareQuote}
|
||
maxWidth="2xl"
|
||
footer={
|
||
<div className="flex justify-end gap-2.5 w-full">
|
||
<Button
|
||
variant="secondary"
|
||
size="sm"
|
||
type="button"
|
||
onClick={() => setIsModalOpen(false)}
|
||
>
|
||
انصراف
|
||
</Button>
|
||
<Button
|
||
variant="primary"
|
||
size="sm"
|
||
type="submit"
|
||
form="testimonialForm"
|
||
isLoading={isSaving}
|
||
>
|
||
ذخیره تغییرات
|
||
</Button>
|
||
</div>
|
||
}
|
||
>
|
||
<div className="space-y-6 text-right font-vazir">
|
||
<form id="testimonialForm" onSubmit={handleSave} className="space-y-4">
|
||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-1">نام شخص / نویسنده *</label>
|
||
<input
|
||
type="text"
|
||
required
|
||
value={formData.authorName}
|
||
onChange={(e) => setFormData({ ...formData, authorName: e.target.value })}
|
||
placeholder="مثال: دکتر علیرضا رضایی"
|
||
className="w-full px-4 py-2.5 rounded-xl border border-gray-200 focus:border-purple-500 outline-none text-sm"
|
||
/>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-1">سمت / تخصص</label>
|
||
<input
|
||
type="text"
|
||
value={formData.roleTitle}
|
||
onChange={(e) => setFormData({ ...formData, roleTitle: e.target.value })}
|
||
placeholder="مثال: متخصص جراحی دامپزشکی"
|
||
className="w-full px-4 py-2.5 rounded-xl border border-gray-200 focus:border-purple-500 outline-none text-sm"
|
||
/>
|
||
</div>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-1">تصویر نویسنده / آواتار</label>
|
||
<div className="flex gap-2">
|
||
<input
|
||
type="text"
|
||
value={formData.avatarUrl}
|
||
onChange={(e) => setFormData({ ...formData, avatarUrl: e.target.value })}
|
||
placeholder="آدرس تصویر یا انتخاب از رسانهها..."
|
||
dir="ltr"
|
||
className="flex-1 px-4 py-2.5 rounded-xl border border-gray-200 focus:border-purple-500 outline-none text-sm font-mono"
|
||
/>
|
||
<Button
|
||
type="button"
|
||
variant="outline"
|
||
size="sm"
|
||
onClick={() => setIsMediaSelectorOpen(true)}
|
||
>
|
||
انتخاب از رسانه
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-1">متن نظر / توصیه نامه *</label>
|
||
<textarea
|
||
required
|
||
rows={4}
|
||
value={formData.content}
|
||
onChange={(e) => setFormData({ ...formData, content: e.target.value })}
|
||
placeholder="متن کامل نظر و تجربه همکاری با محصولات کنینا..."
|
||
className="w-full px-4 py-2.5 rounded-xl border border-gray-200 focus:border-purple-500 outline-none text-sm leading-relaxed"
|
||
/>
|
||
</div>
|
||
|
||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 items-center">
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-1">امتیاز (از ۵ ستاره)</label>
|
||
<div className="flex items-center gap-2">
|
||
{[1, 2, 3, 4, 5].map((star) => (
|
||
<button
|
||
type="button"
|
||
key={star}
|
||
onClick={() => setFormData({ ...formData, rating: star })}
|
||
className="p-1 cursor-pointer"
|
||
>
|
||
<Star
|
||
className={`w-6 h-6 ${
|
||
star <= formData.rating ? 'fill-amber-400 text-amber-400' : 'text-gray-300'
|
||
}`}
|
||
/>
|
||
</button>
|
||
))}
|
||
<span className="text-sm font-bold text-gray-500 mr-2">({formData.rating} از ۵)</span>
|
||
</div>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-1">ترتیب نمایش</label>
|
||
<input
|
||
type="number"
|
||
value={formData.order}
|
||
onChange={(e) => setFormData({ ...formData, order: Number(e.target.value) })}
|
||
className="w-full px-4 py-2.5 rounded-xl border border-gray-200 focus:border-purple-500 outline-none text-sm font-bold"
|
||
/>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="grid grid-cols-2 gap-4 pt-2">
|
||
<label className="flex items-center justify-between p-3 bg-purple-50 rounded-xl border border-purple-200 cursor-pointer">
|
||
<span className="text-xs font-bold text-gray-700">وضعیت انتشار (Active)</span>
|
||
<input
|
||
type="checkbox"
|
||
checked={formData.isActive}
|
||
onChange={(e) => setFormData({ ...formData, isActive: e.target.checked })}
|
||
className="rounded text-purple-600 focus:ring-purple-500"
|
||
/>
|
||
</label>
|
||
|
||
<label className="flex items-center justify-between p-3 bg-blue-50 rounded-xl border border-blue-200 cursor-pointer">
|
||
<span className="text-xs font-bold text-blue-700">نمایش در خانه</span>
|
||
<input
|
||
type="checkbox"
|
||
checked={formData.showOnHome}
|
||
onChange={(e) => setFormData({ ...formData, showOnHome: e.target.checked })}
|
||
className="rounded text-blue-600 focus:ring-blue-500"
|
||
/>
|
||
</label>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</Modal>
|
||
)}
|
||
|
||
|
||
{/* Delete Confirmation Modal */}
|
||
{deleteTargetId && (
|
||
<ConfirmModal
|
||
isOpen={Boolean(deleteTargetId)}
|
||
title="حذف نظر"
|
||
message="آیا از حذف این نظر اطمینان دارید؟"
|
||
onConfirm={confirmDelete}
|
||
onCancel={() => setDeleteTargetId(null)}
|
||
/>
|
||
)}
|
||
|
||
{/* Media Selector */}
|
||
{isMediaSelectorOpen && (
|
||
<MediaSelector
|
||
isOpen={isMediaSelectorOpen}
|
||
onClose={() => setIsMediaSelectorOpen(false)}
|
||
onSelect={(url) => {
|
||
setFormData({ ...formData, avatarUrl: url });
|
||
setIsMediaSelectorOpen(false);
|
||
}}
|
||
/>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|