import React, { useState, useEffect, useRef } from 'react'; import { CustomReminder } from '../../types'; import { reminderIcons } from '../icons/Icons'; import { MIN_REMINDER_FREQUENCY_MINUTES, MAX_REMINDER_FREQUENCY_MINUTES, validateReminderInputs, } from './reminderValidation'; interface EditReminderModalProps { isOpen: boolean; onClose: () => void; reminder: CustomReminder | null; onUpdate: (reminder: CustomReminder) => Promise; } const EditReminderModal: React.FC = ({ isOpen, onClose, reminder, onUpdate, }) => { const [title, setTitle] = useState(''); const [icon, setIcon] = useState('bell'); const [frequencyMinutes, setFrequencyMinutes] = useState(60); const [startTime, setStartTime] = useState('09:00'); const [endTime, setEndTime] = useState('17:00'); const [isSaving, setIsSaving] = useState(false); const [errors, setErrors] = useState<{ frequency?: string; timeRange?: string; }>({}); const titleInputRef = useRef(null); const validate = () => validateReminderInputs({ frequencyMinutes, startTime, endTime, }); useEffect(() => { if (isOpen && reminder) { setTitle(reminder.title); setIcon(reminder.icon); setFrequencyMinutes(reminder.frequencyMinutes); setStartTime(reminder.startTime); setEndTime(reminder.endTime); setIsSaving(false); setErrors({}); setTimeout(() => titleInputRef.current?.focus(), 100); } }, [isOpen, reminder]); useEffect(() => { if (!isOpen) return; setErrors(validate()); }, [isOpen, frequencyMinutes, startTime, endTime]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!title || !reminder || isSaving) return; const validation = validate(); setErrors(validation); if (Object.keys(validation).length > 0) { return; } setIsSaving(true); try { await onUpdate({ ...reminder, title, icon, frequencyMinutes, startTime, endTime, }); } catch (error) { console.error('Failed to update reminder', error); alert('There was an error updating your reminder. Please try again.'); } finally { setIsSaving(false); } }; if (!isOpen) return null; return (

Edit Reminder

setTitle(e.target.value)} required ref={titleInputRef} className='mt-1 block w-full px-3 py-2 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-white dark:bg-slate-700 dark:border-slate-600 dark:placeholder-slate-400 dark:text-white' />
{Object.entries(reminderIcons).map(([key, IconComponent]) => ( ))}
{ const value = parseInt(e.target.value, 10); setFrequencyMinutes(Number.isNaN(value) ? 0 : value); }} min={MIN_REMINDER_FREQUENCY_MINUTES} max={MAX_REMINDER_FREQUENCY_MINUTES} aria-invalid={Boolean(errors.frequency)} aria-describedby='rem-edit-frequency-help' className='mt-1 block w-full px-3 py-2 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-white dark:bg-slate-700 dark:border-slate-600' />

{errors.frequency ? errors.frequency : `Minimum ${MIN_REMINDER_FREQUENCY_MINUTES} minutes, maximum ${MAX_REMINDER_FREQUENCY_MINUTES} minutes.`}

setStartTime(e.target.value)} required aria-invalid={Boolean(errors.timeRange)} aria-describedby={ errors.timeRange ? 'rem-edit-time-help' : undefined } className='mt-1 block w-full px-3 py-2 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-white dark:bg-slate-700 dark:border-slate-600' />
setEndTime(e.target.value)} required aria-invalid={Boolean(errors.timeRange)} aria-describedby={ errors.timeRange ? 'rem-edit-time-help' : undefined } className='mt-1 block w-full px-3 py-2 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm bg-white dark:bg-slate-700 dark:border-slate-600' />
{errors.timeRange && (

{errors.timeRange}

)}
); }; export default EditReminderModal;