From e56278fa82155c9ac08ca659072612df740a922a Mon Sep 17 00:00:00 2001 From: "theis.gaedigk" Date: Wed, 23 Jul 2025 19:45:21 +0200 Subject: [PATCH] feat: add user update functionality and theme management --- backend/server.js | 29 +++++- backend/services/database.js | 9 +- frontend_admin/src/App.css | 18 +++- frontend_admin/src/App.tsx | 7 ++ frontend_admin/src/components/Header.tsx | 4 + frontend_admin/src/components/LoginCard.tsx | 2 + frontend_admin/src/components/Sidebar.tsx | 2 +- frontend_admin/src/components/UserTable.tsx | 97 +++++++++++++---- frontend_admin/src/utils/functions.ts | 110 +++++++++++++++++++- 9 files changed, 245 insertions(+), 33 deletions(-) diff --git a/backend/server.js b/backend/server.js index 0c07ffc..5c3f294 100644 --- a/backend/server.js +++ b/backend/server.js @@ -52,7 +52,7 @@ app.get("/api/getAllUsers", authenticate, async (req, res) => { if (req.user.role === "admin") { getAllUsers() .then((users) => { - res.status(200).json(users); + res.status(200).json(users).reload(); }) .catch((err) => { console.error("Error fetching users:", err); @@ -92,6 +92,33 @@ app.post("/api/deleteUser", authenticate, async (req, res) => { } }); +app.post("/api/updateUser", authenticate, async (req, res) => { + if (req.user.role === "admin") { + updateUser( + req.body.username, + req.body.first_name, + req.body.last_name, + req.body.password, + req.body.email, + req.body.id + ) + .then((result) => { + if (result.success) { + res.status(200).json(result); + } else { + throw new Error("Failed to update user"); + } + }) + .catch((err) => { + console.error("Error updating user:", err); + res + .status(500) + .json({ success: false, message: "Internal server error" }); + }); + console.log("User updated successfully"); + } +}) + app.listen(port, () => { console.log(`Express backend server is running at http://localhost:${port}`); }); diff --git a/backend/services/database.js b/backend/services/database.js index 160a7b4..52266cc 100644 --- a/backend/services/database.js +++ b/backend/services/database.js @@ -62,13 +62,14 @@ export async function updateUser( first_name, last_name, password, - email + email, + id ) { try { - // Update user details based on username + // Update user details based on id const [result] = await pool.query( - "UPDATE users SET first_name = ?, last_name = ?, password = ?, email = ? WHERE username = ?", - [first_name, last_name, password, email, username] + "UPDATE users SET first_name = ?, last_name = ?, password = ?, email = ?, username = ? WHERE id = ?", + [first_name, last_name, password, email, username, id] ); const resultOfquery = result.affectedRows; diff --git a/frontend_admin/src/App.css b/frontend_admin/src/App.css index a461c50..4dbfef3 100644 --- a/frontend_admin/src/App.css +++ b/frontend_admin/src/App.css @@ -1 +1,17 @@ -@import "tailwindcss"; \ No newline at end of file +@import "tailwindcss"; + +/* Example: App.css */ +body.dark { + background: #222; + color: #fff; +} + +body { + background: #fff; + color: #222; +} + +html.dark body { + background: #222; + color: #fff; +} diff --git a/frontend_admin/src/App.tsx b/frontend_admin/src/App.tsx index e73148b..6a380c1 100644 --- a/frontend_admin/src/App.tsx +++ b/frontend_admin/src/App.tsx @@ -2,10 +2,17 @@ import "./App.css"; import Layout from "./layout/Layout"; import { useUsers } from "./utils/useUsers"; import UserTable from "./components/UserTable"; +import { useEffect } from "react"; +import { loadTheme } from "./utils/functions"; +import { replaceUsers } from "./utils/functions"; function App() { const users = useUsers(); + useEffect(() => { + loadTheme(); + }, []); + return ( diff --git a/frontend_admin/src/components/Header.tsx b/frontend_admin/src/components/Header.tsx index 21cb261..1084ce2 100644 --- a/frontend_admin/src/components/Header.tsx +++ b/frontend_admin/src/components/Header.tsx @@ -2,6 +2,7 @@ import { useState } from "react"; import React from "react"; import LoginCard from "./LoginCard"; import { greeting } from "../utils/functions"; +import { changeTheme } from "../utils/functions"; const Header: React.FC = () => { const [loginCardVisible, setLoginCardVisible] = useState(false); @@ -22,6 +23,9 @@ const Header: React.FC = () => {