import React, { useState, useEffect } from 'react'; import { useParams } from 'react-router-dom'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { CheckCircle, XCircle, Calendar, MapPin, Clock } from 'lucide-react'; interface ApprovalDetails { requestId: string; title: string; requestType: string; requestDate: string; requestTime: string; location: string; description: string; status: string; responses: Array<{ recipient: string; response: string; respondedAt: string; }>; } export function ApprovalResponse() { const { id } = useParams<{ id: string }>(); const [approval, setApproval] = useState(null); const [loading, setLoading] = useState(true); const [responding, setResponding] = useState(false); const [recipient, setRecipient] = useState(''); useEffect(() => { fetchApproval(); }, [id]); const fetchApproval = async () => { try { const response = await fetch(`/api/v1/approvals/${id}`); if (response.ok) { const data = await response.json(); setApproval(data); } } catch (error) { console.error('Error fetching approval:', error); } finally { setLoading(false); } }; const handleResponse = async (response: 'approved' | 'denied') => { if (!recipient) { alert('Please enter your email or phone number'); return; } setResponding(true); try { const res = await fetch(`/api/v1/approvals/${id}/respond`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ response, recipient, ipAddress: window.location.hostname, userAgent: navigator.userAgent, }), }); if (res.ok) { alert(`Your ${response} response has been recorded!`); fetchApproval(); } else { const error = await res.json(); alert(error.error || 'Failed to record response'); } } catch (error) { console.error('Error:', error); alert('Error recording response'); } finally { setResponding(false); } }; if (loading) { return
Loading...
; } if (!approval) { return
Approval request not found
; } const hasResponded = approval.responses.some(r => r.recipient === recipient); return (
{approval.title} Request ID: {approval.requestId} | Status: {approval.status}
{approval.requestDate}
{approval.requestTime}
{approval.location && (
{approval.location}
)} {approval.description && (

{approval.description}

)}

Responses

{approval.responses.map((resp, idx) => (
{resp.recipient} {resp.response}
))}
{!hasResponded && (
setRecipient(e.target.value)} placeholder="Enter your email or phone number" />
)} {hasResponded && (
✓ You have already responded to this request
)}
); }