feat: promotion feature added
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
import { useAuth } from '@/contexts/AuthContext';
|
import { useAuth } from '@/contexts/AuthContext';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { UsersIcon, CubeIcon, TagIcon , BellIcon } from '@heroicons/react/24/outline';
|
import { UsersIcon, CubeIcon, TagIcon , BellIcon } from '@heroicons/react/24/outline';
|
||||||
|
import { MegaphoneIcon } from 'lucide-react';
|
||||||
// import { BellIcon } from 'lucide-react';
|
// import { BellIcon } from 'lucide-react';
|
||||||
|
|
||||||
export default function Dashboard() {
|
export default function Dashboard() {
|
||||||
@@ -67,6 +68,15 @@ export default function Dashboard() {
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</Link>
|
</Link>
|
||||||
|
<Link href="/admin/promotions" className="block">
|
||||||
|
<div className="bg-pink-50 hover:bg-pink-100 rounded-lg p-6 transition-colors">
|
||||||
|
<MegaphoneIcon className="h-8 w-8 text-pink-600 mb-3" />
|
||||||
|
<h3 className="text-lg font-medium text-gray-900">مدیریت تبلیغات</h3>
|
||||||
|
<p className="text-sm text-gray-600 mt-1">
|
||||||
|
ایجاد و مدیریت تبلیغات، بنرها و اسلایدرها
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,174 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useEffect, useCallback, useRef } from 'react';
|
||||||
|
import { useAuth } from '@/contexts/AuthContext';
|
||||||
|
import { promotionsApi } from '@/lib/api/promotions';
|
||||||
|
import PromotionList from '@/components/admin/promotions/PromotionList';
|
||||||
|
import PromotionForm from '@/components/admin/promotions/PromotionForm';
|
||||||
|
import { Promotion, CreatePromotionData, UpdatePromotionData } from '@/types/promotion';
|
||||||
|
import { PlusIcon } from '@heroicons/react/24/outline';
|
||||||
|
|
||||||
|
export default function PromotionsPage() {
|
||||||
|
const { token } = useAuth();
|
||||||
|
const [promotions, setPromotions] = useState<Promotion[]>([]);
|
||||||
|
const [selectedPromotion, setSelectedPromotion] = useState<Promotion | null>(null);
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [isFormVisible, setIsFormVisible] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [success, setSuccess] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const initialLoadRef = useRef(false);
|
||||||
|
|
||||||
|
// Load promotions on mount
|
||||||
|
useEffect(() => {
|
||||||
|
if (token && !initialLoadRef.current) {
|
||||||
|
initialLoadRef.current = true;
|
||||||
|
loadPromotions();
|
||||||
|
}
|
||||||
|
}, [token]);
|
||||||
|
|
||||||
|
const loadPromotions = async () => {
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
const data = await promotionsApi.getPromotions(token!);
|
||||||
|
setPromotions(data);
|
||||||
|
} catch (err) {
|
||||||
|
setError('خطا در دریافت لیست تبلیغات');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCreate = () => {
|
||||||
|
setSelectedPromotion(null);
|
||||||
|
setIsFormVisible(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleEdit = (promotion: Promotion) => {
|
||||||
|
setSelectedPromotion(promotion);
|
||||||
|
setIsFormVisible(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDelete = async (promotion: Promotion) => {
|
||||||
|
if (!confirm(`آیا از حذف تبلیغ "${promotion.title || 'بدون عنوان'}" اطمینان دارید؟`)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
setSuccess(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await promotionsApi.deletePromotion(promotion.id, token!);
|
||||||
|
setSuccess('تبلیغ با موفقیت حذف شد');
|
||||||
|
await loadPromotions();
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : 'خطا در حذف تبلیغ');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleToggleActive = async (promotion: Promotion) => {
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
setSuccess(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await promotionsApi.toggleActive(promotion.id, !promotion.is_active, token!);
|
||||||
|
setSuccess(`تبلیغ با موفقیت ${!promotion.is_active ? 'فعال' : 'غیرفعال'} شد`);
|
||||||
|
await loadPromotions();
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : 'خطا در تغییر وضعیت تبلیغ');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = async (data: CreatePromotionData | UpdatePromotionData) => {
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
setSuccess(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (selectedPromotion) {
|
||||||
|
// Update existing promotion
|
||||||
|
await promotionsApi.updatePromotion(selectedPromotion.id, data, token!);
|
||||||
|
setSuccess('تبلیغ با موفقیت بهروزرسانی شد');
|
||||||
|
} else {
|
||||||
|
// Create new promotion
|
||||||
|
await promotionsApi.createPromotion(data as CreatePromotionData, token!);
|
||||||
|
setSuccess('تبلیغ با موفقیت ایجاد شد');
|
||||||
|
}
|
||||||
|
|
||||||
|
await loadPromotions();
|
||||||
|
setIsFormVisible(false);
|
||||||
|
setSelectedPromotion(null);
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : 'خطا در ذخیره تبلیغ');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCancel = () => {
|
||||||
|
setIsFormVisible(false);
|
||||||
|
setSelectedPromotion(null);
|
||||||
|
setError(null);
|
||||||
|
setSuccess(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="p-6">
|
||||||
|
<div className="max-w-7xl mx-auto">
|
||||||
|
<div className="flex justify-between items-center mb-6">
|
||||||
|
<h1 className="text-2xl font-bold text-gray-900">
|
||||||
|
مدیریت تبلیغات
|
||||||
|
</h1>
|
||||||
|
{!isFormVisible && (
|
||||||
|
<button
|
||||||
|
onClick={handleCreate}
|
||||||
|
disabled={isLoading}
|
||||||
|
className="inline-flex items-center 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"
|
||||||
|
>
|
||||||
|
<PlusIcon className="h-5 w-5 ml-2" />
|
||||||
|
تبلیغ جدید
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Messages */}
|
||||||
|
{error && (
|
||||||
|
<div className="mb-4 rounded-md bg-red-50 p-4">
|
||||||
|
<p className="text-sm text-red-800">{error}</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{success && (
|
||||||
|
<div className="mb-4 rounded-md bg-green-50 p-4">
|
||||||
|
<p className="text-sm text-green-800">{success}</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Form or List */}
|
||||||
|
{isFormVisible ? (
|
||||||
|
<PromotionForm
|
||||||
|
promotion={selectedPromotion}
|
||||||
|
onSubmit={handleSubmit}
|
||||||
|
onCancel={handleCancel}
|
||||||
|
isLoading={isLoading}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<PromotionList
|
||||||
|
promotions={promotions}
|
||||||
|
onEdit={handleEdit}
|
||||||
|
onDelete={handleDelete}
|
||||||
|
onToggleActive={handleToggleActive}
|
||||||
|
isLoading={isLoading}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -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>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import { apiClient } from './client';
|
||||||
|
import { Promotion, CreatePromotionData, UpdatePromotionData, ApiResponse } from '@/types/promotion';
|
||||||
|
|
||||||
|
export const promotionsApi = {
|
||||||
|
// Get all promotions
|
||||||
|
getPromotions: async (token: string): Promise<Promotion[]> => {
|
||||||
|
return apiClient.get<Promotion[]>('/promotions', token);
|
||||||
|
},
|
||||||
|
|
||||||
|
// Get single promotion
|
||||||
|
getPromotion: async (id: number, token: string): Promise<Promotion> => {
|
||||||
|
return apiClient.get<Promotion>(`/promotions/${id}`, token);
|
||||||
|
},
|
||||||
|
|
||||||
|
// Create new promotion
|
||||||
|
createPromotion: async (data: CreatePromotionData, token: string): Promise<ApiResponse<Promotion>> => {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
// Append all fields to FormData
|
||||||
|
Object.entries(data).forEach(([key, value]) => {
|
||||||
|
if (value !== null && value !== undefined) {
|
||||||
|
if (key === 'image' && value instanceof File) {
|
||||||
|
formData.append('image', value);
|
||||||
|
} else {
|
||||||
|
formData.append(key, String(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return apiClient.post<ApiResponse<Promotion>>('/promotions', formData, token);
|
||||||
|
},
|
||||||
|
|
||||||
|
// Update promotion
|
||||||
|
updatePromotion: async (id: number, data: UpdatePromotionData, token: string): Promise<ApiResponse<Promotion>> => {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
// Add method spoofing for Laravel
|
||||||
|
// formData.append('_method', 'PUT');
|
||||||
|
|
||||||
|
// Append all fields to FormData
|
||||||
|
Object.entries(data).forEach(([key, value]) => {
|
||||||
|
if (value !== null && value !== undefined) {
|
||||||
|
if (key === 'image' && value instanceof File) {
|
||||||
|
formData.append('image', value);
|
||||||
|
} else if (key === 'is_active') {
|
||||||
|
formData.append(key, value ? '1' : '0');
|
||||||
|
} else {
|
||||||
|
formData.append(key, String(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return apiClient.post<ApiResponse<Promotion>>(`/promotions/${id}`, formData, token);
|
||||||
|
},
|
||||||
|
|
||||||
|
// Delete promotion
|
||||||
|
deletePromotion: async (id: number, token: string): Promise<ApiResponse<any>> => {
|
||||||
|
return apiClient.delete<ApiResponse<any>>(`/promotions/${id}`, undefined, token);
|
||||||
|
},
|
||||||
|
|
||||||
|
// Toggle promotion active status
|
||||||
|
toggleActive: async (id: number, isActive: boolean, token: string): Promise<ApiResponse<Promotion>> => {
|
||||||
|
return apiClient.put<ApiResponse<Promotion>>(`/promotions/${id}`, { is_active: isActive }, token);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
export type PromotionType = 'slider' | 'action' | 'paid_app' | 'banner';
|
||||||
|
|
||||||
|
export const PROMOTION_TYPES = {
|
||||||
|
slider: 'اسلایدر',
|
||||||
|
action: 'اکشن',
|
||||||
|
paid_app: 'اپلیکیشن پولی',
|
||||||
|
banner: 'بنر'
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export interface Promotion {
|
||||||
|
id: number;
|
||||||
|
type: PromotionType;
|
||||||
|
title: string | null;
|
||||||
|
image_url:string | null;
|
||||||
|
subtitle: string | null;
|
||||||
|
image: string | null;
|
||||||
|
action_text: string | null;
|
||||||
|
url_myket: string | null;
|
||||||
|
url_bazzar: string | null;
|
||||||
|
url_google_play: string | null;
|
||||||
|
url_site: string | null;
|
||||||
|
is_active: boolean;
|
||||||
|
priority: number | null;
|
||||||
|
start_at: string | null;
|
||||||
|
end_at: string | null;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CreatePromotionData {
|
||||||
|
type: PromotionType;
|
||||||
|
title?: string | null;
|
||||||
|
subtitle?: string | null;
|
||||||
|
image?: File | null;
|
||||||
|
action_text?: string | null;
|
||||||
|
url_myket?: string | null;
|
||||||
|
url_bazzar?: string | null;
|
||||||
|
url_google_play?: string | null;
|
||||||
|
url_site?: string | null;
|
||||||
|
priority?: number | null;
|
||||||
|
start_at?: string | null;
|
||||||
|
end_at?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdatePromotionData {
|
||||||
|
type?: PromotionType;
|
||||||
|
title?: string | null;
|
||||||
|
subtitle?: string | null;
|
||||||
|
image?: File | null;
|
||||||
|
action_text?: string | null;
|
||||||
|
url_myket?: string | null;
|
||||||
|
url_bazzar?: string | null;
|
||||||
|
url_google_play?: string | null;
|
||||||
|
url_site?: string | null;
|
||||||
|
is_active?: boolean;
|
||||||
|
priority?: number | null;
|
||||||
|
start_at?: string | null;
|
||||||
|
end_at?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ApiResponse<T> {
|
||||||
|
data: T;
|
||||||
|
message?: string;
|
||||||
|
status?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getPromotionTypeLabel = (type: PromotionType): string => {
|
||||||
|
return PROMOTION_TYPES[type] || type;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user