Compare commits
8 Commits
manualRelo
...
debian12
Author | SHA1 | Date | |
---|---|---|---|
94675010ce | |||
fcb8af574c | |||
![]() |
e9e3343f26 | ||
93d535dad9 | |||
8f7b9a5406 | |||
489b29d8e5 | |||
e98c6474f0 | |||
![]() |
a80720fe5f |
@@ -16,7 +16,7 @@ app.use(cookieParser());
|
|||||||
app.use("/api", router);
|
app.use("/api", router);
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Express backend server is running at http://localhost:${port}`);
|
console.log(`Express backend server is running at http://45.133.75.67:5002:${port}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// error handling code
|
// error handling code
|
||||||
|
@@ -1,33 +1,42 @@
|
|||||||
services:
|
services:
|
||||||
# admin_react-frontend:
|
admin_react-frontend:
|
||||||
# container_name: bikelane-frontend_react-admin
|
container_name: bikelane-frontend_react-admin
|
||||||
# build: ./frontend_admin
|
build: ./frontend_admin
|
||||||
# ports:
|
ports:
|
||||||
# - "5001:5001"
|
- "5001:5001"
|
||||||
# environment:
|
networks:
|
||||||
# - CHOKIDAR_USEPOLLING=true
|
- proxynet
|
||||||
# volumes:
|
- bikelane_network
|
||||||
# - ./frontend_admin:/app
|
environment:
|
||||||
# - /app/node_modules
|
- CHOKIDAR_USEPOLLING=true
|
||||||
# restart: unless-stopped
|
volumes:
|
||||||
|
- ./frontend_admin:/app
|
||||||
|
- /app/node_modules
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
# user_react-frontend:
|
user_react-frontend:
|
||||||
# container_name: bikelane-frontend_react-user
|
container_name: bikelane-frontend_react-user
|
||||||
# build: ./frontend_user
|
build: ./frontend_user
|
||||||
# ports:
|
networks:
|
||||||
# - "5003:5003"
|
- proxynet
|
||||||
# environment:
|
- bikelane_network
|
||||||
# - CHOKIDAR_USEPOLLING=true
|
ports:
|
||||||
# volumes:
|
- "5003:5003"
|
||||||
# - ./frontend_user:/app
|
environment:
|
||||||
# - /app/node_modules
|
- CHOKIDAR_USEPOLLING=true
|
||||||
# restart: unless-stopped
|
volumes:
|
||||||
|
- ./frontend_user:/app
|
||||||
|
- /app/node_modules
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
bikelane-backend:
|
bikelane-backend:
|
||||||
container_name: bikelane-backend_express
|
container_name: bikelane-backend_express
|
||||||
build: ./backend
|
build: ./backend
|
||||||
ports:
|
ports:
|
||||||
- "5002:5002"
|
- "5002:5002"
|
||||||
|
networks:
|
||||||
|
- proxynet
|
||||||
|
- bikelane_network
|
||||||
environment:
|
environment:
|
||||||
DB_HOST: mysql
|
DB_HOST: mysql
|
||||||
DB_USER: root
|
DB_USER: root
|
||||||
@@ -43,6 +52,8 @@ services:
|
|||||||
container_name: bikelane-mysql
|
container_name: bikelane-mysql
|
||||||
image: mysql:8.0
|
image: mysql:8.0
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
networks:
|
||||||
|
- bikelane_network
|
||||||
environment:
|
environment:
|
||||||
MYSQL_ROOT_PASSWORD: D7Ze0lwV9hMrNQHdz1Q8yi0MIQuOO8
|
MYSQL_ROOT_PASSWORD: D7Ze0lwV9hMrNQHdz1Q8yi0MIQuOO8
|
||||||
MYSQL_DATABASE: bikelane
|
MYSQL_DATABASE: bikelane
|
||||||
@@ -53,3 +64,9 @@ services:
|
|||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
mysql-data:
|
mysql-data:
|
||||||
|
|
||||||
|
networks:
|
||||||
|
proxynet:
|
||||||
|
external: true
|
||||||
|
bikelane_network:
|
||||||
|
external: false
|
||||||
|
@@ -1 +1 @@
|
|||||||
REACT_APP_SERVER_URL=http://localhost:5002
|
REACT_APP_SERVER_URL=http://45.133.75.67
|
@@ -1,16 +1,13 @@
|
|||||||
import "./App.css";
|
import "./App.css";
|
||||||
import Layout from "./layout/Layout";
|
import Layout from "./layout/Layout";
|
||||||
import { useUsers } from "./utils/useUsers";
|
|
||||||
import UserTable from "./components/UserTable";
|
import UserTable from "./components/UserTable";
|
||||||
import { useEffect } from "react";
|
import LoginCard from "./components/LoginCard";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
import { loadTheme } from "./utils/frontendService";
|
import { loadTheme } from "./utils/frontendService";
|
||||||
import { myToast } from "./utils/frontendService";
|
import { myToast } from "./utils/frontendService";
|
||||||
import "react-toastify/dist/ReactToastify.css";
|
import "react-toastify/dist/ReactToastify.css";
|
||||||
import Cookies from "js-cookie";
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const users = useUsers();
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadTheme();
|
loadTheme();
|
||||||
if (Cookies.get("token")) {
|
if (Cookies.get("token")) {
|
||||||
@@ -18,10 +15,31 @@ function App() {
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const [isAuthenticated, setIsAuthenticated] = useState(
|
||||||
|
!!Cookies.get("token")
|
||||||
|
);
|
||||||
|
const [showLogin, setShowLogin] = useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout>
|
<AuthContext.Provider value={{ isAuthenticated, setIsAuthenticated }}>
|
||||||
<UserTable users={users} />
|
<Layout>
|
||||||
</Layout>
|
{isAuthenticated ? (
|
||||||
|
<UserTable isAuthenticated={isAuthenticated} />
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<LoginCard
|
||||||
|
onClose={() => setShowLogin(false)}
|
||||||
|
changeAuth={(loggedIn) => setIsAuthenticated(loggedIn)}
|
||||||
|
/>
|
||||||
|
<div className="flex items-center justify-center h-full">
|
||||||
|
<h2 className="text-2xl font-bold text-gray-700 dark:text-gray-300">
|
||||||
|
Please log in to view the user table.
|
||||||
|
</h2>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Layout>
|
||||||
|
</AuthContext.Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,46 +1,39 @@
|
|||||||
import { useState } from "react";
|
import React, { useContext } from "react";
|
||||||
import React from "react";
|
import Cookies from "js-cookie";
|
||||||
import LoginCard from "./LoginCard";
|
import { AuthContext } from "../utils/context";
|
||||||
import { greeting } from "../utils/frontendService";
|
import { myToast } from "../utils/frontendService";
|
||||||
import { changeTheme } from "../utils/frontendService";
|
|
||||||
|
|
||||||
const Header: React.FC = () => {
|
const Header: React.FC = () => {
|
||||||
const [loginCardVisible, setLoginCardVisible] = useState(false);
|
const { isAuthenticated, setIsAuthenticated } = useContext(AuthContext);
|
||||||
|
const firstName = Cookies.get("firstName");
|
||||||
|
|
||||||
const closeLoginCard = () => {
|
const handleLogout = () => {
|
||||||
setLoginCardVisible(false);
|
Cookies.remove("token");
|
||||||
|
Cookies.remove("firstName");
|
||||||
|
setIsAuthenticated(false);
|
||||||
|
myToast("Logged out successfully!", "info");
|
||||||
};
|
};
|
||||||
|
|
||||||
let loginBtnVal: string = "Hello, " + greeting() + "!";
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<header className="bg-blue-600 dark:bg-gray-900 text-white p-4 shadow-md">
|
<header className="w-full flex justify-between items-center px-8 py-4 bg-gradient-to-r from-blue-100 via-blue-200 to-blue-100 dark:from-gray-900 dark:via-gray-950 dark:to-gray-900 shadow-lg">
|
||||||
<div className="container mx-auto flex justify-between items-center">
|
<h1 className="text-2xl font-bold text-blue-700 dark:text-blue-200 tracking-wide">
|
||||||
<h1 className="text-xl font-bold">
|
🚲 Bikelane Dashboard
|
||||||
Bikelane <strong>Admin Panel</strong>
|
</h1>
|
||||||
</h1>
|
<div className="flex items-center gap-4">
|
||||||
<nav>
|
{isAuthenticated && (
|
||||||
<ul className="flex space-x-4">
|
<>
|
||||||
<li>
|
<span className="text-lg font-semibold text-blue-700 dark:text-blue-200">
|
||||||
<a className="hover:underline">
|
Hello, {firstName || "User"}
|
||||||
<button
|
</span>
|
||||||
onClick={() => changeTheme()}
|
<button
|
||||||
className="bg-blue-700 dark:bg-gray-800 shadow-md hover:bg-blue-800 dark:hover:bg-gray-700 transition padding px-4 py-2 rounded-md text-white font-semibold"
|
onClick={handleLogout}
|
||||||
>
|
className="px-4 py-2 bg-red-600 text-white rounded shadow hover:bg-red-700 transition"
|
||||||
Change Theme
|
>
|
||||||
</button>
|
Logout
|
||||||
<button
|
</button>
|
||||||
onClick={() => setLoginCardVisible(true)}
|
</>
|
||||||
className="bg-blue-700 dark:bg-gray-800 shadow-md hover:bg-blue-800 dark:hover:bg-gray-700 transition padding px-4 py-2 rounded-md text-white font-semibold"
|
)}
|
||||||
>
|
|
||||||
{loginBtnVal ?? "Login"}
|
|
||||||
</button>
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</nav>
|
|
||||||
</div>
|
</div>
|
||||||
<div>{loginCardVisible && <LoginCard onClose={closeLoginCard} />}</div>
|
|
||||||
</header>
|
</header>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@@ -1,93 +1,85 @@
|
|||||||
import React from "react";
|
import React, { useState, useContext } from "react";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { logout, loginUser } from "../utils/userHandler";
|
import { AuthContext } from "../utils/context";
|
||||||
type LoginCardProps = {
|
import { myToast } from "../utils/frontendService";
|
||||||
onClose: () => void;
|
|
||||||
};
|
interface LoginCardProps {
|
||||||
|
onClose: () => void;
|
||||||
|
changeAuth?: (loggedIn: boolean) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const LoginCard: React.FC<LoginCardProps> = ({ onClose, changeAuth }) => {
|
||||||
|
const [username, setUsername] = useState("");
|
||||||
|
const [password, setPassword] = useState("");
|
||||||
|
const [error, setError] = useState("");
|
||||||
|
const { setIsAuthenticated } = useContext(AuthContext);
|
||||||
|
|
||||||
|
const handleLogin = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
setError("");
|
||||||
|
try {
|
||||||
|
const response = await fetch("http://localhost:5002/api/login", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ username, password }),
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
if (response.ok && data.token) {
|
||||||
|
Cookies.set("token", data.token);
|
||||||
|
Cookies.set("firstName", data.user.first_name);
|
||||||
|
setIsAuthenticated(true);
|
||||||
|
if (changeAuth) changeAuth(true);
|
||||||
|
onClose();
|
||||||
|
myToast("Login successful!", "success");
|
||||||
|
} else {
|
||||||
|
myToast("Login failed!", "error");
|
||||||
|
setError(data.message || "Login fehlgeschlagen");
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
setError("Netzwerkfehler");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const LoginCard: React.FC<LoginCardProps> = ({ onClose }) => {
|
|
||||||
return (
|
return (
|
||||||
<div className="fixed inset-0 flex items-center justify-center bg-black/35">
|
<div className="fixed inset-0 z-50 pointer-events-none">
|
||||||
<div className="max-w-sm bg-white dark:bg-gray-900 rounded-xl shadow-md p-8 relative">
|
{/* Overlay */}
|
||||||
<button
|
<div className="absolute inset-0 bg-white bg-opacity-40 backdrop-blur-sm pointer-events-auto"></div>
|
||||||
className="absolute top-4 right-4 bg-red-500 text-white w-8 h-8 rounded-full flex items-center justify-center font-bold shadow hover:bg-red-600 transition focus:outline-none focus:ring-2 focus:ring-red-400"
|
{/* Card oben am Dashboard */}
|
||||||
onClick={onClose}
|
<form
|
||||||
aria-label="Close"
|
onSubmit={handleLogin}
|
||||||
>
|
className="absolute left-1/2 -translate-x-1/2 top-10 z-10 bg-white dark:bg-gray-900 rounded-2xl shadow-2xl px-8 py-10 flex flex-col gap-6 min-w-[340px] max-w-[90vw] border border-blue-200 dark:border-gray-800 pointer-events-auto"
|
||||||
×
|
>
|
||||||
</button>
|
<h2 className="text-2xl font-bold text-center text-blue-700 dark:text-blue-200 mb-2">
|
||||||
<h2 className="text-black dark:text-white text-2xl font-bold mb-6 text-center">
|
|
||||||
Login
|
Login
|
||||||
</h2>
|
</h2>
|
||||||
<form
|
{error && (
|
||||||
onSubmit={async (event) => {
|
<div className="text-red-600 text-center font-semibold">{error}</div>
|
||||||
event.preventDefault();
|
|
||||||
const formData = new FormData(event.currentTarget);
|
|
||||||
const username = formData.get("username");
|
|
||||||
const password = formData.get("password");
|
|
||||||
loginUser(username as string, password as string);
|
|
||||||
}}
|
|
||||||
className="space-y-4 text-black dark:text-white"
|
|
||||||
>
|
|
||||||
<div>
|
|
||||||
<label
|
|
||||||
htmlFor="username"
|
|
||||||
className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"
|
|
||||||
>
|
|
||||||
Username
|
|
||||||
</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
name="username"
|
|
||||||
required
|
|
||||||
id="username"
|
|
||||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-white"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label
|
|
||||||
htmlFor="password"
|
|
||||||
className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"
|
|
||||||
>
|
|
||||||
Password
|
|
||||||
</label>
|
|
||||||
<input
|
|
||||||
type="password"
|
|
||||||
name="password"
|
|
||||||
required
|
|
||||||
id="password"
|
|
||||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:text-white"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
{Cookies.get("name") ? (
|
|
||||||
<p></p>
|
|
||||||
) : (
|
|
||||||
<input
|
|
||||||
type="submit"
|
|
||||||
value="Login"
|
|
||||||
className="w-full bg-blue-600 text-white font-semibold py-2 rounded-md hover:bg-blue-700 transition"
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</form>
|
|
||||||
{Cookies.get("name") ? (
|
|
||||||
<button
|
|
||||||
className="w-full bg-black dark:bg-gray-800 text-white font-semibold py-2 rounded-md hover:bg-green-400 dark:hover:bg-green-600 transition"
|
|
||||||
onClick={logout}
|
|
||||||
>
|
|
||||||
Logout
|
|
||||||
</button>
|
|
||||||
) : (
|
|
||||||
<p className="text-center text-gray-500 dark:text-gray-400 mt-4">
|
|
||||||
Don't have an account?{" "}
|
|
||||||
<a
|
|
||||||
href="/register"
|
|
||||||
className="text-blue-600 dark:text-blue-400 hover:underline"
|
|
||||||
>
|
|
||||||
Register here
|
|
||||||
</a>
|
|
||||||
</p>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Benutzername"
|
||||||
|
value={username}
|
||||||
|
onChange={(e) => setUsername(e.target.value)}
|
||||||
|
className="border border-blue-300 dark:border-gray-700 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-400 dark:bg-gray-800 dark:text-white"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
placeholder="Passwort"
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
className="border border-blue-300 dark:border-gray-700 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-400 dark:bg-gray-800 dark:text-white"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<div className="flex gap-3 mt-2 justify-center">
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="bg-blue-600 hover:bg-blue-700 text-white font-semibold px-5 py-2 rounded-lg shadow transition"
|
||||||
|
>
|
||||||
|
Login
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { MoreVertical } from "lucide-react";
|
import { MoreVertical } from "lucide-react";
|
||||||
import React, { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { deleteUser, updateUserFunc } from "../utils/userHandler.ts";
|
import { useUsers } from "../utils/useUsers.ts";
|
||||||
|
|
||||||
interface User {
|
interface User {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -14,16 +14,25 @@ interface User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface UserTableProps {
|
interface UserTableProps {
|
||||||
users: User[];
|
isAuthenticated: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const UserTable: React.FC<UserTableProps> = ({ users }) => {
|
const UserTable: React.FC<UserTableProps> = ({ isAuthenticated }) => {
|
||||||
const [openMenu, setOpenMenu] = useState<number | null>(null);
|
const [openMenu, setOpenMenu] = useState<number | null>(null);
|
||||||
const [userList, setUserList] = useState<User[]>(users);
|
|
||||||
|
const {
|
||||||
|
users,
|
||||||
|
refresh: refreshUsers,
|
||||||
|
setUsers,
|
||||||
|
deleteUser,
|
||||||
|
updateUser: updateUserFunc,
|
||||||
|
} = useUsers();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setUserList(users);
|
if (isAuthenticated) {
|
||||||
}, [users]);
|
refreshUsers();
|
||||||
|
}
|
||||||
|
}, [isAuthenticated]);
|
||||||
|
|
||||||
const handleMenuClick = (userId: number) => {
|
const handleMenuClick = (userId: number) => {
|
||||||
setOpenMenu(openMenu === userId ? null : userId);
|
setOpenMenu(openMenu === userId ? null : userId);
|
||||||
@@ -32,8 +41,10 @@ const UserTable: React.FC<UserTableProps> = ({ users }) => {
|
|||||||
const handleMenuClose = () => setOpenMenu(null);
|
const handleMenuClose = () => setOpenMenu(null);
|
||||||
|
|
||||||
const handleInputChange = (id: number, field: keyof User, value: string) => {
|
const handleInputChange = (id: number, field: keyof User, value: string) => {
|
||||||
setUserList((prev) =>
|
setUsers((prevUsers) =>
|
||||||
prev.map((user) => (user.id === id ? { ...user, [field]: value } : user))
|
prevUsers.map((user) =>
|
||||||
|
user.id === id ? { ...user, [field]: value } : user
|
||||||
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -71,9 +82,9 @@ const UserTable: React.FC<UserTableProps> = ({ users }) => {
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody className="bg-white text-black dark:bg-gray-900 dark:text-white divide-y divide-gray-100 dark:divide-gray-800 z-10">
|
<tbody className="bg-white text-black dark:bg-gray-900 dark:text-white divide-y divide-gray-100 dark:divide-gray-800 z-10">
|
||||||
{userList.map((user, idx) => {
|
{(users ?? []).map((user, idx) => {
|
||||||
// If this is one of the last 2 rows, open menu upwards
|
// If this is one of the last 2 rows, open menu upwards
|
||||||
const openUp = idx >= userList.length - 2;
|
const openUp = idx >= users.length - 2;
|
||||||
return (
|
return (
|
||||||
<tr
|
<tr
|
||||||
key={user.id}
|
key={user.id}
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import Header from "../components/Header";
|
import Header from "../components/Header";
|
||||||
import Cookies from "js-cookie";
|
|
||||||
import Sidebar from "../components/Sidebar";
|
import Sidebar from "../components/Sidebar";
|
||||||
import { ToastContainer } from "react-toastify";
|
import { ToastContainer } from "react-toastify";
|
||||||
|
|
||||||
@@ -9,23 +8,15 @@ type LayoutProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const Layout: React.FC<LayoutProps> = ({ children }) => {
|
const Layout: React.FC<LayoutProps> = ({ children }) => {
|
||||||
const isLoggedIn = !!Cookies.get("name");
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col min-h-screen bg-gradient-to-br from-blue-50 via-blue-100 to-blue-200 dark:from-gray-900 dark:via-gray-950 dark:to-gray-900">
|
<div className="flex flex-col min-h-screen bg-gradient-to-br from-blue-50 via-blue-100 to-blue-200 dark:from-gray-900 dark:via-gray-950 dark:to-gray-900">
|
||||||
<Header />
|
<Header />
|
||||||
<ToastContainer />
|
<ToastContainer />
|
||||||
<div className="flex flex-1">
|
<div className="flex flex-1">
|
||||||
{isLoggedIn && (
|
<Sidebar />
|
||||||
<>
|
<main className="flex-1 p-10 bg-white/80 dark:bg-gray-900/80 rounded-l-3xl shadow-2xl m-6 overflow-auto text-black dark:text-white">
|
||||||
{/* Sidebar */}
|
{children}
|
||||||
<Sidebar />
|
</main>
|
||||||
{/* Main content */}
|
|
||||||
<main className="flex-1 p-10 bg-white/80 dark:bg-gray-900/80 rounded-l-3xl shadow-2xl m-6 overflow-auto text-black dark:text-white">
|
|
||||||
{children}
|
|
||||||
</main>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
<footer className="bg-gradient-to-r from-blue-800 via-blue-900 to-blue-800 dark:from-gray-900 dark:via-gray-950 dark:to-gray-900 text-blue-100 dark:text-gray-400 py-6 px-5 text-center rounded-t-3xl shadow-xl mt-8 tracking-wide border-t border-blue-700 dark:border-gray-800">
|
<footer className="bg-gradient-to-r from-blue-800 via-blue-900 to-blue-800 dark:from-gray-900 dark:via-gray-950 dark:to-gray-900 text-blue-100 dark:text-gray-400 py-6 px-5 text-center rounded-t-3xl shadow-xl mt-8 tracking-wide border-t border-blue-700 dark:border-gray-800">
|
||||||
<div className="flex flex-col items-center gap-2">
|
<div className="flex flex-col items-center gap-2">
|
||||||
|
9
frontend_admin/src/utils/context.ts
Normal file
9
frontend_admin/src/utils/context.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
export const AuthContext = React.createContext<{
|
||||||
|
isAuthenticated: boolean;
|
||||||
|
setIsAuthenticated: (auth: boolean) => void;
|
||||||
|
}>({
|
||||||
|
isAuthenticated: false,
|
||||||
|
setIsAuthenticated: () => {},
|
||||||
|
});
|
3
frontend_admin/src/utils/context.tsx
Normal file
3
frontend_admin/src/utils/context.tsx
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import { createContext } from "react";
|
||||||
|
|
||||||
|
export const AuthContext = createContext(false);
|
@@ -2,7 +2,7 @@ import Cookies from "js-cookie";
|
|||||||
import { toast, type ToastOptions } from "react-toastify";
|
import { toast, type ToastOptions } from "react-toastify";
|
||||||
|
|
||||||
export const greeting = () => {
|
export const greeting = () => {
|
||||||
return Cookies.get("name") ?? "Login";
|
return Cookies.get("username") ?? "Login";
|
||||||
};
|
};
|
||||||
|
|
||||||
export const loadTheme = () => {
|
export const loadTheme = () => {
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, type Dispatch, type SetStateAction } from "react";
|
||||||
|
import Cookies from "js-cookie";
|
||||||
|
import { myToast } from "./frontendService";
|
||||||
|
|
||||||
export interface User {
|
export interface User {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -11,20 +13,139 @@ export interface User {
|
|||||||
role: string;
|
role: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useUsers(): User[] {
|
export type UserReturn = {
|
||||||
|
users: User[];
|
||||||
|
refresh: () => void;
|
||||||
|
setUsers: Dispatch<SetStateAction<User[]>>;
|
||||||
|
deleteUser: (id: number) => void;
|
||||||
|
updateUser: (id: number) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function useUsers(): UserReturn {
|
||||||
const [users, setUsers] = useState<User[]>([]);
|
const [users, setUsers] = useState<User[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
const token = Cookies.get("token");
|
||||||
const data = localStorage.getItem("users");
|
|
||||||
if (data) {
|
|
||||||
try {
|
|
||||||
const parsed = JSON.parse(data);
|
|
||||||
setUsers(parsed.result || []);
|
|
||||||
} catch {
|
|
||||||
setUsers([]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return users;
|
const headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchUsers = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch("http://localhost:5002/api/getAllUsers", {
|
||||||
|
method: "GET",
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
if (response.ok) {
|
||||||
|
const data = await response.json();
|
||||||
|
setUsers(data.result);
|
||||||
|
myToast("Users fetched successfully", "success");
|
||||||
|
} else {
|
||||||
|
console.error("Failed to fetch users");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching users:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const deleteUser = (id: number) => {
|
||||||
|
fetch("http://localhost:5002/api/deleteUser", {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify({ id: id }),
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((response) => {
|
||||||
|
if (response.ok) {
|
||||||
|
setUsers((prevUsers) => prevUsers.filter((user) => user.id !== id));
|
||||||
|
myToast("User deleted successfully", "success");
|
||||||
|
} else {
|
||||||
|
alert("Failed to delete user");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.log("Error deleting user: ", error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateUserFunc = async (userID: number) => {
|
||||||
|
console.log("UpdateFunc" + userID);
|
||||||
|
|
||||||
|
// Validate that required DOM elements exist
|
||||||
|
const usernameEl = document.getElementById(
|
||||||
|
`username-${userID}`
|
||||||
|
) as HTMLInputElement;
|
||||||
|
const firstNameEl = document.getElementById(
|
||||||
|
`first_name-${userID}`
|
||||||
|
) as HTMLInputElement;
|
||||||
|
const lastNameEl = document.getElementById(
|
||||||
|
`last_name-${userID}`
|
||||||
|
) as HTMLInputElement;
|
||||||
|
const emailEl = document.getElementById(
|
||||||
|
`email-${userID}`
|
||||||
|
) as HTMLInputElement;
|
||||||
|
const passwordEl = document.getElementById(
|
||||||
|
`password-${userID}`
|
||||||
|
) as HTMLInputElement;
|
||||||
|
|
||||||
|
if (!usernameEl || !firstNameEl || !lastNameEl || !emailEl) {
|
||||||
|
console.error("Required form elements not found");
|
||||||
|
alert("Form elements not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const userData = {
|
||||||
|
id: userID,
|
||||||
|
username: usernameEl.value,
|
||||||
|
first_name: firstNameEl.value,
|
||||||
|
last_name: lastNameEl.value,
|
||||||
|
email: emailEl.value,
|
||||||
|
password: passwordEl?.value || "", // password might be optional
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("Sending user data:", userData);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch("http://localhost:5002/api/updateUser", {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(userData),
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("Response status:", response.status);
|
||||||
|
console.log("Response ok:", response.ok);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
console.log("User updated successfully");
|
||||||
|
setUsers((prevUsers) =>
|
||||||
|
prevUsers.map((user) =>
|
||||||
|
user.id === userID ? { ...user, ...userData } : user
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
myToast("User updated successfully", "success");
|
||||||
|
} else {
|
||||||
|
const errorText = await response.text();
|
||||||
|
console.error("Server error:", response.status, errorText);
|
||||||
|
alert(`Failed to update user: ${response.status} - ${errorText}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Network error updating user:", error);
|
||||||
|
alert("Network error occurred while updating user");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
users: users,
|
||||||
|
setUsers: setUsers,
|
||||||
|
refresh: () => fetchUsers(),
|
||||||
|
deleteUser: (id: number) => deleteUser(id),
|
||||||
|
updateUser: (id: number) => updateUserFunc(id),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@ import Cookies from "js-cookie";
|
|||||||
import { myToast } from "./frontendService";
|
import { myToast } from "./frontendService";
|
||||||
|
|
||||||
export const loginUser = (username: string, password: string) => {
|
export const loginUser = (username: string, password: string) => {
|
||||||
fetch(`http://localhost:5002/api/login`, {
|
fetch("http://localhost:5002/api/login", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ username, password }),
|
body: JSON.stringify({ username, password }),
|
||||||
@@ -35,14 +35,14 @@ export const loginUser = (username: string, password: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const logout = () => {
|
export const logout = () => {
|
||||||
Cookies.remove("name");
|
Cookies.remove("username");
|
||||||
Cookies.remove("token");
|
Cookies.remove("token");
|
||||||
localStorage.removeItem("users");
|
localStorage.removeItem("users");
|
||||||
myToast("Logged out successfully!", "success");
|
myToast("Logged out successfully!", "success");
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteUser = (id: number) => {
|
export const deleteUser = (id: number) => {
|
||||||
fetch("http://localhost:5002/api/deleteUser", {
|
fetch("http://45.133.75.67:5002/api/deleteUser", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({ id: id }),
|
body: JSON.stringify({ id: id }),
|
||||||
headers: {
|
headers: {
|
||||||
@@ -64,7 +64,7 @@ export const deleteUser = (id: number) => {
|
|||||||
|
|
||||||
export const replaceUsers = async (alertMessage: string) => {
|
export const replaceUsers = async (alertMessage: string) => {
|
||||||
localStorage.removeItem("users");
|
localStorage.removeItem("users");
|
||||||
await fetch("http://localhost:5002/api/getAllUsers", {
|
await fetch("http://45.133.75.67:5002/api/getAllUsers", {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
@@ -115,7 +115,7 @@ export const updateUserFunc = async (userID: number) => {
|
|||||||
console.log("Sending user data:", userData);
|
console.log("Sending user data:", userData);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("http://localhost:5002/api/updateUser", {
|
const response = await fetch("http://45.133.75.67:5002/api/updateUser", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(userData),
|
body: JSON.stringify(userData),
|
||||||
headers: {
|
headers: {
|
||||||
|
6
package-lock.json
generated
Normal file
6
package-lock.json
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "bikelane",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {}
|
||||||
|
}
|
47
scheme.sql
47
scheme.sql
@@ -11,8 +11,51 @@ CREATE TABLE users (
|
|||||||
);
|
);
|
||||||
|
|
||||||
-- Mock data for users
|
-- Mock data for users
|
||||||
|
INSERT INTO users (username, first_name, last_name, email, password, role)
|
||||||
|
VALUES
|
||||||
|
('t', 'John', 'Doe', 'd@example.com', 'g', 'admin');
|
||||||
|
|
||||||
INSERT INTO users (username, first_name, last_name, email, password)
|
INSERT INTO users (username, first_name, last_name, email, password)
|
||||||
VALUES
|
VALUES
|
||||||
('test1', 'John', 'Doe', 'jdoe@example.com', '1test'),
|
('test1', 'John', 'Doe', 'jdoe@example.com', '1test'),
|
||||||
('t', 'John', 'Doe', 'd@example.com', 'g'),
|
('test2', 'Alice', 'Smith', 'asmith@example.com', '2test'),
|
||||||
('test2', 'Alice', 'Smith', 'asmith@example.com', '2test');
|
('test3', 'Bob', 'Johnson', 'bjohnson@example.com', '3test'),
|
||||||
|
('test4', 'Carol', 'Williams', 'cwilliams@example.com', '4test'),
|
||||||
|
('test5', 'David', 'Brown', 'dbrown@example.com', '5test'),
|
||||||
|
('test6', 'Eve', 'Davis', 'edavis@example.com', '6test'),
|
||||||
|
('test7', 'Frank', 'Miller', 'fmiller@example.com', '7test'),
|
||||||
|
('test8', 'Grace', 'Wilson', 'gwilson@example.com', '8test'),
|
||||||
|
('test9', 'Hank', 'Moore', 'hmoore@example.com', '9test'),
|
||||||
|
('test10', 'Ivy', 'Taylor', 'itaylor@example.com', '10test'),
|
||||||
|
('test11', 'Jack', 'Anderson', 'janderson@example.com', '11test'),
|
||||||
|
('test12', 'Kathy', 'Thomas', 'kthomas@example.com', '12test'),
|
||||||
|
('test13', 'Leo', 'Jackson', 'ljackson@example.com', '13test'),
|
||||||
|
('test14', 'Mona', 'White', 'mwhite@example.com', '14test'),
|
||||||
|
('test15', 'Nina', 'Harris', 'nharris@example.com', '15test'),
|
||||||
|
('test16', 'Oscar', 'Martin', 'omartin@example.com', '16test'),
|
||||||
|
('test17', 'Paul', 'Thompson', 'pthompson@example.com', '17test'),
|
||||||
|
('test18', 'Quinn', 'Garcia', 'qgarcia@example.com', '18test'),
|
||||||
|
('test19', 'Rita', 'Martinez', 'rmartinez@example.com', '19test'),
|
||||||
|
('test20', 'Sam', 'Robinson', 'srobinson@example.com', '20test'),
|
||||||
|
('test21', 'Tina', 'Clark', 'tclark@example.com', '21test'),
|
||||||
|
('test22', 'Uma', 'Rodriguez', 'urodriguez@example.com', '22test'),
|
||||||
|
('test23', 'Vince', 'Lewis', 'vlewis@example.com', '23test'),
|
||||||
|
('test24', 'Wendy', 'Lee', 'wlee@example.com', '24test'),
|
||||||
|
('test25', 'Xander', 'Walker', 'xwalker@example.com', '25test'),
|
||||||
|
('test26', 'Yara', 'Hall', 'yhall@example.com', '26test'),
|
||||||
|
('test27', 'Zane', 'Allen', 'zallen@example.com', '27test'),
|
||||||
|
('test28', 'Amy', 'Young', 'ayoung@example.com', '28test'),
|
||||||
|
('test29', 'Ben', 'King', 'bking@example.com', '29test'),
|
||||||
|
('test30', 'Cathy', 'Wright', 'cwright@example.com', '30test'),
|
||||||
|
('test31', 'Dan', 'Scott', 'dscott@example.com', '31test'),
|
||||||
|
('test32', 'Ella', 'Green', 'egreen@example.com', '32test'),
|
||||||
|
('test33', 'Finn', 'Baker', 'fbaker@example.com', '33test'),
|
||||||
|
('test34', 'Gina', 'Adams', 'gadams@example.com', '34test'),
|
||||||
|
('test35', 'Hugo', 'Nelson', 'hnelson@example.com', '35test'),
|
||||||
|
('test36', 'Iris', 'Carter', 'icarter@example.com', '36test'),
|
||||||
|
('test37', 'Jake', 'Mitchell', 'jmitchell@example.com', '37test'),
|
||||||
|
('test38', 'Kara', 'Perez', 'kperez@example.com', '38test'),
|
||||||
|
('test39', 'Liam', 'Roberts', 'lroberts@example.com', '39test'),
|
||||||
|
('test40', 'Mia', 'Turner', 'mturner@example.com', '40test'),
|
||||||
|
('test41', 'Noah', 'Phillips', 'nphillips@example.com', '41test'),
|
||||||
|
('test42', 'Olga', 'Campbell', 'ocampbell@example.com', '42test');
|
Reference in New Issue
Block a user