feat: promotion feature added
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import { useAuth } from '@/contexts/AuthContext';
|
||||
import Link from 'next/link';
|
||||
import { UsersIcon, CubeIcon, TagIcon , BellIcon } from '@heroicons/react/24/outline';
|
||||
import { MegaphoneIcon } from 'lucide-react';
|
||||
// import { BellIcon } from 'lucide-react';
|
||||
|
||||
export default function Dashboard() {
|
||||
@@ -67,6 +68,15 @@ export default function Dashboard() {
|
||||
</p>
|
||||
</div>
|
||||
</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>
|
||||
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user