feat: add Newsletter Campaigns frontend UI

- Add Campaigns list page with table, status badges, search, actions
- Add Campaign editor with title, subject, content fields
- Add preview modal, test email dialog, send confirmation
- Update Marketing index to show hub with Newsletter, Campaigns, Coupons cards
- Add routes in App.tsx
This commit is contained in:
Dwindi Ramadhana
2025-12-31 18:59:49 +07:00
parent 65dd847a66
commit 3d5191aab3
4 changed files with 791 additions and 36 deletions

View File

@@ -0,0 +1,400 @@
import React, { useState, useEffect } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useNavigate, useParams } from 'react-router-dom';
import { SettingsLayout } from '@/routes/Settings/components/SettingsLayout';
import { SettingsCard } from '@/routes/Settings/components/SettingsCard';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import {
ArrowLeft,
Send,
Eye,
TestTube,
Save,
Loader2,
} from 'lucide-react';
import { toast } from 'sonner';
import { api } from '@/lib/api';
import { __ } from '@/lib/i18n';
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
interface Campaign {
id: number;
title: string;
subject: string;
content: string;
status: string;
scheduled_at: string | null;
}
export default function CampaignEdit() {
const { id } = useParams<{ id: string }>();
const isNew = id === 'new';
const navigate = useNavigate();
const queryClient = useQueryClient();
const [title, setTitle] = useState('');
const [subject, setSubject] = useState('');
const [content, setContent] = useState('');
const [showPreview, setShowPreview] = useState(false);
const [previewHtml, setPreviewHtml] = useState('');
const [showTestDialog, setShowTestDialog] = useState(false);
const [testEmail, setTestEmail] = useState('');
const [showSendConfirm, setShowSendConfirm] = useState(false);
const [isSaving, setIsSaving] = useState(false);
// Fetch campaign if editing
const { data: campaign, isLoading } = useQuery({
queryKey: ['campaign', id],
queryFn: async () => {
const response = await api.get(`/campaigns/${id}`);
return response.data as Campaign;
},
enabled: !isNew && !!id,
});
// Populate form when campaign loads
useEffect(() => {
if (campaign) {
setTitle(campaign.title || '');
setSubject(campaign.subject || '');
setContent(campaign.content || '');
}
}, [campaign]);
// Save mutation
const saveMutation = useMutation({
mutationFn: async (data: { title: string; subject: string; content: string; status?: string }) => {
if (isNew) {
return api.post('/campaigns', data);
} else {
return api.put(`/campaigns/${id}`, data);
}
},
onSuccess: (response) => {
queryClient.invalidateQueries({ queryKey: ['campaigns'] });
toast.success(isNew ? __('Campaign created') : __('Campaign saved'));
if (isNew && response?.data?.id) {
navigate(`/marketing/campaigns/${response.data.id}`, { replace: true });
}
},
onError: () => {
toast.error(__('Failed to save campaign'));
},
});
// Preview mutation
const previewMutation = useMutation({
mutationFn: async () => {
// First save, then preview
let campaignId = id;
if (isNew || !id) {
const saveResponse = await api.post('/campaigns', { title, subject, content, status: 'draft' });
campaignId = saveResponse?.data?.id;
if (campaignId) {
navigate(`/marketing/campaigns/${campaignId}`, { replace: true });
}
} else {
await api.put(`/campaigns/${id}`, { title, subject, content });
}
const response = await api.get(`/campaigns/${campaignId}/preview`);
return response;
},
onSuccess: (response) => {
setPreviewHtml(response?.html || response?.data?.html || '');
setShowPreview(true);
},
onError: () => {
toast.error(__('Failed to generate preview'));
},
});
// Test email mutation
const testMutation = useMutation({
mutationFn: async (email: string) => {
// First save
if (!isNew && id) {
await api.put(`/campaigns/${id}`, { title, subject, content });
}
return api.post(`/campaigns/${id}/test`, { email });
},
onSuccess: () => {
toast.success(__('Test email sent'));
setShowTestDialog(false);
},
onError: () => {
toast.error(__('Failed to send test email'));
},
});
// Send mutation
const sendMutation = useMutation({
mutationFn: async () => {
// First save
await api.put(`/campaigns/${id}`, { title, subject, content });
return api.post(`/campaigns/${id}/send`);
},
onSuccess: (response) => {
queryClient.invalidateQueries({ queryKey: ['campaigns'] });
queryClient.invalidateQueries({ queryKey: ['campaign', id] });
toast.success(response?.message || __('Campaign sent successfully'));
setShowSendConfirm(false);
navigate('/marketing/campaigns');
},
onError: (error: any) => {
toast.error(error?.response?.data?.error || __('Failed to send campaign'));
},
});
const handleSave = async () => {
if (!title.trim()) {
toast.error(__('Please enter a title'));
return;
}
setIsSaving(true);
try {
await saveMutation.mutateAsync({ title, subject, content, status: 'draft' });
} finally {
setIsSaving(false);
}
};
const canSend = !isNew && id && campaign?.status !== 'sent' && campaign?.status !== 'sending';
if (!isNew && isLoading) {
return (
<SettingsLayout title={__('Loading...')} description="">
<div className="flex items-center justify-center py-12">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
</SettingsLayout>
);
}
return (
<SettingsLayout
title={isNew ? __('New Campaign') : __('Edit Campaign')}
description={isNew ? __('Create a new email campaign') : campaign?.title || ''}
>
{/* Back button */}
<div className="mb-6">
<Button variant="ghost" onClick={() => navigate('/marketing/campaigns')}>
<ArrowLeft className="mr-2 h-4 w-4" />
{__('Back to Campaigns')}
</Button>
</div>
<div className="space-y-6">
{/* Campaign Details */}
<SettingsCard
title={__('Campaign Details')}
description={__('Basic information about your campaign')}
>
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="title">{__('Campaign Title')}</Label>
<Input
id="title"
placeholder={__('e.g., Holiday Sale Announcement')}
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<p className="text-xs text-muted-foreground">
{__('Internal name for this campaign (not shown to subscribers)')}
</p>
</div>
<div className="space-y-2">
<Label htmlFor="subject">{__('Email Subject')}</Label>
<Input
id="subject"
placeholder={__('e.g., 🎄 Exclusive Holiday Deals Inside!')}
value={subject}
onChange={(e) => setSubject(e.target.value)}
/>
<p className="text-xs text-muted-foreground">
{__('The subject line subscribers will see in their inbox')}
</p>
</div>
</div>
</SettingsCard>
{/* Campaign Content */}
<SettingsCard
title={__('Campaign Content')}
description={__('Write your newsletter content. The design template is configured in Settings > Notifications.')}
>
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="content">{__('Email Content')}</Label>
<Textarea
id="content"
placeholder={__('Write your newsletter content here...\n\nYou can use:\n- {site_name} - Your store name\n- {current_date} - Today\'s date\n- {subscriber_email} - Subscriber\'s email')}
value={content}
onChange={(e) => setContent(e.target.value)}
className="min-h-[300px] font-mono text-sm"
/>
<p className="text-xs text-muted-foreground">
{__('Use HTML for rich formatting. The design wrapper will be applied from your campaign email template.')}
</p>
</div>
</div>
</SettingsCard>
{/* Actions */}
<div className="flex flex-col sm:flex-row gap-3 sm:justify-between">
<div className="flex gap-3">
<Button
variant="outline"
onClick={() => previewMutation.mutate()}
disabled={previewMutation.isPending || !title.trim()}
>
{previewMutation.isPending ? (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
) : (
<Eye className="mr-2 h-4 w-4" />
)}
{__('Preview')}
</Button>
{!isNew && (
<Button
variant="outline"
onClick={() => setShowTestDialog(true)}
disabled={!id}
>
<TestTube className="mr-2 h-4 w-4" />
{__('Send Test')}
</Button>
)}
</div>
<div className="flex gap-3">
<Button
variant="outline"
onClick={handleSave}
disabled={isSaving || !title.trim()}
>
{isSaving ? (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
) : (
<Save className="mr-2 h-4 w-4" />
)}
{__('Save Draft')}
</Button>
{canSend && (
<Button
onClick={() => setShowSendConfirm(true)}
disabled={sendMutation.isPending}
>
{sendMutation.isPending ? (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
) : (
<Send className="mr-2 h-4 w-4" />
)}
{__('Send Now')}
</Button>
)}
</div>
</div>
</div>
{/* Preview Dialog */}
<Dialog open={showPreview} onOpenChange={setShowPreview}>
<DialogContent className="max-w-4xl max-h-[90vh] overflow-y-auto">
<DialogHeader>
<DialogTitle>{__('Email Preview')}</DialogTitle>
</DialogHeader>
<div className="border rounded-lg bg-white p-4">
<div
className="prose max-w-none"
dangerouslySetInnerHTML={{ __html: previewHtml }}
/>
</div>
</DialogContent>
</Dialog>
{/* Test Email Dialog */}
<Dialog open={showTestDialog} onOpenChange={setShowTestDialog}>
<DialogContent>
<DialogHeader>
<DialogTitle>{__('Send Test Email')}</DialogTitle>
</DialogHeader>
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="test-email">{__('Email Address')}</Label>
<Input
id="test-email"
type="email"
placeholder="your@email.com"
value={testEmail}
onChange={(e) => setTestEmail(e.target.value)}
/>
</div>
<div className="flex justify-end gap-3">
<Button variant="outline" onClick={() => setShowTestDialog(false)}>
{__('Cancel')}
</Button>
<Button
onClick={() => testMutation.mutate(testEmail)}
disabled={!testEmail || testMutation.isPending}
>
{testMutation.isPending ? (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
) : (
<Send className="mr-2 h-4 w-4" />
)}
{__('Send Test')}
</Button>
</div>
</div>
</DialogContent>
</Dialog>
{/* Send Confirmation Dialog */}
<AlertDialog open={showSendConfirm} onOpenChange={setShowSendConfirm}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{__('Send Campaign')}</AlertDialogTitle>
<AlertDialogDescription>
{__('Are you sure you want to send this campaign to all newsletter subscribers? This action cannot be undone.')}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{__('Cancel')}</AlertDialogCancel>
<AlertDialogAction
onClick={() => sendMutation.mutate()}
disabled={sendMutation.isPending}
>
{sendMutation.isPending ? (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
) : (
<Send className="mr-2 h-4 w-4" />
)}
{__('Send to All Subscribers')}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</SettingsLayout>
);
}

View File

@@ -0,0 +1,293 @@
import React, { useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import { SettingsLayout } from '@/routes/Settings/components/SettingsLayout';
import { SettingsCard } from '@/routes/Settings/components/SettingsCard';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import {
Plus,
Search,
Send,
Clock,
CheckCircle2,
AlertCircle,
Trash2,
Edit,
MoreHorizontal,
Copy
} from 'lucide-react';
import { toast } from 'sonner';
import { api } from '@/lib/api';
import { __ } from '@/lib/i18n';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
interface Campaign {
id: number;
title: string;
subject: string;
status: 'draft' | 'scheduled' | 'sending' | 'sent' | 'failed';
recipient_count: number;
sent_count: number;
failed_count: number;
scheduled_at: string | null;
sent_at: string | null;
created_at: string;
}
const statusConfig = {
draft: { label: 'Draft', icon: Edit, className: 'bg-gray-100 text-gray-800 dark:bg-gray-800 dark:text-gray-300' },
scheduled: { label: 'Scheduled', icon: Clock, className: 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300' },
sending: { label: 'Sending', icon: Send, className: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-300' },
sent: { label: 'Sent', icon: CheckCircle2, className: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300' },
failed: { label: 'Failed', icon: AlertCircle, className: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300' },
};
export default function CampaignsList() {
const [searchQuery, setSearchQuery] = useState('');
const [deleteId, setDeleteId] = useState<number | null>(null);
const navigate = useNavigate();
const queryClient = useQueryClient();
const { data, isLoading } = useQuery({
queryKey: ['campaigns'],
queryFn: async () => {
const response = await api.get('/campaigns');
return response.data as Campaign[];
},
});
const deleteMutation = useMutation({
mutationFn: async (id: number) => {
await api.del(`/campaigns/${id}`);
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['campaigns'] });
toast.success(__('Campaign deleted'));
setDeleteId(null);
},
onError: () => {
toast.error(__('Failed to delete campaign'));
},
});
const duplicateMutation = useMutation({
mutationFn: async (campaign: Campaign) => {
const response = await api.post('/campaigns', {
title: `${campaign.title} (Copy)`,
subject: campaign.subject,
content: '', // Would need to fetch full content
status: 'draft',
});
return response;
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['campaigns'] });
toast.success(__('Campaign duplicated'));
},
onError: () => {
toast.error(__('Failed to duplicate campaign'));
},
});
const campaigns = data || [];
const filteredCampaigns = campaigns.filter((c) =>
c.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
c.subject?.toLowerCase().includes(searchQuery.toLowerCase())
);
const formatDate = (dateStr: string | null) => {
if (!dateStr) return '-';
return new Date(dateStr).toLocaleDateString(undefined, {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
});
};
return (
<SettingsLayout
title={__('Campaigns')}
description={__('Create and send email campaigns to your newsletter subscribers')}
>
<SettingsCard
title={__('All Campaigns')}
description={`${campaigns.length} ${__('campaigns total')}`}
>
<div className="space-y-4">
{/* Actions Bar */}
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
<div className="relative flex-1 max-w-sm">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground pointer-events-none" />
<Input
placeholder={__('Search campaigns...')}
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="!pl-9"
/>
</div>
<Button onClick={() => navigate('/marketing/campaigns/new')}>
<Plus className="mr-2 h-4 w-4" />
{__('New Campaign')}
</Button>
</div>
{/* Campaigns Table */}
{isLoading ? (
<div className="text-center py-8 text-muted-foreground">
{__('Loading campaigns...')}
</div>
) : filteredCampaigns.length === 0 ? (
<div className="text-center py-12 text-muted-foreground">
{searchQuery ? __('No campaigns found matching your search') : (
<div className="space-y-4">
<Send className="h-12 w-12 mx-auto opacity-50" />
<p>{__('No campaigns yet')}</p>
<Button onClick={() => navigate('/marketing/campaigns/new')}>
<Plus className="mr-2 h-4 w-4" />
{__('Create your first campaign')}
</Button>
</div>
)}
</div>
) : (
<div className="border rounded-lg">
<Table>
<TableHeader>
<TableRow>
<TableHead>{__('Title')}</TableHead>
<TableHead>{__('Status')}</TableHead>
<TableHead className="hidden md:table-cell">{__('Recipients')}</TableHead>
<TableHead className="hidden md:table-cell">{__('Date')}</TableHead>
<TableHead className="text-right">{__('Actions')}</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{filteredCampaigns.map((campaign) => {
const status = statusConfig[campaign.status] || statusConfig.draft;
const StatusIcon = status.icon;
return (
<TableRow key={campaign.id}>
<TableCell>
<div>
<div className="font-medium">{campaign.title}</div>
{campaign.subject && (
<div className="text-sm text-muted-foreground truncate max-w-[200px]">
{campaign.subject}
</div>
)}
</div>
</TableCell>
<TableCell>
<span className={`inline-flex items-center gap-1 px-2 py-1 rounded-full text-xs font-medium ${status.className}`}>
<StatusIcon className="h-3 w-3" />
{__(status.label)}
</span>
</TableCell>
<TableCell className="hidden md:table-cell">
{campaign.status === 'sent' ? (
<span>
{campaign.sent_count}/{campaign.recipient_count}
{campaign.failed_count > 0 && (
<span className="text-red-500 ml-1">
({campaign.failed_count} failed)
</span>
)}
</span>
) : (
'-'
)}
</TableCell>
<TableCell className="hidden md:table-cell text-muted-foreground">
{campaign.sent_at
? formatDate(campaign.sent_at)
: campaign.scheduled_at
? `Scheduled: ${formatDate(campaign.scheduled_at)}`
: formatDate(campaign.created_at)
}
</TableCell>
<TableCell className="text-right">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="sm">
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={() => navigate(`/marketing/campaigns/${campaign.id}`)}>
<Edit className="mr-2 h-4 w-4" />
{__('Edit')}
</DropdownMenuItem>
<DropdownMenuItem onClick={() => duplicateMutation.mutate(campaign)}>
<Copy className="mr-2 h-4 w-4" />
{__('Duplicate')}
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => setDeleteId(campaign.id)}
className="text-red-600"
>
<Trash2 className="mr-2 h-4 w-4" />
{__('Delete')}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</div>
)}
</div>
</SettingsCard>
{/* Delete Confirmation Dialog */}
<AlertDialog open={deleteId !== null} onOpenChange={() => setDeleteId(null)}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{__('Delete Campaign')}</AlertDialogTitle>
<AlertDialogDescription>
{__('Are you sure you want to delete this campaign? This action cannot be undone.')}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{__('Cancel')}</AlertDialogCancel>
<AlertDialogAction
onClick={() => deleteId && deleteMutation.mutate(deleteId)}
className="bg-red-600 hover:bg-red-700"
>
{__('Delete')}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</SettingsLayout>
);
}

View File

@@ -1,5 +1,63 @@
import { Navigate } from 'react-router-dom';
import { useNavigate } from 'react-router-dom';
import { SettingsLayout } from '@/routes/Settings/components/SettingsLayout';
import { Mail, Send, Tag } from 'lucide-react';
import { __ } from '@/lib/i18n';
interface MarketingCard {
title: string;
description: string;
icon: React.ElementType;
to: string;
}
const cards: MarketingCard[] = [
{
title: __('Newsletter'),
description: __('Manage subscribers and email templates'),
icon: Mail,
to: '/marketing/newsletter',
},
{
title: __('Campaigns'),
description: __('Create and send email campaigns'),
icon: Send,
to: '/marketing/campaigns',
},
{
title: __('Coupons'),
description: __('Discounts, promotions, and coupon codes'),
icon: Tag,
to: '/marketing/coupons',
},
];
export default function Marketing() {
return <Navigate to="/marketing/newsletter" replace />;
const navigate = useNavigate();
return (
<SettingsLayout
title={__('Marketing')}
description={__('Newsletter, campaigns, and promotions')}
>
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{cards.map((card) => (
<button
key={card.to}
onClick={() => navigate(card.to)}
className="flex items-start gap-4 p-6 rounded-lg border bg-card hover:bg-accent transition-colors text-left"
>
<div className="flex-shrink-0 w-10 h-10 rounded-lg bg-primary/10 text-primary flex items-center justify-center">
<card.icon className="w-5 h-5" />
</div>
<div className="flex-1 min-w-0">
<div className="font-medium">{card.title}</div>
<div className="text-sm text-muted-foreground mt-1">
{card.description}
</div>
</div>
</button>
))}
</div>
</SettingsLayout>
);
}