diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index fdd45e2..bb84940 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -6,14 +6,17 @@ import Form2 from "./components/Form2"; import Form3 from "./components/Form3"; import LoginForm from "./components/LoginForm"; import Cookies from "js-cookie"; -import { ToastContainer } from "react-toastify"; +import { fetchAllData, ALL_ITEMS_UPDATED_EVENT } from "./utils/fetchData"; +import { myToast } from "./utils/toastify"; function App() { const [isLoggedIn, setIsLoggedIn] = useState(false); useEffect(() => { - if (Cookies.get("token")) { + const token = Cookies.get("token"); + if (token) { setIsLoggedIn(true); + fetchAllData(token); } localStorage.setItem("borrowableItems", JSON.stringify([])); @@ -21,8 +24,17 @@ function App() { }, []); // Mock flow without real logic: show the three sections stacked for design preview + const handleLogout = () => { + Cookies.remove("token"); + localStorage.removeItem("allItems"); + // Let listeners refresh from empty state + window.dispatchEvent(new Event(ALL_ITEMS_UPDATED_EVENT)); + myToast("Logged out successfully!", "success"); + setIsLoggedIn(false); + }; + return isLoggedIn ? ( - +
@@ -32,22 +44,7 @@ function App() {
) : ( - <> - setIsLoggedIn(true)} /> - - + setIsLoggedIn(true)} /> ); } diff --git a/frontend/src/components/Header.tsx b/frontend/src/components/Header.tsx index fcd95d2..e891823 100644 --- a/frontend/src/components/Header.tsx +++ b/frontend/src/components/Header.tsx @@ -1,7 +1,10 @@ import React from "react"; -import Cookies from "js-cookie"; -const Header: React.FC = () => { +type HeaderProps = { + onLogout: () => void; +}; + +const Header: React.FC = ({ onLogout }) => { return (

@@ -11,12 +14,8 @@ const Header: React.FC = () => { Schnell und unkompliziert Equipment reservieren

); }; diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index bef5202..e583583 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -1,10 +1,25 @@ -import { StrictMode } from 'react' -import { createRoot } from 'react-dom/client' -import './index.css' -import App from './App.tsx' +import { StrictMode } from "react"; +import { createRoot } from "react-dom/client"; +import "./index.css"; +import App from "./App.tsx"; +import { ToastContainer } from "react-toastify"; +import "react-toastify/dist/ReactToastify.css"; -createRoot(document.getElementById('root')!).render( +createRoot(document.getElementById("root")!).render( - , -) + + +); diff --git a/frontend/src/utils/fetchData.ts b/frontend/src/utils/fetchData.ts index 5eec60f..eb09261 100644 --- a/frontend/src/utils/fetchData.ts +++ b/frontend/src/utils/fetchData.ts @@ -1,6 +1,9 @@ import Cookies from "js-cookie"; import { myToast } from "./toastify"; +// Event name used to notify the app when the list of items has been updated +export const ALL_ITEMS_UPDATED_EVENT = "allItemsUpdated"; + export const fetchAllData = async (token: string | undefined) => { if (!token) return; try { @@ -19,6 +22,8 @@ export const fetchAllData = async (token: string | undefined) => { const data = await response.json(); localStorage.setItem("allItems", JSON.stringify(data)); + // Notify listeners (e.g., Sidebar) that items have been updated + window.dispatchEvent(new Event(ALL_ITEMS_UPDATED_EVENT)); } catch (error) { myToast("An error occurred", "error"); }