feat: promotion feature added

This commit is contained in:
2026-02-23 21:04:18 +03:30
parent 240e8f4c13
commit 577d8afb53
6 changed files with 912 additions and 0 deletions
@@ -0,0 +1,395 @@
'use client';
import { useState, useEffect } from 'react';
import { Promotion, CreatePromotionData, UpdatePromotionData, PROMOTION_TYPES } from '@/types/promotion';
import { PhotoIcon, XMarkIcon } from '@heroicons/react/24/outline';
import Image from 'next/image';
interface PromotionFormProps {
promotion?: Promotion | null;
onSubmit: (data: CreatePromotionData | UpdatePromotionData) => Promise<void>;
onCancel: () => void;
isLoading: boolean;
}
export default function PromotionForm({ promotion, onSubmit, onCancel, isLoading }: PromotionFormProps) {
const [formData, setFormData] = useState<CreatePromotionData | UpdatePromotionData>({
type: 'slider',
title: '',
subtitle: '',
action_text: '',
url_myket: '',
url_bazzar: '',
url_google_play: '',
url_site: '',
priority: 0,
start_at: '',
end_at: '',
});
const [imageFile, setImageFile] = useState<File | null>(null);
const [imagePreview, setImagePreview] = useState<string | null>(null);
const [isActive, setIsActive] = useState(true);
useEffect(() => {
if (promotion) {
// Format dates for input fields
const startAt = promotion.start_at ? promotion.start_at.slice(0, 16) : '';
const endAt = promotion.end_at ? promotion.end_at.slice(0, 16) : '';
setFormData({
type: promotion.type || 'slider',
title: promotion.title || '',
subtitle: promotion.subtitle || '',
action_text: promotion.action_text || '',
url_myket: promotion.url_myket || '',
url_bazzar: promotion.url_bazzar || '',
url_google_play: promotion.url_google_play || '',
url_site: promotion.url_site || '',
priority: promotion.priority || 0,
start_at: startAt,
end_at: endAt,
});
setIsActive(promotion.is_active);
if (promotion.image_url) {
setImagePreview(promotion.image_url);
}
} else {
// Set default priority
setFormData(prev => ({ ...prev, priority: 0 }));
}
}, [promotion]);
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>) => {
const { name, value } = e.target;
setFormData(prev => ({
...prev,
[name]: value === '' ? null : value,
}));
};
const handleNumberChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setFormData(prev => ({
...prev,
[name]: value === '' ? null : parseInt(value, 10),
}));
};
const handleImageChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (file) {
setImageFile(file);
setFormData(prev => ({ ...prev, image: file }));
// Create preview
const reader = new FileReader();
reader.onloadend = () => {
setImagePreview(reader.result as string);
};
reader.readAsDataURL(file);
}
};
const removeImage = () => {
setImageFile(null);
setImagePreview(null);
setFormData(prev => ({ ...prev, image: null }));
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
// Format dates properly
let submitData: any = { ...formData };
if (submitData.start_at) {
submitData.start_at = submitData.start_at.replace('T', ' ') + ':00';
}
if (submitData.end_at) {
submitData.end_at = submitData.end_at.replace('T', ' ') + ':00';
}
// Add is_active for updates
if (promotion) {
submitData.is_active = isActive;
}
await onSubmit(submitData);
};
return (
<form onSubmit={handleSubmit} className="space-y-6 bg-white p-6 rounded-lg shadow">
<div>
<h3 className="text-lg font-medium text-gray-900 mb-4">
{promotion ? 'ویرایش تبلیغ' : 'ایجاد تبلیغ جدید'}
</h3>
</div>
<div className="grid grid-cols-1 gap-6 md:grid-cols-2">
{/* Type */}
<div>
<label htmlFor="type" className="block text-sm font-medium text-gray-700 mb-1">
نوع <span className="text-red-500">*</span>
</label>
<select
id="type"
name="type"
required
value={formData.type || 'slider'}
onChange={handleInputChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
>
{Object.entries(PROMOTION_TYPES).map(([value, label]) => (
<option key={value} value={value}>
{label}
</option>
))}
</select>
</div>
{/* Title */}
<div>
<label htmlFor="title" className="block text-sm font-medium text-gray-700 mb-1">
عنوان
</label>
<input
type="text"
id="title"
name="title"
value={formData.title || ''}
onChange={handleInputChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
placeholder="عنوان تبلیغ"
/>
</div>
{/* Subtitle */}
<div className="md:col-span-2">
<label htmlFor="subtitle" className="block text-sm font-medium text-gray-700 mb-1">
زیرعنوان
</label>
<input
type="text"
id="subtitle"
name="subtitle"
value={formData.subtitle || ''}
onChange={handleInputChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
placeholder="زیرعنوان تبلیغ"
/>
</div>
{/* Action Text */}
<div>
<label htmlFor="action_text" className="block text-sm font-medium text-gray-700 mb-1">
متن دکمه
</label>
<input
type="text"
id="action_text"
name="action_text"
value={formData.action_text || ''}
onChange={handleInputChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
placeholder="مثال: همین حالا نصب کن"
/>
</div>
{/* Priority */}
<div>
<label htmlFor="priority" className="block text-sm font-medium text-gray-700 mb-1">
اولویت
</label>
<input
type="number"
id="priority"
name="priority"
min="0"
value={formData.priority || 0}
onChange={handleNumberChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
placeholder="عدد بالاتر = اولویت بیشتر"
/>
</div>
{/* Start Date */}
<div>
<label htmlFor="start_at" className="block text-sm font-medium text-gray-700 mb-1">
تاریخ شروع
</label>
<input
type="datetime-local"
id="start_at"
name="start_at"
value={formData.start_at || ''}
onChange={handleInputChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
/>
</div>
{/* End Date */}
<div>
<label htmlFor="end_at" className="block text-sm font-medium text-gray-700 mb-1">
تاریخ پایان
</label>
<input
type="datetime-local"
id="end_at"
name="end_at"
value={formData.end_at || ''}
onChange={handleInputChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
/>
</div>
</div>
{/* URLs Section */}
<div className="border-t border-gray-200 pt-4">
<h4 className="text-md font-medium text-gray-900 mb-3">لینکها</h4>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
{/* Myket URL */}
<div>
<label htmlFor="url_myket" className="block text-sm font-medium text-gray-700 mb-1">
لینک مایکت
</label>
<input
type="url"
id="url_myket"
name="url_myket"
value={formData.url_myket || ''}
onChange={handleInputChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
placeholder="https://myket.ir/app/..."
/>
</div>
{/* Bazaar URL */}
<div>
<label htmlFor="url_bazzar" className="block text-sm font-medium text-gray-700 mb-1">
لینک بازار
</label>
<input
type="url"
id="url_bazzar"
name="url_bazzar"
value={formData.url_bazzar || ''}
onChange={handleInputChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
placeholder="https://cafebazaar.ir/app/..."
/>
</div>
{/* Google Play URL */}
<div>
<label htmlFor="url_google_play" className="block text-sm font-medium text-gray-700 mb-1">
لینک گوگلپلی
</label>
<input
type="url"
id="url_google_play"
name="url_google_play"
value={formData.url_google_play || ''}
onChange={handleInputChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
placeholder="https://play.google.com/store/apps/..."
/>
</div>
{/* Site URL */}
<div>
<label htmlFor="url_site" className="block text-sm font-medium text-gray-700 mb-1">
لینک وبسایت
</label>
<input
type="url"
id="url_site"
name="url_site"
value={formData.url_site || ''}
onChange={handleInputChange}
className="block w-full px-3 py-2 text-slate-900 border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
placeholder="https://example.com"
/>
</div>
</div>
</div>
{/* Image Upload */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
تصویر
</label>
<div className="flex items-center space-x-3 space-x-reverse">
<label className="cursor-pointer flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
<PhotoIcon className="h-5 w-5 ml-2 text-gray-400" />
انتخاب تصویر
<input
type="file"
accept="image/*"
onChange={handleImageChange}
className="hidden"
/>
</label>
{imagePreview && (
<button
type="button"
onClick={removeImage}
className="text-red-600 hover:text-red-900"
>
<XMarkIcon className="h-5 w-5" />
</button>
)}
</div>
{imagePreview && (
<div className="mt-2 relative h-32 w-32 rounded-lg overflow-hidden border border-gray-200">
<img
src={imagePreview}
alt="Preview"
className="w-full h-full object-cover"
/>
</div>
)}
<p className="mt-1 text-xs text-gray-500">
حداکثر حجم: ۲ مگابایت
</p>
</div>
{/* Active Status (only for edit) */}
{promotion && (
<div className="flex items-center">
<input
type="checkbox"
id="is_active"
checked={isActive}
onChange={(e) => setIsActive(e.target.checked)}
className="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded"
/>
<label htmlFor="is_active" className="mr-2 block text-sm text-gray-900">
فعال
</label>
</div>
)}
{/* Form Actions */}
<div className="flex justify-end space-x-3 space-x-reverse pt-4 border-t">
<button
type="button"
onClick={onCancel}
className="px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
>
انصراف
</button>
<button
type="submit"
disabled={isLoading}
className="px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50"
>
{isLoading ? 'در حال ذخیره...' : promotion ? 'به‌روزرسانی' : 'ایجاد'}
</button>
</div>
</form>
);
}
@@ -0,0 +1,199 @@
'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-12">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-indigo-600"></div>
</div>
);
}
if (!promotions || promotions.length === 0) {
return (
<div className="text-center py-12 bg-white rounded-lg shadow">
<p className="text-gray-500">هیچ تبلیغاتی یافت نشد</p>
</div>
);
}
// Sort promotions by priority (higher priority first) and then by creation date
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 shadow overflow-hidden sm:rounded-lg">
<div className="px-4 py-5 sm:px-6">
<h3 className="text-lg leading-6 font-medium text-gray-900">
لیست تبلیغات
</h3>
</div>
<ul className="divide-y divide-gray-200">
{sortedPromotions.map((promotion) => (
<li key={promotion.id} className="px-6 py-4 hover:bg-gray-50">
<div className="flex items-start justify-between">
<div className="flex-1 min-w-0">
<div className="flex items-start space-x-4 space-x-reverse">
{/* Promotion Image - Using unoptimized Image component */}
{promotion.image_url && !imageErrors[promotion.id] && (
<div className="flex-shrink-0">
<div className="relative h-20 w-20 rounded-lg overflow-hidden border border-gray-200">
<Image
src={promotion.image_url}
alt={promotion.title || 'Promotion'}
fill
className="object-cover"
onError={() => handleImageError(promotion.id)}
unoptimized={true} // This disables image optimization
/>
</div>
</div>
)}
{/* Promotion Details */}
<div className="flex-1">
<div className="flex items-center justify-between">
<div className="flex items-center space-x-2 space-x-reverse">
<p className="text-sm font-medium text-indigo-600">
{promotion.title || 'بدون عنوان'}
</p>
<span className="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-blue-100 text-blue-800">
{getPromotionTypeLabel(promotion.type)}
</span>
{promotion.is_active ? (
<span className="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
فعال
</span>
) : (
<span className="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">
غیرفعال
</span>
)}
</div>
</div>
{promotion.subtitle && (
<p className="mt-1 text-sm text-gray-600">
{promotion.subtitle}
</p>
)}
{promotion.action_text && (
<p className="mt-1 text-xs text-gray-500">
متن دکمه: {promotion.action_text}
</p>
)}
<div className="mt-2 grid grid-cols-1 gap-2 sm:grid-cols-2 lg:grid-cols-4">
{promotion.priority && (
<div className="text-xs text-gray-500">
<span className="font-medium">اولویت:</span> {promotion.priority}
</div>
)}
{promotion.start_at && (
<div className="text-xs text-gray-500">
<span className="font-medium">شروع:</span> {formatDate(promotion.start_at)}
</div>
)}
{promotion.end_at && (
<div className="text-xs text-gray-500">
<span className="font-medium">پایان:</span> {formatDate(promotion.end_at)}
</div>
)}
</div>
{/* URLs */}
<div className="mt-2 space-y-1">
{promotion.url_site && (
<div className="text-xs text-gray-500 truncate">
<span className="font-medium">وبسایت:</span> {promotion.url_site}
</div>
)}
{promotion.url_myket && (
<div className="text-xs text-gray-500 truncate">
<span className="font-medium">مایکت:</span> {promotion.url_myket}
</div>
)}
{promotion.url_bazzar && (
<div className="text-xs text-gray-500 truncate">
<span className="font-medium">بازار:</span> {promotion.url_bazzar}
</div>
)}
{promotion.url_google_play && (
<div className="text-xs text-gray-500 truncate">
<span className="font-medium">گوگلپلی:</span> {promotion.url_google_play}
</div>
)}
</div>
<div className="mt-2 text-xs text-gray-400">
آخرین بهروزرسانی: {formatDate(promotion.updated_at)}
</div>
</div>
</div>
</div>
{/* Action Buttons */}
<div className="mr-4 flex-shrink-0 flex flex-col space-y-2">
<button
onClick={() => onToggleActive(promotion)}
className={`p-2 rounded-full hover:bg-gray-100 ${
promotion.is_active ? 'text-green-600' : 'text-gray-400'
}`}
title={promotion.is_active ? 'غیرفعال کردن' : 'فعال کردن'}
>
{promotion.is_active ? (
<EyeIcon className="h-5 w-5" />
) : (
<EyeSlashIcon className="h-5 w-5" />
)}
</button>
<button
onClick={() => onEdit(promotion)}
className="text-indigo-600 hover:text-indigo-900 p-2 rounded-full hover:bg-indigo-50"
>
<PencilIcon className="h-5 w-5" />
</button>
<button
onClick={() => onDelete(promotion)}
className="text-red-600 hover:text-red-900 p-2 rounded-full hover:bg-red-50"
>
<TrashIcon className="h-5 w-5" />
</button>
</div>
</div>
</li>
))}
</ul>
</div>
);
}