import { useState } from "react"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Plus, Pencil, Trash2, Loader2, Bell } from "lucide-react"; import { useNotificationRules, useCreateNotificationRule, useUpdateNotificationRule, useDeleteNotificationRule } from "@/hooks/queries"; import { useToast } from "@/hooks/use-toast"; import type { NotificationChannel, ReminderFrequency, NotificationRule, NotificationRuleCreateRequest } from "@/types/notification"; export default function NotificationRules() { const { toast } = useToast(); const { data: rules = [], isLoading } = useNotificationRules(); const createRule = useCreateNotificationRule(); const updateRule = useUpdateNotificationRule(); const deleteRule = useDeleteNotificationRule(); const [showForm, setShowForm] = useState(false); const [editingRule, setEditingRule] = useState(null); const [name, setName] = useState(""); const [eventType, setEventType] = useState(""); const [daysBefore, setDaysBefore] = useState(1); const [frequency, setFrequency] = useState("once"); const [channel, setChannel] = useState("in_app"); const resetForm = () => { setName(""); setEventType(""); setDaysBefore(1); setFrequency("once"); setChannel("in_app"); setEditingRule(null); }; const openEdit = (rule: NotificationRule) => { setEditingRule(rule); setName(rule.name); setEventType(rule.event_type); setDaysBefore(rule.days_before); setFrequency(rule.frequency); setChannel(rule.channel); setShowForm(true); }; const handleSave = () => { const data = { name, event_type: eventType, days_before: daysBefore, frequency, channel }; if (editingRule) { updateRule.mutate( { id: editingRule.id, data }, { onSuccess: () => { toast({ title: "Rule Updated" }); setShowForm(false); resetForm(); }, onError: () => toast({ title: "Error", description: "Failed to update rule", variant: "destructive" }), }, ); } else { createRule.mutate(data, { onSuccess: () => { toast({ title: "Rule Created" }); setShowForm(false); resetForm(); }, onError: () => toast({ title: "Error", description: "Failed to create rule", variant: "destructive" }), }); } }; const handleDelete = (id: number) => { if (!window.confirm("Delete this notification rule?")) return; deleteRule.mutate(id, { onSuccess: () => toast({ title: "Rule Deleted" }), onError: () => toast({ title: "Error", description: "Failed to delete rule", variant: "destructive" }), }); }; const handleToggleActive = (rule: NotificationRule) => { updateRule.mutate( { id: rule.id, data: { active: !rule.active } as Partial & { active?: boolean } }, { onSuccess: () => toast({ title: rule.active ? "Rule Deactivated" : "Rule Activated" }), onError: () => toast({ title: "Error", description: "Failed to toggle rule", variant: "destructive" }), }, ); }; if (isLoading) return
; return (

Notification Rules

Configure automated notification rules and reminders.

{ setShowForm(open); if (!open) resetForm(); }}> {editingRule ? "Edit Rule" : "Create Notification Rule"}
setName(e.target.value)} />
setDaysBefore(Number(e.target.value))} />
Name Event Days Before Frequency Channel Active Actions {rules.map(rule => ( {rule.name} {rule.event_type} {rule.days_before} {rule.frequency} {rule.channel} handleToggleActive(rule)} />
))} {rules.length === 0 && (

No notification rules configured.

)}
); }