import { useState, useEffect } from 'react'; import { format, parseISO } from 'date-fns'; import type { TimeLog } from '../hooks/useLogs'; interface Props { isOpen: boolean; onClose: () => void; onSave: (data: { date: string; time: string; description: string }) => Promise; editingLog?: TimeLog | null; } function formatTimeForInput(hours: number, mins: number): string { if (mins === 0) { return String(hours); } return `${hours}:${String(mins).padStart(2, '0')}`; } export function AddLogModal({ isOpen, onClose, onSave, editingLog }: Props) { const [date, setDate] = useState(format(new Date(), 'yyyy-MM-dd')); const [time, setTime] = useState('8'); const [description, setDescription] = useState(''); const [error, setError] = useState(''); const [isLoading, setIsLoading] = useState(false); useEffect(() => { if (isOpen) { if (editingLog) { setDate(format(parseISO(editingLog.date), 'yyyy-MM-dd')); setTime(formatTimeForInput(editingLog.hours, editingLog.mins)); setDescription(editingLog.description); } else { setDate(format(new Date(), 'yyyy-MM-dd')); setTime('8'); setDescription(''); } setError(''); } }, [isOpen, editingLog]); if (!isOpen) return null; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setIsLoading(true); try { await onSave({ date, time, description }); onClose(); } catch (err) { setError(err instanceof Error ? err.message : 'Ошибка'); } finally { setIsLoading(false); } }; const setQuickTime = (value: string) => { setTime(value); }; return (

{editingLog ? 'Редактирование записи' : 'Новая запись'}

setDate(e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" required />
setTime(e.target.value)} placeholder="8, 8:30, 8,5" className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" required />

Форматы: 8, 8:30, 8,5, 8.5