import React, { useState, useEffect, useCallback } from "react"; import { rentalAPI } from "../services/api"; import { Rental } from "../types"; interface DeclineRentalModalProps { show: boolean; onHide: () => void; rental: Rental; onDeclineComplete: (updatedRental: Rental) => void; } const DeclineRentalModal: React.FC = ({ show, onHide, rental, onDeclineComplete, }) => { const [processing, setProcessing] = useState(false); const [error, setError] = useState(null); const [reason, setReason] = useState(""); const [success, setSuccess] = useState(false); const [updatedRental, setUpdatedRental] = useState(null); const handleDecline = async () => { if (!reason.trim()) { setError("Please provide a reason for declining this request"); return; } try { setProcessing(true); setError(null); const response = await rentalAPI.declineRental(rental.id, reason.trim()); // Store updated rental data for later callback setUpdatedRental(response.data); // Show success confirmation setSuccess(true); } catch (error: any) { setError( error.response?.data?.error || "Failed to decline rental request" ); } finally { setProcessing(false); } }; const handleClose = () => { // Call parent callback with updated rental data if we have it if (updatedRental) { onDeclineComplete(updatedRental); // Notify Navbar to update pending count window.dispatchEvent(new CustomEvent("rentalStatusChanged")); } // Reset all states when closing setProcessing(false); setError(null); setReason(""); setSuccess(false); setUpdatedRental(null); onHide(); }; const handleBackdropClick = useCallback( (e: React.MouseEvent) => { if (e.target === e.currentTarget && !processing) { handleClose(); } }, [handleClose, processing] ); const handleKeyDown = useCallback( (e: KeyboardEvent) => { if (e.key === "Escape" && !processing) { handleClose(); } }, [handleClose, processing] ); useEffect(() => { if (show) { document.addEventListener("keydown", handleKeyDown); document.body.style.overflow = "hidden"; } else { document.removeEventListener("keydown", handleKeyDown); document.body.style.overflow = "unset"; } return () => { document.removeEventListener("keydown", handleKeyDown); document.body.style.overflow = "unset"; }; }, [show, handleKeyDown]); if (!show) return null; return (
{success ? "Request Declined" : "Decline Rental Request"}
{success ? (

Request Declined

The renter has been notified that their request was declined.

) : ( <>
Rental Details

Item: {rental.item?.name}

Renter: {rental.renter?.firstName}{" "} {rental.renter?.lastName}

Start:{" "} {new Date(rental.startDateTime).toLocaleString()}

End:{" "} {new Date(rental.endDateTime).toLocaleString()}

{error && (
{error}
)}
{ e.preventDefault(); handleDecline(); }} >