Added improved login and logout route for the frontend. The frontend page now doesn't have to reload.
This commit is contained in:
@@ -6,14 +6,17 @@ import Form2 from "./components/Form2";
|
|||||||
import Form3 from "./components/Form3";
|
import Form3 from "./components/Form3";
|
||||||
import LoginForm from "./components/LoginForm";
|
import LoginForm from "./components/LoginForm";
|
||||||
import Cookies from "js-cookie";
|
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() {
|
function App() {
|
||||||
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (Cookies.get("token")) {
|
const token = Cookies.get("token");
|
||||||
|
if (token) {
|
||||||
setIsLoggedIn(true);
|
setIsLoggedIn(true);
|
||||||
|
fetchAllData(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
localStorage.setItem("borrowableItems", JSON.stringify([]));
|
localStorage.setItem("borrowableItems", JSON.stringify([]));
|
||||||
@@ -21,8 +24,17 @@ function App() {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Mock flow without real logic: show the three sections stacked for design preview
|
// 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 ? (
|
return isLoggedIn ? (
|
||||||
<Layout>
|
<Layout onLogout={handleLogout}>
|
||||||
<div className="space-y-10">
|
<div className="space-y-10">
|
||||||
<Form1 />
|
<Form1 />
|
||||||
<div className="h-px bg-blue-100" />
|
<div className="h-px bg-blue-100" />
|
||||||
@@ -32,22 +44,7 @@ function App() {
|
|||||||
</div>
|
</div>
|
||||||
</Layout>
|
</Layout>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<LoginForm onLogin={() => setIsLoggedIn(true)} />
|
||||||
<LoginForm onLogin={() => setIsLoggedIn(true)} />
|
|
||||||
<ToastContainer
|
|
||||||
position="top-right"
|
|
||||||
autoClose={3000}
|
|
||||||
hideProgressBar={false}
|
|
||||||
newestOnTop
|
|
||||||
closeOnClick
|
|
||||||
rtl={false}
|
|
||||||
pauseOnFocusLoss={false}
|
|
||||||
draggable
|
|
||||||
pauseOnHover
|
|
||||||
theme="light"
|
|
||||||
className="!z-50"
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,7 +1,10 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import Cookies from "js-cookie";
|
|
||||||
|
|
||||||
const Header: React.FC = () => {
|
type HeaderProps = {
|
||||||
|
onLogout: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const Header: React.FC<HeaderProps> = ({ onLogout }) => {
|
||||||
return (
|
return (
|
||||||
<header className="mb-6 md:mb-10">
|
<header className="mb-6 md:mb-10">
|
||||||
<h1 className="text-3xl md:text-4xl font-extrabold text-blue-800 tracking-tight drop-shadow-sm">
|
<h1 className="text-3xl md:text-4xl font-extrabold text-blue-800 tracking-tight drop-shadow-sm">
|
||||||
@@ -11,12 +14,8 @@ const Header: React.FC = () => {
|
|||||||
Schnell und unkompliziert Equipment reservieren
|
Schnell und unkompliziert Equipment reservieren
|
||||||
</p>
|
</p>
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
type="button"
|
||||||
Cookies.remove("token");
|
onClick={onLogout}
|
||||||
localStorage.removeItem("allItems");
|
|
||||||
|
|
||||||
window.location.reload();
|
|
||||||
}}
|
|
||||||
className="text-blue-500 hover:underline"
|
className="text-blue-500 hover:underline"
|
||||||
>
|
>
|
||||||
Logout
|
Logout
|
||||||
|
@@ -15,7 +15,6 @@ const LoginForm: React.FC<LoginFormProps> = ({ onLogin }) => {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const result = await loginUser(username, password);
|
const result = await loginUser(username, password);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
myToast("Login successful!", "success");
|
|
||||||
onLogin();
|
onLogin();
|
||||||
} else {
|
} else {
|
||||||
myToast("Login failed. Please check your credentials.", "error");
|
myToast("Login failed. Please check your credentials.", "error");
|
||||||
|
@@ -1,11 +1,22 @@
|
|||||||
import React from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import Object from "./Object";
|
import Object from "./Object";
|
||||||
import { useEffect } from "react";
|
import { ALL_ITEMS_UPDATED_EVENT } from "../utils/fetchData";
|
||||||
|
|
||||||
const Sidebar: React.FC = () => {
|
const Sidebar: React.FC = () => {
|
||||||
useEffect(() => {});
|
const [items, setItems] = useState<any[]>(
|
||||||
|
JSON.parse(localStorage.getItem("allItems") || "[]")
|
||||||
|
);
|
||||||
|
|
||||||
const items = JSON.parse(localStorage.getItem("allItems") || "[]");
|
useEffect(() => {
|
||||||
|
const handler = () => {
|
||||||
|
const next = JSON.parse(localStorage.getItem("allItems") || "[]");
|
||||||
|
setItems(next);
|
||||||
|
};
|
||||||
|
// Update immediately in case data changed before this mounted
|
||||||
|
handler();
|
||||||
|
window.addEventListener(ALL_ITEMS_UPDATED_EVENT, handler);
|
||||||
|
return () => window.removeEventListener(ALL_ITEMS_UPDATED_EVENT, handler);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<aside className="w-full md:w-80 md:min-h-screen bg-white/90 backdrop-blur md:border-r border-blue-100 shadow-xl flex flex-col p-6">
|
<aside className="w-full md:w-80 md:min-h-screen bg-white/90 backdrop-blur md:border-r border-blue-100 shadow-xl flex flex-col p-6">
|
||||||
|
@@ -1,14 +1,14 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import "../App.css";
|
import "../App.css";
|
||||||
import { ToastContainer } from "react-toastify";
|
|
||||||
import Header from "../components/Header";
|
import Header from "../components/Header";
|
||||||
import Sidebar from "../components/Sidebar";
|
import Sidebar from "../components/Sidebar";
|
||||||
|
|
||||||
type LayoutProps = {
|
type LayoutProps = {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
|
onLogout: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Layout: React.FC<LayoutProps> = ({ children }) => {
|
const Layout: React.FC<LayoutProps> = ({ children, onLogout }) => {
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen flex bg-gradient-to-r from-blue-50 via-white to-blue-100">
|
<div className="min-h-screen flex bg-gradient-to-r from-blue-50 via-white to-blue-100">
|
||||||
{/* Sidebar */}
|
{/* Sidebar */}
|
||||||
@@ -19,7 +19,7 @@ const Layout: React.FC<LayoutProps> = ({ children }) => {
|
|||||||
{/* Main */}
|
{/* Main */}
|
||||||
<main className="flex-1 flex flex-col items-center py-10 px-4 md:py-14">
|
<main className="flex-1 flex flex-col items-center py-10 px-4 md:py-14">
|
||||||
<div className="w-full max-w-3xl">
|
<div className="w-full max-w-3xl">
|
||||||
<Header />
|
<Header onLogout={onLogout} />
|
||||||
</div>
|
</div>
|
||||||
<div className="w-full max-w-3xl bg-white/90 shadow-2xl rounded-3xl p-6 md:p-10 ring-1 ring-blue-100">
|
<div className="w-full max-w-3xl bg-white/90 shadow-2xl rounded-3xl p-6 md:p-10 ring-1 ring-blue-100">
|
||||||
{children}
|
{children}
|
||||||
@@ -32,20 +32,6 @@ const Layout: React.FC<LayoutProps> = ({ children }) => {
|
|||||||
<Sidebar />
|
<Sidebar />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ToastContainer
|
|
||||||
position="top-right"
|
|
||||||
autoClose={3000}
|
|
||||||
hideProgressBar={false}
|
|
||||||
newestOnTop
|
|
||||||
closeOnClick
|
|
||||||
rtl={false}
|
|
||||||
pauseOnFocusLoss={false}
|
|
||||||
draggable
|
|
||||||
pauseOnHover
|
|
||||||
theme="light"
|
|
||||||
className="!z-50"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@@ -1,10 +1,25 @@
|
|||||||
import { StrictMode } from 'react'
|
import { StrictMode } from "react";
|
||||||
import { createRoot } from 'react-dom/client'
|
import { createRoot } from "react-dom/client";
|
||||||
import './index.css'
|
import "./index.css";
|
||||||
import App from './App.tsx'
|
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(
|
||||||
<StrictMode>
|
<StrictMode>
|
||||||
<App />
|
<App />
|
||||||
</StrictMode>,
|
<ToastContainer
|
||||||
)
|
position="top-right"
|
||||||
|
autoClose={3000}
|
||||||
|
hideProgressBar={false}
|
||||||
|
newestOnTop
|
||||||
|
closeOnClick
|
||||||
|
rtl={false}
|
||||||
|
pauseOnFocusLoss={false}
|
||||||
|
draggable
|
||||||
|
pauseOnHover
|
||||||
|
theme="light"
|
||||||
|
style={{ zIndex: 9999 }}
|
||||||
|
/>
|
||||||
|
</StrictMode>
|
||||||
|
);
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { myToast } from "./toastify";
|
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) => {
|
export const fetchAllData = async (token: string | undefined) => {
|
||||||
if (!token) return;
|
if (!token) return;
|
||||||
try {
|
try {
|
||||||
@@ -19,6 +22,8 @@ export const fetchAllData = async (token: string | undefined) => {
|
|||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
localStorage.setItem("allItems", JSON.stringify(data));
|
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) {
|
} catch (error) {
|
||||||
myToast("An error occurred", "error");
|
myToast("An error occurred", "error");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user