import React, { useState, useEffect } from "react"; import { Link } from "react-router-dom"; import { useAuth } from "../contexts/AuthContext"; import { rentalAPI } from "../services/api"; import { Rental } from "../types"; import ReviewItemModal from "../components/ReviewModal"; import ConfirmationModal from "../components/ConfirmationModal"; const MyRentals: React.FC = () => { // Helper function to format time const formatTime = (timeString?: string) => { if (!timeString || timeString.trim() === "") return ""; try { const [hour, minute] = timeString.split(":"); const hourNum = parseInt(hour); const hour12 = hourNum === 0 ? 12 : hourNum > 12 ? hourNum - 12 : hourNum; const period = hourNum < 12 ? "AM" : "PM"; return `${hour12}:${minute} ${period}`; } catch (error) { return ""; } }; const { user } = useAuth(); const [rentals, setRentals] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [showReviewModal, setShowReviewModal] = useState(false); const [selectedRental, setSelectedRental] = useState(null); const [showCancelModal, setShowCancelModal] = useState(false); const [rentalToCancel, setRentalToCancel] = useState(null); const [cancelling, setCancelling] = useState(false); useEffect(() => { fetchRentals(); }, []); const fetchRentals = async () => { try { const response = await rentalAPI.getMyRentals(); setRentals(response.data); } catch (err: any) { setError(err.response?.data?.message || "Failed to fetch rentals"); } finally { setLoading(false); } }; const handleCancelClick = (rentalId: string) => { setRentalToCancel(rentalId); setShowCancelModal(true); }; const confirmCancelRental = async () => { if (!rentalToCancel) return; setCancelling(true); try { await rentalAPI.updateRentalStatus(rentalToCancel, "cancelled"); fetchRentals(); setShowCancelModal(false); setRentalToCancel(null); } catch (err: any) { alert("Failed to cancel rental"); } finally { setCancelling(false); } }; const handleReviewClick = (rental: Rental) => { setSelectedRental(rental); setShowReviewModal(true); }; const handleReviewSuccess = () => { fetchRentals(); alert("Thank you for your review!"); }; // Filter rentals - only show active rentals (pending, confirmed, active) const renterActiveRentals = rentals.filter((r) => ["pending", "confirmed", "active"].includes(r.status) ); if (loading) { return (
Loading...
); } if (error) { return (
{error}
); } return (

My Rentals

{renterActiveRentals.length === 0 ? (
No Active Rental Requests

You don't have any rental requests at the moment.

Browse Items to Rent
) : (
{renterActiveRentals.map((rental) => (
) => { const target = e.target as HTMLElement; if (!rental.item || target.closest("button")) { e.preventDefault(); } }} >
{rental.item?.images && rental.item.images[0] && ( {rental.item.name} )}
{rental.item ? rental.item.name : "Item Unavailable"}
{rental.status.charAt(0).toUpperCase() + rental.status.slice(1)}

Rental Period:
Start:{" "} {new Date(rental.startDateTime).toLocaleString()}
End:{" "} {new Date(rental.endDateTime).toLocaleString()}

Total: ${rental.totalAmount}

{rental.owner && (

Owner: {rental.owner.firstName}{" "} {rental.owner.lastName}

)} {rental.renterPrivateMessage && rental.renterReviewVisible && (
Private Note from Owner:
{rental.renterPrivateMessage}
)} {rental.status === "cancelled" && rental.rejectionReason && (
Rejection reason:{" "} {rental.rejectionReason}
)}
{rental.status === "pending" && ( )} {rental.status === "active" && !rental.itemRating && !rental.itemReviewSubmittedAt && ( )} {rental.itemReviewSubmittedAt && !rental.itemReviewVisible && (
Review Submitted
)} {rental.itemReviewVisible && rental.itemRating && (
Review Published ({rental.itemRating}/5)
)} {rental.status === "completed" && rental.rating && !rental.itemRating && (
Reviewed ({rental.rating}/5)
)}
))}
)} {/* Review Modal */} {selectedRental && ( { setShowReviewModal(false); setSelectedRental(null); }} rental={selectedRental} onSuccess={handleReviewSuccess} /> )} { setShowCancelModal(false); setRentalToCancel(null); }} onConfirm={confirmCancelRental} title="Cancel Rental" message="Are you sure you want to cancel this rental? This action cannot be undone." confirmText="Yes, Cancel Rental" cancelText="Keep Rental" confirmButtonClass="btn-danger" loading={cancelling} />
); }; export default MyRentals;