import React, { useEffect, useMemo, useState } from 'react'; import { IonButton, IonButtons, IonContent, IonHeader, IonInput, IonItem, IonLabel, IonModal, IonTextarea, IonTitle, IonToolbar, } from '@ionic/react'; interface HabitFormModalProps { isOpen: boolean; onDismiss: () => void; onSubmit: (values: { name: string; targetDescription: string; color: string; }) => Promise; initialValues?: { name: string; targetDescription: string; color: string; }; } const COLORS = [ '#7c3aed', '#0f766e', '#ea580c', '#dc2626', '#2563eb', '#059669', ]; const HabitFormModal: React.FC = ({ isOpen, onDismiss, onSubmit, initialValues, }) => { const isEditing = !!initialValues; const [name, setName] = useState(initialValues?.name ?? ''); const [targetDescription, setTargetDescription] = useState( initialValues?.targetDescription ?? '' ); const [color, setColor] = useState(initialValues?.color ?? COLORS[0]); const [saving, setSaving] = useState(false); const [error, setError] = useState(''); const canSubmit = useMemo( () => name.trim().length > 0 && name.trim().length <= 80, [name] ); useEffect(() => { if (isOpen) { setName(initialValues?.name ?? ''); setTargetDescription(initialValues?.targetDescription ?? ''); setColor(initialValues?.color ?? COLORS[0]); setError(''); } }, [isOpen, initialValues]); const handleSubmit = async () => { setError(''); setSaving(true); try { await onSubmit({ name: name.trim(), targetDescription: targetDescription.trim(), color, }); setName(''); setTargetDescription(''); setColor(COLORS[0]); onDismiss(); } catch (err) { setError(err instanceof Error ? err.message : 'Unable to save habit.'); } finally { setSaving(false); } }; return ( {isEditing ? 'Edit Habit' : 'New Habit'} Close
setName(e.detail.value ?? '')} /> setTargetDescription(e.detail.value ?? '')} />
Color
{COLORS.map((swatch) => (
{error ?

{error}

: null} {saving ? 'Saving…' : isEditing ? 'Save Changes' : 'Create Habit'}
); }; export default HabitFormModal;