import React, { useState, useEffect } from 'react'; import { X } from 'lucide-react'; import { toast } from 'react-toastify'; import notificationService from '../../services/api/notificationService'; interface SendNotificationModalProps { onClose: () => void; onSuccess: () => void; initialData?: { user_id?: number; booking_id?: number; payment_id?: number; }; } const SendNotificationModal: React.FC = ({ onClose, onSuccess, initialData }) => { const [formData, setFormData] = useState({ user_id: initialData?.user_id?.toString() || '', notification_type: 'custom', channel: 'email', subject: '', content: '', priority: 'normal', scheduled_at: '', booking_id: initialData?.booking_id?.toString() || '', payment_id: initialData?.payment_id?.toString() || '', }); const [loading, setLoading] = useState(false); const [templates, setTemplates] = useState([]); const [selectedTemplate, setSelectedTemplate] = useState(''); useEffect(() => { loadTemplates(); }, [formData.notification_type, formData.channel]); const loadTemplates = async () => { try { const response = await notificationService.getTemplates({ notification_type: formData.notification_type, channel: formData.channel, }); setTemplates(response.data.data || []); } catch (error) { // Ignore errors } }; const handleTemplateSelect = (templateId: string) => { const template = templates.find(t => t.id.toString() === templateId); if (template) { setFormData({ ...formData, subject: template.subject || '', content: template.content || '', }); setSelectedTemplate(templateId); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!formData.content.trim()) { toast.error('Content is required'); return; } try { setLoading(true); await notificationService.sendNotification({ user_id: formData.user_id ? parseInt(formData.user_id) : undefined, notification_type: formData.notification_type, channel: formData.channel, content: formData.content, subject: formData.subject || undefined, priority: formData.priority, scheduled_at: formData.scheduled_at || undefined, booking_id: formData.booking_id ? parseInt(formData.booking_id) : undefined, payment_id: formData.payment_id ? parseInt(formData.payment_id) : undefined, }); toast.success('Notification sent successfully'); onSuccess(); } catch (error: any) { toast.error(error.message || 'Failed to send notification'); } finally { setLoading(false); } }; return (

Send Notification

{templates.length > 0 && (
)}
setFormData({ ...formData, user_id: e.target.value })} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500" placeholder="Leave empty for system-wide" />
{(formData.channel === 'email' || formData.channel === 'push') && (
setFormData({ ...formData, subject: e.target.value })} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500" />
)}