diff --git a/taxonomy-editor-frontend/src/App.tsx b/taxonomy-editor-frontend/src/App.tsx index 514fd4df..f44517a4 100644 --- a/taxonomy-editor-frontend/src/App.tsx +++ b/taxonomy-editor-frontend/src/App.tsx @@ -18,7 +18,7 @@ import { ProjectPage, projectLoader } from "./pages/project"; import { ProjectNotFound } from "./pages/project/ProjectNotFound"; import { PageNotFound } from "./pages/PageNotFound"; import { RootLayout } from "./pages/RootLayout"; - +import { AppProvider } from "./components/UseContext"; const theme = createTheme({ typography: { fontFamily: "Plus Jakarta Sans", @@ -79,12 +79,14 @@ const router = createBrowserRouter([ function App() { return ( - - - - - - + + + + + + + + ); } diff --git a/taxonomy-editor-frontend/src/components/ResponsiveAppBar.tsx b/taxonomy-editor-frontend/src/components/ResponsiveAppBar.tsx index f7334414..b2ce2ae1 100644 --- a/taxonomy-editor-frontend/src/components/ResponsiveAppBar.tsx +++ b/taxonomy-editor-frontend/src/components/ResponsiveAppBar.tsx @@ -1,6 +1,5 @@ import { useState, useRef, useMemo } from "react"; -import { Link, useParams, Params } from "react-router-dom"; - +import { Link, useParams, Params, useNavigate } from "react-router-dom"; import AppBar from "@mui/material/AppBar"; import Box from "@mui/material/Box"; import Toolbar from "@mui/material/Toolbar"; @@ -16,6 +15,7 @@ import MuiLink from "@mui/material/Link"; import SettingsIcon from "@mui/icons-material/Settings"; import { useTranslation } from "react-i18next"; import logoUrl from "@/assets/logosmall.jpg"; +import { useAppContext } from "./UseContext"; const getDisplayedPages = ( params: Params, @@ -35,15 +35,30 @@ const getDisplayedPages = ( export const ResponsiveAppBar = () => { const params = useParams(); const displayedPages = useMemo(() => getDisplayedPages(params), [params]); - const { t } = useTranslation(); - const menuAnchorRef = useRef(); + const menuAnchorRef = useRef(null); const [isMenuOpen, setIsMenuOpen] = useState(false); + const navigate = useNavigate(); + + const { taxonomyName, ownerName, description, clearContext } = + useAppContext(); const handleCloseNavMenu = () => { setIsMenuOpen(false); }; + const handleNavigation = (url: string) => { + if (taxonomyName || ownerName || description) { + const confirmLeave = window.confirm( + "You have unsaved changes. Are you sure you want to leave?", + ); + if (!confirmLeave) return; // Don't navigate if user cancels + clearContext(); // Call the clearContext function to clear the form + } + navigate(url); // Proceed with the navigation + handleCloseNavMenu(); // Close the menu + }; + return ( @@ -85,7 +100,7 @@ export const ResponsiveAppBar = () => { page.url ? ( handleNavigation(`/${page.url}`)} // Use handleNavigation component={Link} to={`/${page.url}`} > @@ -101,24 +116,6 @@ export const ResponsiveAppBar = () => { )} - - Taxonomy Editor - {/* Desktop content */} { > { + event.preventDefault(); + handleNavigation("/"); + }} rel="noopener" > { { + // Prevent the default navigation behavior + event.preventDefault(); + + // Call the handleNavigation to check for unsaved changes and navigate + handleNavigation("/"); // Navigate to homepage if no unsaved changes + }} sx={{ mr: 2, display: "flex", @@ -165,6 +172,7 @@ export const ResponsiveAppBar = () => { letterSpacing: ".1rem", color: "inherit", textDecoration: "none", + cursor: "pointer", // Add pointer cursor for better UX }} > Taxonomy Editor @@ -174,7 +182,7 @@ export const ResponsiveAppBar = () => {