322 lines
13 KiB
TypeScript
322 lines
13 KiB
TypeScript
import { Edit2, Plus, Search, Trash2, Activity, Tag, Sparkles } from 'lucide-react';
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
import { toast } from 'react-hot-toast';
|
|
import Button from '../components/ui/Button';
|
|
import ConfirmModal from '../components/ui/ConfirmModal';
|
|
import Modal from '../components/ui/Modal';
|
|
import Pagination from '../components/ui/Pagination';
|
|
import Spinner from '../components/ui/Spinner';
|
|
import api from '../services/api';
|
|
import { useSearchParams } from 'react-router-dom';
|
|
|
|
export interface Symptom {
|
|
id: string;
|
|
name: string;
|
|
slug: string;
|
|
description?: string;
|
|
metaTitle?: string;
|
|
metaDescription?: string;
|
|
keywords?: string;
|
|
productsCount?: number;
|
|
createdAt?: string;
|
|
}
|
|
|
|
export default function SymptomsPage() {
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
const [symptoms, setSymptoms] = useState<Symptom[]>([]);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const [search, setSearch] = useState(() => searchParams.get('search') || '');
|
|
const [page, setPage] = useState(() => Number(searchParams.get('page')) || 1);
|
|
const [totalPages, setTotalPages] = useState(1);
|
|
const limit = 15;
|
|
|
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
const [editingSymptom, setEditingSymptom] = useState<Symptom | null>(null);
|
|
const [deleteTargetId, setDeleteTargetId] = useState<string | null>(null);
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
const [formData, setFormData] = useState({
|
|
name: '',
|
|
slug: '',
|
|
description: '',
|
|
metaTitle: '',
|
|
metaDescription: '',
|
|
keywords: ''
|
|
});
|
|
|
|
const openModal = (symptom: Symptom | null = null) => {
|
|
if (symptom) {
|
|
setEditingSymptom(symptom);
|
|
setFormData({
|
|
name: symptom.name,
|
|
slug: symptom.slug,
|
|
description: symptom.description || '',
|
|
metaTitle: symptom.metaTitle || '',
|
|
metaDescription: symptom.metaDescription || '',
|
|
keywords: symptom.keywords || ''
|
|
});
|
|
} else {
|
|
setEditingSymptom(null);
|
|
setFormData({
|
|
name: '',
|
|
slug: '',
|
|
description: '',
|
|
metaTitle: '',
|
|
metaDescription: '',
|
|
keywords: ''
|
|
});
|
|
}
|
|
setIsModalOpen(true);
|
|
};
|
|
|
|
const closeModal = () => {
|
|
setIsModalOpen(false);
|
|
};
|
|
|
|
const fetchSymptoms = useCallback(async () => {
|
|
try {
|
|
setIsLoading(true);
|
|
const res = await api.get('/admin/symptoms', { params: { page, limit, search } });
|
|
if (res.data?.data) {
|
|
setSymptoms(res.data.data);
|
|
setTotalPages(res.data.meta?.lastPage || 1);
|
|
}
|
|
} catch {
|
|
toast.error('خطا در دریافت لیست علائم');
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
}, [page, search]);
|
|
|
|
useEffect(() => {
|
|
fetchSymptoms();
|
|
}, [fetchSymptoms]);
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!formData.name.trim()) {
|
|
toast.error('نام علامت الزامی است');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setIsSubmitting(true);
|
|
if (editingSymptom) {
|
|
await api.put(`/admin/symptoms/${editingSymptom.id}`, formData);
|
|
toast.success('علامت با موفقیت ویرایش و در تمامی محصولات همگام شد');
|
|
} else {
|
|
await api.post('/admin/symptoms', formData);
|
|
toast.success('علامت درمانی جدید با موفقیت ایجاد شد');
|
|
}
|
|
closeModal();
|
|
fetchSymptoms();
|
|
} catch {
|
|
toast.error('خطا در ذخیره اطلاعات علامت');
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
};
|
|
|
|
const handleDelete = async () => {
|
|
if (!deleteTargetId) return;
|
|
try {
|
|
await api.delete(`/admin/symptoms/${deleteTargetId}`);
|
|
toast.success('علامت با موفقیت حذف شد');
|
|
setDeleteTargetId(null);
|
|
fetchSymptoms();
|
|
} catch {
|
|
toast.error('خطا در حذف علامت');
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-6 font-vazir" dir="rtl">
|
|
{/* Header */}
|
|
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4 bg-white p-6 rounded-3xl border border-gray-100 shadow-sm">
|
|
<div>
|
|
<div className="flex items-center gap-2.5">
|
|
<span className="w-10 h-10 rounded-2xl bg-canina-blue/10 text-canina-blue flex items-center justify-center">
|
|
<Activity className="w-5 h-5 text-purple-600" />
|
|
</span>
|
|
<h1 className="text-xl font-black text-gray-900">مدیریت علائم بالینی و درمانی</h1>
|
|
</div>
|
|
<p className="text-xs text-gray-500 font-bold mt-1">
|
|
تعریف و ویرایش علائم، بهینهسازی متادیتاهای سئو و همگامسازی یکپارچه با محصولات کاتالوگ
|
|
</p>
|
|
</div>
|
|
<Button onClick={() => openModal()} className="bg-purple-600 hover:bg-purple-700 text-white rounded-2xl px-5 py-3 flex items-center gap-2 font-bold text-xs shadow-md shadow-purple-600/20">
|
|
<Plus className="w-4 h-4" />
|
|
<span>افزودن علامت جدید</span>
|
|
</Button>
|
|
</div>
|
|
|
|
{/* Filter & Search */}
|
|
<div className="bg-white p-4 rounded-3xl border border-gray-100 shadow-sm flex items-center gap-3">
|
|
<div className="relative flex-1">
|
|
<Search className="w-4 h-4 text-gray-400 absolute right-3 top-1/2 -translate-y-1/2" />
|
|
<input
|
|
type="text"
|
|
placeholder="جستجوی علامت بالینی بر اساس نام یا اسلاگ..."
|
|
value={search}
|
|
onChange={(e) => {
|
|
setSearch(e.target.value);
|
|
setPage(1);
|
|
}}
|
|
className="w-full bg-gray-50 border border-gray-200 rounded-2xl py-2.5 pr-10 pl-4 text-xs focus:outline-none focus:ring-2 focus:ring-purple-500/20"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Table */}
|
|
<div className="bg-white rounded-3xl border border-gray-100 shadow-sm overflow-hidden">
|
|
{isLoading ? (
|
|
<div className="py-20 flex justify-center">
|
|
<Spinner />
|
|
</div>
|
|
) : symptoms.length === 0 ? (
|
|
<div className="py-20 text-center text-gray-400 font-bold text-sm">
|
|
هیچ علامت بالینی یافت نشد.
|
|
</div>
|
|
) : (
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-right text-xs">
|
|
<thead className="bg-gray-50/75 border-b border-gray-100 text-gray-500 font-black uppercase">
|
|
<tr>
|
|
<th className="px-6 py-4">نام علامت بالینی</th>
|
|
<th className="px-6 py-4">اسلاگ URL</th>
|
|
<th className="px-6 py-4">محصولات متصل</th>
|
|
<th className="px-6 py-4">سئو متاتایتل</th>
|
|
<th className="px-6 py-4 text-left">عملیات</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-gray-100">
|
|
{symptoms.map((symptom) => (
|
|
<tr key={symptom.id} className="hover:bg-purple-50/20 transition-colors">
|
|
<td className="px-6 py-4 font-black text-gray-900 flex items-center gap-2">
|
|
<Tag className="w-3.5 h-3.5 text-purple-600" />
|
|
<span>{symptom.name}</span>
|
|
</td>
|
|
<td className="px-6 py-4 font-mono text-gray-500 dir-ltr text-right">
|
|
{symptom.slug}
|
|
</td>
|
|
<td className="px-6 py-4">
|
|
<span className="px-2.5 py-1 bg-purple-50 text-purple-700 font-black rounded-lg border border-purple-100">
|
|
{symptom.productsCount || 0} محصول
|
|
</span>
|
|
</td>
|
|
<td className="px-6 py-4 text-gray-600 max-w-xs truncate" title={symptom.metaTitle}>
|
|
{symptom.metaTitle || '—'}
|
|
</td>
|
|
<td className="px-6 py-4 text-left space-x-2 space-x-reverse">
|
|
<button
|
|
onClick={() => openModal(symptom)}
|
|
className="p-2 text-blue-600 hover:bg-blue-50 rounded-xl transition-colors"
|
|
title="ویرایش و سئو"
|
|
>
|
|
<Edit2 className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
onClick={() => setDeleteTargetId(symptom.id)}
|
|
className="p-2 text-red-600 hover:bg-red-50 rounded-xl transition-colors"
|
|
title="حذف"
|
|
>
|
|
<Trash2 className="w-4 h-4" />
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
|
|
<div className="p-4 border-t border-gray-100 flex justify-center">
|
|
<Pagination currentPage={page} totalPages={totalPages} onPageChange={(p) => setPage(p)} />
|
|
</div>
|
|
</div>
|
|
|
|
{/* Modal CRUD */}
|
|
<Modal isOpen={isModalOpen} onClose={closeModal} title={editingSymptom ? 'ویرایش علامت و سئو' : 'تعریف علامت جدید'}>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<div>
|
|
<label className="block text-xs font-bold text-gray-700 mb-1">نام علامت درمانی (فارسی) *</label>
|
|
<input
|
|
type="text"
|
|
required
|
|
value={formData.name}
|
|
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
|
className="w-full bg-gray-50 border border-gray-200 rounded-xl p-3 text-xs focus:outline-none focus:ring-2 focus:ring-purple-500/20"
|
|
placeholder="مثلاً: پوکی استخوان یا کمخونی"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-bold text-gray-700 mb-1">اسلاگ URL و انگلیسی</label>
|
|
<input
|
|
type="text"
|
|
value={formData.slug}
|
|
onChange={(e) => setFormData({ ...formData, slug: e.target.value })}
|
|
className="w-full bg-gray-50 border border-gray-200 rounded-xl p-3 text-xs dir-ltr focus:outline-none focus:ring-2 focus:ring-purple-500/20"
|
|
placeholder="anemia یا osteoporosis"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-xs font-bold text-gray-700 mb-1">توضیحات تشخیصی و بالینی</label>
|
|
<textarea
|
|
rows={3}
|
|
value={formData.description}
|
|
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
|
className="w-full bg-gray-50 border border-gray-200 rounded-xl p-3 text-xs focus:outline-none focus:ring-2 focus:ring-purple-500/20"
|
|
placeholder="توضیحات بالینی و پروتکلهای مکملهای درمانی..."
|
|
/>
|
|
</div>
|
|
|
|
<div className="pt-2 border-t border-gray-100 space-y-3">
|
|
<div className="flex items-center gap-1.5 text-xs font-black text-purple-700">
|
|
<Sparkles className="w-3.5 h-3.5" />
|
|
<span>تنظیمات پیشرفته سئو (SEO Meta)</span>
|
|
</div>
|
|
<div>
|
|
<label className="block text-[11px] font-bold text-gray-600 mb-1">Meta Title</label>
|
|
<input
|
|
type="text"
|
|
value={formData.metaTitle}
|
|
onChange={(e) => setFormData({ ...formData, metaTitle: e.target.value })}
|
|
className="w-full bg-gray-50 border border-gray-200 rounded-xl p-2.5 text-xs"
|
|
placeholder="عنوان سئو در نتایج گوگل..."
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-[11px] font-bold text-gray-600 mb-1">Meta Description</label>
|
|
<textarea
|
|
rows={2}
|
|
value={formData.metaDescription}
|
|
onChange={(e) => setFormData({ ...formData, metaDescription: e.target.value })}
|
|
className="w-full bg-gray-50 border border-gray-200 rounded-xl p-2.5 text-xs"
|
|
placeholder="توضیحات متای صفحه سئو..."
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex justify-end gap-2 pt-4">
|
|
<Button type="button" variant="outline" onClick={closeModal}>انصراف</Button>
|
|
<Button type="submit" disabled={isSubmitting} className="bg-purple-600 hover:bg-purple-700 text-white">
|
|
{isSubmitting ? 'در حال ثبت...' : (editingSymptom ? 'بروزرسانی یکپارچه' : 'ثبت علامت')}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</Modal>
|
|
|
|
{/* Delete Confirmation */}
|
|
<ConfirmModal
|
|
isOpen={!!deleteTargetId}
|
|
onClose={() => setDeleteTargetId(null)}
|
|
onConfirm={handleDelete}
|
|
title="حذف علامت بالینی"
|
|
description="با حذف این علامت، ارتباط آن از تمام محصولات کاتالوگ نیز به طور خودکار حذف خواهد شد. آیا مطمئن هستید؟"
|
|
/>
|
|
</div>
|
|
);
|
|
}
|