'use client'; import { useState, useEffect } from 'react'; import { Plus, Tag, RefreshCw, Trash, Edit, Check, X } from 'lucide-react'; import { useRouter } from 'next/navigation'; import { Button } from '@/components/ui/button'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@/components/ui/dialog'; import { toast } from '@/components/ui/use-toast'; import { Badge } from '@/components/ui/badge'; import { Promotion } from '@/lib/types/promotion'; import { fetchClient } from '@/lib/api'; import NewPromotionForm from './NewPromotionForm'; import EditPromotionForm from './EditPromotionForm'; export default function PromotionsList() { const router = useRouter(); const [promotions, setPromotions] = useState([]); const [loading, setLoading] = useState(true); const [showNewDialog, setShowNewDialog] = useState(false); const [editingPromotion, setEditingPromotion] = useState(null); const [showDeleteDialog, setShowDeleteDialog] = useState(false); const [promotionToDelete, setPromotionToDelete] = useState(null); // Load promotions on mount useEffect(() => { loadPromotions(); }, []); const loadPromotions = async () => { setLoading(true); try { const response = await fetchClient('/promotions'); setPromotions(response || []); } catch (error) { console.error('Failed to fetch promotions:', error); toast({ title: 'Error', description: 'Failed to load promotions', variant: 'destructive', }); } finally { setLoading(false); } }; const deletePromotion = async (id: string) => { try { await fetchClient(`/promotions/${id}`, { method: 'DELETE', }); toast({ title: 'Success', description: 'Promotion deleted successfully', }); // Refresh the list loadPromotions(); } catch (error) { console.error('Error deleting promotion:', error); toast({ title: 'Error', description: 'Failed to delete promotion', variant: 'destructive', }); } finally { setShowDeleteDialog(false); setPromotionToDelete(null); } }; function handleOpenEditDialog(promotion: Promotion) { setEditingPromotion(promotion); } function handleCloseEditDialog() { setEditingPromotion(null); } function handleCreateComplete() { setShowNewDialog(false); loadPromotions(); } function handleEditComplete() { setEditingPromotion(null); loadPromotions(); } function formatDate(dateString: string | null) { if (!dateString) return 'No end date'; return new Date(dateString).toLocaleDateString(); } return ( <>

Your Promotions

{loading ? (
) : promotions.length === 0 ? (

No promotions yet

Create your first promotion to offer discounts to your customers

) : ( Code Type Value Min. Order Usage Valid Until Status Actions {promotions.map((promotion) => ( {promotion.code} {promotion.discountType === 'percentage' ? 'Percentage' : 'Fixed Amount'} {promotion.discountType === 'percentage' ? `${promotion.discountValue}%` : `£${promotion.discountValue.toFixed(2)}`} {promotion.minOrderAmount > 0 ? `£${promotion.minOrderAmount.toFixed(2)}` : 'None'} {promotion.usageCount} / {promotion.maxUsage || '∞'} {formatDate(promotion.endDate)} {promotion.isActive ? 'Active' : 'Inactive'}
))}
)}
{/* New Promotion Dialog */} Create New Promotion Add a promotional code to offer discounts to your customers.
setShowNewDialog(false)} />
{/* Edit Promotion Dialog */} editingPromotion && handleCloseEditDialog()}> Edit Promotion Modify this promotional code's details.
{editingPromotion && ( )}
{/* Delete Confirmation Dialog */} Confirm Deletion Are you sure you want to delete this promotion? This action cannot be undone. ); }