163 lines
10 KiB
TypeScript
163 lines
10 KiB
TypeScript
'use client';
|
|
|
|
import { Promotion } from '@/types/promotion';
|
|
import { getPromotionTypeLabel } from '@/types/promotion';
|
|
import { PencilIcon, TrashIcon, EyeIcon, EyeSlashIcon } from '@heroicons/react/24/outline';
|
|
import { formatDate } from '@/lib/utils';
|
|
import Image from 'next/image';
|
|
import { useState } from 'react';
|
|
|
|
interface PromotionListProps {
|
|
promotions: Promotion[];
|
|
onEdit: (promotion: Promotion) => void;
|
|
onDelete: (promotion: Promotion) => void;
|
|
onToggleActive: (promotion: Promotion) => void;
|
|
isLoading: boolean;
|
|
}
|
|
|
|
export default function PromotionList({ promotions, onEdit, onDelete, onToggleActive, isLoading }: PromotionListProps) {
|
|
const [imageErrors, setImageErrors] = useState<Record<number, boolean>>({});
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="flex justify-center items-center py-16">
|
|
<div className="h-10 w-10 border-4 border-indigo-200 dark:border-indigo-800 border-t-indigo-600 rounded-full animate-spin" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!promotions || promotions.length === 0) {
|
|
return (
|
|
<div className="text-center py-16 bg-white dark:bg-gray-900 rounded-2xl ring-1 ring-gray-200 dark:ring-gray-800 shadow-sm">
|
|
<div className="mx-auto h-12 w-12 rounded-xl bg-gray-100 dark:bg-gray-800 flex items-center justify-center mb-3">
|
|
<svg className="h-6 w-6 text-gray-400" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor">
|
|
<path strokeLinecap="round" strokeLinejoin="round" d="M10.34 15.84c-.688-.06-1.386-.09-2.09-.09H7.5a4.5 4.5 0 1 1 0-9h.75c.704 0 1.402-.03 2.09-.09m0 9.18c.253.962.584 1.892.985 2.783.247.55.06 1.21-.463 1.511l-.657.38c-.551.318-1.26.117-1.527-.461a20.845 20.845 0 0 1-1.44-4.282m3.102.069a18.03 18.03 0 0 1-.59-4.59c0-1.586.205-3.124.59-4.59m0 9.18a23.848 23.848 0 0 1-8.635 2.517m0 0a23.848 23.848 0 0 1-8.635-2.517m8.635 2.517a23.848 23.848 0 0 0 8.635 2.517m0 0a23.848 23.848 0 0 0 8.635-2.517M12 13.5a2.25 2.25 0 1 1-4.5 0 2.25 2.25 0 0 1 4.5 0Z" />
|
|
</svg>
|
|
</div>
|
|
<p className="text-gray-500 dark:text-gray-400 text-sm">هیچ تبلیغاتی یافت نشد</p>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const sortedPromotions = [...promotions].sort((a, b) => {
|
|
if (a.priority && b.priority) return b.priority - a.priority;
|
|
if (a.priority) return -1;
|
|
if (b.priority) return 1;
|
|
return new Date(b.created_at).getTime() - new Date(a.created_at).getTime();
|
|
});
|
|
|
|
const handleImageError = (id: number) => {
|
|
setImageErrors(prev => ({ ...prev, [id]: true }));
|
|
};
|
|
|
|
return (
|
|
<div className="bg-white dark:bg-gray-900 ring-1 ring-gray-200 dark:ring-gray-800 rounded-2xl overflow-hidden shadow-sm">
|
|
<div className="px-5 py-4 border-b border-gray-200 dark:border-gray-800">
|
|
<h3 className="text-base font-semibold text-gray-900 dark:text-gray-100">
|
|
لیست تبلیغات
|
|
</h3>
|
|
</div>
|
|
<ul className="divide-y divide-gray-200 dark:divide-gray-800">
|
|
{sortedPromotions.map((promotion) => (
|
|
<li key={promotion.id} className="px-5 py-4 hover:bg-gray-50 dark:hover:bg-gray-800/40 transition-colors">
|
|
<div className="flex items-start gap-4">
|
|
{promotion.image_url && !imageErrors[promotion.id] && (
|
|
<div className="flex-shrink-0">
|
|
<div className="relative h-16 w-16 rounded-xl overflow-hidden ring-1 ring-gray-200 dark:ring-gray-800">
|
|
<Image
|
|
src={promotion.image_url}
|
|
alt={promotion.title || 'Promotion'}
|
|
fill
|
|
className="object-cover"
|
|
onError={() => handleImageError(promotion.id)}
|
|
unoptimized
|
|
/>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex-1 min-w-0">
|
|
<div className="flex items-center gap-2 flex-wrap">
|
|
<p className="text-sm font-semibold text-indigo-600 dark:text-indigo-400">
|
|
{promotion.title || 'بدون عنوان'}
|
|
</p>
|
|
<span className="px-2 py-0.5 text-xs font-semibold rounded-lg bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300">
|
|
{getPromotionTypeLabel(promotion.type)}
|
|
</span>
|
|
{promotion.is_active ? (
|
|
<span className="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-semibold rounded-lg bg-green-50 text-green-700 dark:bg-green-500/10 dark:text-green-400">
|
|
<span className="h-1.5 w-1.5 rounded-full bg-green-500" />
|
|
فعال
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-semibold rounded-lg bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-400">
|
|
<span className="h-1.5 w-1.5 rounded-full bg-gray-400" />
|
|
غیرفعال
|
|
</span>
|
|
)}
|
|
{promotion.priority && (
|
|
<span className="px-2 py-0.5 text-xs font-medium rounded-lg bg-purple-100 text-purple-700 dark:bg-purple-900/30 dark:text-purple-300">
|
|
اولویت: {promotion.priority}
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{promotion.subtitle && (
|
|
<p className="mt-1 text-sm text-gray-600 dark:text-gray-400">{promotion.subtitle}</p>
|
|
)}
|
|
|
|
<div className="mt-2 flex flex-wrap gap-x-5 gap-y-1 text-xs text-gray-500 dark:text-gray-400">
|
|
{promotion.action_text && <span>دکمه: {promotion.action_text}</span>}
|
|
{promotion.start_at && <span>شروع: {formatDate(promotion.start_at)}</span>}
|
|
{promotion.end_at && <span>پایان: {formatDate(promotion.end_at)}</span>}
|
|
</div>
|
|
|
|
<div className="mt-1.5 flex flex-wrap gap-1.5">
|
|
{promotion.url_site && (
|
|
<span className="px-2 py-0.5 text-xs bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 rounded-md">وبسایت</span>
|
|
)}
|
|
{promotion.url_myket && (
|
|
<span className="px-2 py-0.5 text-xs bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 rounded-md">مایکت</span>
|
|
)}
|
|
{promotion.url_bazzar && (
|
|
<span className="px-2 py-0.5 text-xs bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 rounded-md">بازار</span>
|
|
)}
|
|
{promotion.url_google_play && (
|
|
<span className="px-2 py-0.5 text-xs bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 rounded-md">گوگلپلی</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-1 flex-shrink-0">
|
|
<button
|
|
onClick={() => onToggleActive(promotion)}
|
|
className={`p-2 rounded-lg transition-colors ${
|
|
promotion.is_active
|
|
? 'text-green-600 dark:text-green-400 hover:bg-green-50 dark:hover:bg-green-500/10'
|
|
: 'text-gray-400 dark:text-gray-500 hover:bg-gray-100 dark:hover:bg-gray-800'
|
|
}`}
|
|
title={promotion.is_active ? 'غیرفعال کردن' : 'فعال کردن'}
|
|
>
|
|
{promotion.is_active ? <EyeIcon className="h-4 w-4" /> : <EyeSlashIcon className="h-4 w-4" />}
|
|
</button>
|
|
<button
|
|
onClick={() => onEdit(promotion)}
|
|
className="p-2 rounded-lg text-indigo-600 dark:text-indigo-400 hover:bg-indigo-50 dark:hover:bg-indigo-500/10 transition-colors"
|
|
>
|
|
<PencilIcon className="h-4 w-4" />
|
|
</button>
|
|
<button
|
|
onClick={() => onDelete(promotion)}
|
|
className="p-2 rounded-lg text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-500/10 transition-colors"
|
|
>
|
|
<TrashIcon className="h-4 w-4" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
);
|
|
}
|