import React, { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { CheckCircle, XCircle, Loader2, AlertCircle } from 'lucide-react'; import { ASSET_REQUEST_CONFIG } from '@/types/asset-request'; interface ActionDialogProps { isOpen: boolean; onClose: () => void; onConfirm: (comments: string) => Promise; title: string; description: string; actionType: 'approve' | 'reject'; requestId: number; employeeName: string; } const ActionDialog: React.FC = ({ isOpen, onClose, onConfirm, title, description, actionType, requestId, employeeName, }) => { const [comments, setComments] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(''); // Reset state when dialog opens useEffect(() => { if (isOpen) { setComments(''); setError(''); setIsSubmitting(false); } }, [isOpen]); // Handle escape key and body scroll useEffect(() => { if (!isOpen) return; const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape' && !isSubmitting) { onClose(); } }; document.addEventListener('keydown', handleEscape); document.body.style.overflow = 'hidden'; return () => { document.removeEventListener('keydown', handleEscape); document.body.style.overflow = 'unset'; }; }, [isOpen, isSubmitting, onClose]); const handleSubmit = async () => { setError(''); if (!comments.trim() || comments.trim().length < 5) { setError('Please provide at least 5 characters of comments'); return; } setIsSubmitting(true); try { await onConfirm(comments.trim()); onClose(); } catch (err) { setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setIsSubmitting(false); } }; const handleOverlayClick = (e: React.MouseEvent) => { if (!isSubmitting && e.target === e.currentTarget) { onClose(); } }; if (!isOpen) return null; return (
{/* Overlay */}