simplified create item. Restructured profile. Simplified availability

This commit is contained in:
jackiettran
2025-08-19 17:28:22 -04:00
parent 99eae4774e
commit 66dc187295
11 changed files with 1317 additions and 872 deletions

View File

@@ -1,17 +1,17 @@
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 ReviewModal from '../components/ReviewModal';
import ConfirmationModal from '../components/ConfirmationModal';
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 ReviewModal from "../components/ReviewModal";
import ConfirmationModal from "../components/ConfirmationModal";
const MyRentals: React.FC = () => {
const { user } = useAuth();
const [rentals, setRentals] = useState<Rental[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [activeTab, setActiveTab] = useState<'active' | 'past'>('active');
const [activeTab, setActiveTab] = useState<"active" | "past">("active");
const [showReviewModal, setShowReviewModal] = useState(false);
const [selectedRental, setSelectedRental] = useState<Rental | null>(null);
const [showCancelModal, setShowCancelModal] = useState(false);
@@ -27,7 +27,7 @@ const MyRentals: React.FC = () => {
const response = await rentalAPI.getMyRentals();
setRentals(response.data);
} catch (err: any) {
setError(err.response?.data?.message || 'Failed to fetch rentals');
setError(err.response?.data?.message || "Failed to fetch rentals");
} finally {
setLoading(false);
}
@@ -40,15 +40,15 @@ const MyRentals: React.FC = () => {
const confirmCancelRental = async () => {
if (!rentalToCancel) return;
setCancelling(true);
try {
await rentalAPI.updateRentalStatus(rentalToCancel, 'cancelled');
await rentalAPI.updateRentalStatus(rentalToCancel, "cancelled");
fetchRentals(); // Refresh the list
setShowCancelModal(false);
setRentalToCancel(null);
} catch (err: any) {
alert('Failed to cancel rental');
alert("Failed to cancel rental");
} finally {
setCancelling(false);
}
@@ -61,18 +61,18 @@ const MyRentals: React.FC = () => {
const handleReviewSuccess = () => {
fetchRentals(); // Refresh to show the review has been added
alert('Thank you for your review!');
alert("Thank you for your review!");
};
// Filter rentals based on status
const activeRentals = rentals.filter(r =>
['pending', 'confirmed', 'active'].includes(r.status)
const activeRentals = rentals.filter((r) =>
["pending", "confirmed", "active"].includes(r.status)
);
const pastRentals = rentals.filter(r =>
['completed', 'cancelled'].includes(r.status)
const pastRentals = rentals.filter((r) =>
["completed", "cancelled"].includes(r.status)
);
const displayedRentals = activeTab === 'active' ? activeRentals : pastRentals;
const displayedRentals = activeTab === "active" ? activeRentals : pastRentals;
if (loading) {
return (
@@ -99,20 +99,20 @@ const MyRentals: React.FC = () => {
return (
<div className="container mt-4">
<h1>My Rentals</h1>
<ul className="nav nav-tabs mb-4">
<li className="nav-item">
<button
className={`nav-link ${activeTab === 'active' ? 'active' : ''}`}
onClick={() => setActiveTab('active')}
className={`nav-link ${activeTab === "active" ? "active" : ""}`}
onClick={() => setActiveTab("active")}
>
Active Rentals ({activeRentals.length})
</button>
</li>
<li className="nav-item">
<button
className={`nav-link ${activeTab === 'past' ? 'active' : ''}`}
onClick={() => setActiveTab('past')}
className={`nav-link ${activeTab === "past" ? "active" : ""}`}
onClick={() => setActiveTab("past")}
>
Past Rentals ({pastRentals.length})
</button>
@@ -122,8 +122,8 @@ const MyRentals: React.FC = () => {
{displayedRentals.length === 0 ? (
<div className="text-center py-5">
<p className="text-muted">
{activeTab === 'active'
? "You don't have any active rentals."
{activeTab === "active"
? "You don't have any active rentals."
: "You don't have any past rentals."}
</p>
<Link to="/items" className="btn btn-primary mt-3">
@@ -134,98 +134,116 @@ const MyRentals: React.FC = () => {
<div className="row">
{displayedRentals.map((rental) => (
<div key={rental.id} className="col-md-6 col-lg-4 mb-4">
<Link
to={rental.item ? `/items/${rental.item.id}` : '#'}
<Link
to={rental.item ? `/items/${rental.item.id}` : "#"}
className="text-decoration-none"
onClick={(e: React.MouseEvent<HTMLAnchorElement>) => {
const target = e.target as HTMLElement;
if (!rental.item || target.closest('button')) {
if (!rental.item || target.closest("button")) {
e.preventDefault();
}
}}
>
<div className="card h-100" style={{ cursor: rental.item ? 'pointer' : 'default' }}>
<div
className="card h-100"
style={{ cursor: rental.item ? "pointer" : "default" }}
>
{rental.item?.images && rental.item.images[0] && (
<img
src={rental.item.images[0]}
className="card-img-top"
alt={rental.item.name}
style={{ height: '200px', objectFit: 'cover' }}
style={{ height: "200px", objectFit: "cover" }}
/>
)}
<div className="card-body">
<h5 className="card-title text-dark">
{rental.item ? rental.item.name : 'Item Unavailable'}
{rental.item ? rental.item.name : "Item Unavailable"}
</h5>
<div className="mb-2">
<span className={`badge ${
rental.status === 'active' ? 'bg-success' :
rental.status === 'pending' ? 'bg-warning' :
rental.status === 'confirmed' ? 'bg-info' :
rental.status === 'completed' ? 'bg-secondary' :
'bg-danger'
}`}>
{rental.status.charAt(0).toUpperCase() + rental.status.slice(1)}
</span>
{rental.paymentStatus === 'paid' && (
<span className="badge bg-success ms-2">Paid</span>
)}
</div>
<p className="mb-1 text-dark">
<strong>Rental Period:</strong><br />
{new Date(rental.startDate).toLocaleDateString()} - {new Date(rental.endDate).toLocaleDateString()}
</p>
<p className="mb-1 text-dark">
<strong>Total:</strong> ${rental.totalAmount}
</p>
<p className="mb-1 text-dark">
<strong>Delivery:</strong> {rental.deliveryMethod === 'pickup' ? 'Pick-up' : 'Delivery'}
</p>
{rental.owner && (
<p className="mb-1 text-dark">
<strong>Owner:</strong> {rental.owner.firstName} {rental.owner.lastName}
</p>
)}
{rental.status === 'cancelled' && rental.rejectionReason && (
<div className="alert alert-warning mt-2 mb-1 p-2 small">
<strong>Rejection reason:</strong> {rental.rejectionReason}
<div className="mb-2">
<span
className={`badge ${
rental.status === "active"
? "bg-success"
: rental.status === "pending"
? "bg-warning"
: rental.status === "confirmed"
? "bg-info"
: rental.status === "completed"
? "bg-secondary"
: "bg-danger"
}`}
>
{rental.status.charAt(0).toUpperCase() +
rental.status.slice(1)}
</span>
{rental.paymentStatus === "paid" && (
<span className="badge bg-success ms-2">Paid</span>
)}
</div>
)}
<div className="d-flex gap-2 mt-3">
{rental.status === 'pending' && (
<button
className="btn btn-sm btn-danger"
onClick={() => handleCancelClick(rental.id)}
>
Cancel
</button>
)}
{rental.status === 'completed' && !rental.rating && (
<button
className="btn btn-sm btn-primary"
onClick={() => handleReviewClick(rental)}
>
Leave Review
</button>
)}
{rental.status === 'completed' && rental.rating && (
<div className="text-success small">
<i className="bi bi-check-circle-fill me-1"></i>
Reviewed ({rental.rating}/5)
</div>
<p className="mb-1 text-dark">
<strong>Rental Period:</strong>
<br />
{new Date(rental.startDate).toLocaleDateString()} -{" "}
{new Date(rental.endDate).toLocaleDateString()}
</p>
<p className="mb-1 text-dark">
<strong>Total:</strong> ${rental.totalAmount}
</p>
<p className="mb-1 text-dark">
<strong>Delivery:</strong>{" "}
{rental.deliveryMethod === "pickup"
? "Pick-up"
: "Delivery"}
</p>
{rental.owner && (
<p className="mb-1 text-dark">
<strong>Owner:</strong> {rental.owner.firstName}{" "}
{rental.owner.lastName}
</p>
)}
{rental.status === "cancelled" &&
rental.rejectionReason && (
<div className="alert alert-warning mt-2 mb-1 p-2 small">
<strong>Rejection reason:</strong>{" "}
{rental.rejectionReason}
</div>
)}
<div className="d-flex gap-2 mt-3">
{rental.status === "pending" && (
<button
className="btn btn-sm btn-danger"
onClick={() => handleCancelClick(rental.id)}
>
Cancel
</button>
)}
{rental.status === "completed" && !rental.rating && (
<button
className="btn btn-sm btn-primary"
onClick={() => handleReviewClick(rental)}
>
Leave Review
</button>
)}
{rental.status === "completed" && rental.rating && (
<div className="text-success small">
<i className="bi bi-check-circle-fill me-1"></i>
Reviewed ({rental.rating}/5)
</div>
)}
</div>
</div>
</div>
</div>
</Link>
</div>
</Link>
</div>
))}
</div>
)}
@@ -260,4 +278,4 @@ const MyRentals: React.FC = () => {
);
};
export default MyRentals;
export default MyRentals;