'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([]); const [selectedPromotion, setSelectedPromotion] = useState(null); const [isLoading, setIsLoading] = useState(false); const [isFormVisible, setIsFormVisible] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(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 (

مدیریت تبلیغات

{!isFormVisible && ( )}
{/* Messages */} {error && (

{error}

)} {success && (

{success}

)} {/* Form or List */} {isFormVisible ? ( ) : ( )}
); }