Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add redirect to homepage for invalid URLs #381

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// eslint-disable-next-line no-unused-vars
import React from "react";
import { useState, useEffect } from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import { BrowserRouter, Route, Routes, Navigate } from "react-router-dom";
import "./App.css";
import Navbar from "./Components/Navbar.jsx";
import ScrollToTop from "./Components/Scrolltotop.jsx";
Expand Down Expand Up @@ -85,10 +85,9 @@ function App() {
<Route path="/recipe/:id" element={<OneRecipe />} />
<Route path="/contributors" element={<Contributors />} />
<Route path="/recipe-suggestions" element={<RecipeSuggestions />} />
<Route path="*" element={<NotFound />} />
{/* Add Contributors route */}
{/* Redirect all unknown routes to home */}
<Route path="*" element={<Navigate to="/" replace />} />
<Route path="/privacy-policy" element={<PrivacyPolicy />} />
{/* Added Privacy-policy route */}
</Routes>
<Footer />
</BrowserRouter>
Expand Down
24 changes: 20 additions & 4 deletions frontend/src/Pages/NotFound.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import React from "react";
import food from "../assets/Images/food404.png";
import { Link } from "react-router-dom";
import { Link, useNavigate } from "react-router-dom";

const NotFound = () => {
const navigate = useNavigate(); // Hook to access the navigation function

const handleGoBack = () => {
navigate(-1); // Go back to the previous page
};

return (
<div className="h-screen w-screen flex items-stretch">
<div className="container flex flex-col items-center justify-center px-5 text-gray-700">
Expand All @@ -22,6 +28,12 @@ const NotFound = () => {
>
Back To Homepage
</Link>
<button
onClick={handleGoBack}
className="inline-flex items-center hidden md:inline ml-4 bg-gray-300 border-0 py-1 px-3 focus:outline-none hover:bg-gray-200 rounded text-black transition-all"
>
Go Back
</button>
</div>

<div className="md:ml-8">
Expand All @@ -34,16 +46,20 @@ const NotFound = () => {
<p className="mt-8 mb-8">
But don't worry, you can find plenty of other things on our homepage.
</p>
<Link
<Link
to="/"
className="inline-flex items-center bg-red-700 border-0 py-1 px-3 focus:outline-none hover:bg-red-500 rounded text-white transition-all"
>
Back To Homepage
</Link>
<button
onClick={handleGoBack}
className="inline-flex items-center mt-4 bg-gray-300 border-0 py-1 px-3 focus:outline-none hover:bg-gray-200 rounded text-black transition-all"
>
Go Back
</button>
</div>

</div>

</div>
);
};
Expand Down