Skip to content

Commit

Permalink
Merge pull request #179 from kubapoke/fix/reloading-offer-page
Browse files Browse the repository at this point in the history
Fix/reloading offer page
  • Loading branch information
kubapoke authored Jan 17, 2025
2 parents 456900e + de3ba90 commit 7de001b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
7 changes: 4 additions & 3 deletions car-search/frontend/car-search/src/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'react-router-dom';
import MainLayout from "../Layouts/MainLayout.tsx";
import HomePage from "../Pages/HomePage.tsx";
import NotFoundPage from "../Pages/NotFoundPage.tsx";
import ErrorPage from "../Pages/ErrorPage.tsx";
import BrowseOffersPage from "../Pages/BrowseOffersPage.tsx";
import OfferPage from "../Pages/OfferPage.tsx";
import NewUserForm from "../Pages/NewUserForm.tsx";
Expand Down Expand Up @@ -44,8 +44,9 @@ function MainRouter() {
<Route path='/new-user-form' element={<NewUserForm/>}/>
<Route path='/offers/:id' element={<OfferPage/>}/>
<Route path='/offers/rent-confirmation' element={<RentConfirmationPage/>}/>
<Route path="/user-rents" element={isLoggedIn ? <UserRentsPage/> : <NotFoundPage/>}/>
<Route path="*" element={<NotFoundPage/>}/>
<Route path="/user-rents" element={isLoggedIn ? <UserRentsPage/> : <ErrorPage/>}/>
<Route path="*" element={<ErrorPage/>}/>
<Route path="/error" element={<ErrorPage/>}/>
<Route path="/new-rent-confirm" element={<RentConfirmedPage/>}/>
</Route>
)
Expand Down
27 changes: 27 additions & 0 deletions car-search/frontend/car-search/src/Pages/ErrorPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Link, useLocation } from 'react-router-dom';
import { FaExclamationTriangle } from "react-icons/fa";

const ErrorPage = () => {
const location = useLocation();
const { message, status } = location.state || {};

return (
<section className="text-center flex flex-col justify-center items-center h-96">
<FaExclamationTriangle className="fas text-yellow-400 text-6xl mb-4" />
<h1 className="text-6xl font-bold mb-4">
Error {status || 404}
</h1>
<p className="text-xl mb-5">
{message || "This page does not exist"}
</p>
<Link
to="/"
className="text-white bg-blue-700 hover:bg-blue-900 rounded-md px-3 py-2 mt-4"
>
Go Back
</Link>
</section>
);
};

export default ErrorPage;
18 changes: 0 additions & 18 deletions car-search/frontend/car-search/src/Pages/NotFoundPage.tsx

This file was deleted.

24 changes: 17 additions & 7 deletions car-search/frontend/car-search/src/Pages/OfferPage.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
import {Link, useNavigate, useParams} from "react-router-dom";
import {FaArrowLeft, FaMapMarker} from "react-icons/fa";
import notFoundPage from "./NotFoundPage.tsx";
import notFoundPage from "./ErrorPage.tsx";
import {useOffers, Offer} from "../Context/OffersContext.tsx";
import {calculateDaysBetweenDates} from "../Components/CarOffer.tsx";
import {useEffect} from "react";

const OfferPage = () => {
const navigate = useNavigate();
const { id } = useParams();
if (!id) {
throw new Response("Offer ID is missing", { status: 400 });
}

// get offers from the context
const { offers } = useOffers();

// find appropriate offer
const offer = offers.find((offer) => offer.offerId === id);
useEffect(() => {
if (!id) {
navigate("/error", { state: { message: "Offer ID is missing", status: 400 } });
}
}, [id, navigate]);

// find and save appropriate offer
const offerData = sessionStorage.getItem("offerData");
let offer: Offer | null = null;

if (offerData) {
offer = JSON.parse(offerData);
}

if (!offer) {
throw new Response("Offer not found", { status: 404 });
offer = offers.find((offer) => offer.offerId === id) || null;
sessionStorage.setItem("offerData", JSON.stringify(offer));
}

const onRentClick = async (offer: Offer) => {
Expand Down

0 comments on commit 7de001b

Please sign in to comment.