import { update } from '@/actions/App/Http/Controllers/Settings/AutomationRuleController'; import { RuleBuilder } from '@/components/automation-rules/rule-builder'; import { CategoryCombobox } from '@/components/shared/category-combobox'; import { LabelCombobox } from '@/components/shared/label-combobox'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { buildJsonLogic, createEmptyGroup, isValidRuleStructure, parseJsonLogic, type RuleStructure, } from '@/lib/rule-builder-utils'; import type { AutomationRule } from '@/types/automation-rule'; import type { Category } from '@/types/category'; import type { Label as LabelType } from '@/types/label'; import { router } from '@inertiajs/react'; import { useEffect, useState } from 'react'; interface EditAutomationRuleDialogProps { rule: AutomationRule; categories: Category[]; labels: LabelType[]; open: boolean; onOpenChange: (open: boolean) => void; onSuccess?: () => void; } export function EditAutomationRuleDialog({ rule, categories, labels, open, onOpenChange, onSuccess, }: EditAutomationRuleDialogProps) { const [title, setTitle] = useState(''); const [priority, setPriority] = useState('0'); const [ruleStructure, setRuleStructure] = useState({ groups: [createEmptyGroup()], groupOperator: 'and', }); const [categoryId, setCategoryId] = useState(''); const [selectedLabelIds, setSelectedLabelIds] = useState([]); const [isSubmitting, setIsSubmitting] = useState(false); const [errors, setErrors] = useState>({}); useEffect(() => { if (rule && open) { setTitle(rule.title); setPriority(String(rule.priority)); setRuleStructure(parseJsonLogic(rule.rules_json)); setCategoryId( rule.action_category_id ? String(rule.action_category_id) : '', ); setSelectedLabelIds(rule.labels?.map((l) => l.id) || []); } }, [rule, open]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setErrors({}); if (!title.trim()) { setErrors((prev) => ({ ...prev, title: 'Title is required' })); return; } if (!isValidRuleStructure(ruleStructure)) { setErrors((prev) => ({ ...prev, rules_json: 'At least one valid condition is required', })); return; } if (!categoryId && selectedLabelIds.length === 0) { setErrors((prev) => ({ ...prev, action_category_id: 'At least one action is required', })); return; } setIsSubmitting(true); try { const jsonLogic = buildJsonLogic(ruleStructure); router.patch( update(rule.id).url, { title: title.trim(), priority: parseInt(priority, 10), rules_json: JSON.stringify(jsonLogic), action_category_id: categoryId || null, action_note: null, action_note_iv: null, action_label_ids: selectedLabelIds.length > 0 ? selectedLabelIds : null, }, { preserveState: true, preserveScroll: true, onSuccess: () => { onOpenChange(false); setErrors({}); onSuccess?.(); }, onError: (errors) => { setErrors(errors as Record); }, onFinish: () => { setIsSubmitting(false); }, }, ); } catch (error) { console.error('Failed to update automation rule:', error); setIsSubmitting(false); } }; return ( Edit Automation Rule Update the rule to automatically categorize transactions and add labels.
setTitle(e.target.value)} placeholder="Rule title" required /> {errors.title && (

{errors.title}

)}
setPriority(e.target.value)} placeholder="0" required />

Lower numbers execute first

{errors.priority && (

{errors.priority}

)}

Actions

At least one action is required

{errors.action_category_id && (

{errors.action_category_id}

)} {(errors['action_label_ids.0'] || errors.action_label_ids) && (

{errors['action_label_ids.0'] || errors.action_label_ids}

)}
); }