import React from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { api } from '@/lib/api'; import { SettingsCard } from '../../components/SettingsCard'; import { Switch } from '@/components/ui/switch'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { RefreshCw, Mail, MessageCircle, Send, Bell } from 'lucide-react'; import { toast } from 'sonner'; import { __ } from '@/lib/i18n'; interface NotificationEvent { id: string; label: string; description: string; category: string; enabled: boolean; channels: { [channelId: string]: { enabled: boolean; recipient: 'admin' | 'customer' | 'both'; }; }; } interface NotificationChannel { id: string; label: string; icon: string; enabled: boolean; builtin?: boolean; addon?: string; } export default function NotificationEvents() { const queryClient = useQueryClient(); // Fetch staff events const { data: eventsData, isLoading: eventsLoading } = useQuery({ queryKey: ['notification-staff-events'], queryFn: () => api.get('/notifications/staff/events'), }); // Fetch channels const { data: channels, isLoading: channelsLoading } = useQuery({ queryKey: ['notification-channels'], queryFn: () => api.get('/notifications/channels'), }); // Update event mutation const updateMutation = useMutation({ mutationFn: async ({ eventId, channelId, enabled, recipient }: any) => { return api.post('/notifications/events/update', { eventId, channelId, enabled, recipient, }); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['notification-staff-events'] }); toast.success(__('Event settings updated')); }, onError: (error: any) => { toast.error(error?.message || __('Failed to update event')); }, }); const getChannelIcon = (channelId: string) => { switch (channelId) { case 'email': return ; case 'whatsapp': return ; case 'telegram': return ; default: return ; } }; const toggleChannel = (eventId: string, channelId: string, currentlyEnabled: boolean) => { updateMutation.mutate({ eventId, channelId, enabled: !currentlyEnabled, recipient: 'admin', // Default recipient }); }; if (eventsLoading || channelsLoading) { return (
); } const orderEvents = eventsData?.orders || []; const productEvents = eventsData?.products || []; const customerEvents = eventsData?.customers || []; return (
{/* Info Card */}

{__( 'Choose which notification channels (Email, WhatsApp, Telegram, etc.) should be used for each event. Enable multiple channels to send notifications through different mediums.' )}

💡 {__('Tip: Email is always available. Install addons to enable WhatsApp, Telegram, SMS, and other channels.')}

{/* Order Events */} {orderEvents.length > 0 && (
{orderEvents.map((event: NotificationEvent) => (

{event.label}

{event.description}

{/* Channel Selection */}
{channels?.map((channel: NotificationChannel) => { const channelEnabled = event.channels?.[channel.id]?.enabled || false; const recipient = event.channels?.[channel.id]?.recipient || 'admin'; return (
{getChannelIcon(channel.id)}
{channel.label} {channel.builtin && ( {__('Built-in')} )}
{channelEnabled && ( {__('Send to')}: {recipient === 'admin' ? __('Admin') : recipient === 'customer' ? __('Customer') : __('Both')} )}
toggleChannel(event.id, channel.id, channelEnabled)} disabled={!channel.enabled || updateMutation.isPending} />
); })}
))}
)} {/* Product Events */} {productEvents.length > 0 && (
{productEvents.map((event: NotificationEvent) => (

{event.label}

{event.description}

{channels?.map((channel: NotificationChannel) => { const channelEnabled = event.channels?.[channel.id]?.enabled || false; const recipient = event.channels?.[channel.id]?.recipient || 'admin'; return (
{getChannelIcon(channel.id)}
{channel.label} {channel.builtin && ( {__('Built-in')} )}
{channelEnabled && ( {__('Send to')}: {recipient === 'admin' ? __('Admin') : recipient === 'customer' ? __('Customer') : __('Both')} )}
toggleChannel(event.id, channel.id, channelEnabled)} disabled={!channel.enabled || updateMutation.isPending} />
); })}
))}
)} {/* Customer Events */} {customerEvents.length > 0 && (
{customerEvents.map((event: NotificationEvent) => (

{event.label}

{event.description}

{channels?.map((channel: NotificationChannel) => { const channelEnabled = event.channels?.[channel.id]?.enabled || false; const recipient = event.channels?.[channel.id]?.recipient || 'admin'; return (
{getChannelIcon(channel.id)}
{channel.label} {channel.builtin && ( {__('Built-in')} )}
{channelEnabled && ( {__('Send to')}: {recipient === 'admin' ? __('Admin') : recipient === 'customer' ? __('Customer') : __('Both')} )}
toggleChannel(event.id, channel.id, channelEnabled)} disabled={!channel.enabled || updateMutation.isPending} />
); })}
))}
)}
); }