import { store } 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, DialogTrigger, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label as FormLabel } from '@/components/ui/label'; import { buildJsonLogic, createEmptyGroup, isValidRuleStructure, type RuleStructure, } from '@/lib/rule-builder-utils'; import type { Category } from '@/types/category'; import type { Label } from '@/types/label'; import { __ } from '@/utils/i18n'; import { router } from '@inertiajs/react'; import { useState } from 'react'; interface CreateAutomationRuleDialogProps { categories: Category[]; labels: Label[]; disabled?: boolean; onSuccess?: () => void; } export function CreateAutomationRuleDialog({ categories, labels, disabled = false, onSuccess, }: CreateAutomationRuleDialogProps) { const [open, setOpen] = useState(false); const [title, setTitle] = useState(''); const [priority, setPriority] = useState('10'); const [ruleStructure, setRuleStructure] = useState({ groups: [createEmptyGroup()], groupOperator: 'or', }); const [categoryId, setCategoryId] = useState(''); const [selectedLabelIds, setSelectedLabelIds] = useState([]); const [isSubmitting, setIsSubmitting] = useState(false); const [errors, setErrors] = useState>({}); 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.post( store().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: () => { setOpen(false); setTitle(''); setPriority('10'); setRuleStructure({ groups: [createEmptyGroup()], groupOperator: 'and', }); setCategoryId(''); setSelectedLabelIds([]); setErrors({}); onSuccess?.(); }, onError: (errors) => { setErrors(errors as Record); }, onFinish: () => { setIsSubmitting(false); }, }, ); } catch (error) { console.error('Failed to create automation rule:', error); setIsSubmitting(false); } }; return ( {__('Create Automation Rule')} {__( 'Create a rule to automatically categorize transactions\n and add labels.', )}
{__('Title')} setTitle(e.target.value)} placeholder={__('Rule title')} required /> {errors.title && (

{errors.title}

)}
{__('Priority')} setPriority(e.target.value)} placeholder="10" required />

{__('Lower numbers execute first')}

{errors.priority && (

{errors.priority}

)}

{__('Actions')}

{__('At least one action is required')}

{__('Set Category')}
{__('Add Labels')}
{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}

)}
); }