From 02fbfb033d9c6bf6a5e99eca7a55ca67378cbc61 Mon Sep 17 00:00:00 2001 From: araemer Date: Sun, 7 Dec 2025 07:35:22 +0100 Subject: [PATCH] Fix user selection --- .../src/components/users/UserEditForm.tsx | 21 +++++++++++++++++-- frontend/src/components/users/UserList.tsx | 6 +++++- .../components/users/UserManagementPage.tsx | 10 +++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/users/UserEditForm.tsx b/frontend/src/components/users/UserEditForm.tsx index b2114fb..06b0c4f 100644 --- a/frontend/src/components/users/UserEditForm.tsx +++ b/frontend/src/components/users/UserEditForm.tsx @@ -1,4 +1,4 @@ -import {useState} from "react"; +import {useEffect, useState} from "react"; import type {UserDto} from "../../api/dtos/UserDto"; import Button from "../basics/Button"; import {ButtonType} from "../basics/BasicButtonDefinitions"; @@ -33,6 +33,21 @@ export default function UserEditForm({ const [confirmPassword, setConfirmPassword] = useState(""); const [passwordError, setPasswordError] = useState(""); + /** + * React to changes in selected user. + * + * When a new user is selected, the edit user must be updated and password fields as well as error messege + * must be reset. + */ + useEffect(() => { + setEditedUser({...user}); + setPassword(""); + setConfirmPassword(""); + setPasswordError(""); + }, [user]); + /** + * Calls on save after validating passwords for new user. + */ const handleSave = async () => { if (!editedUser.id) { // New user - validate passwords @@ -52,7 +67,9 @@ export default function UserEditForm({ } }; - // @todo API string + /** + * All available roles + */ const roleOptions = UserRoleHelper.getRoleOptions(); return ( diff --git a/frontend/src/components/users/UserList.tsx b/frontend/src/components/users/UserList.tsx index c888a0a..51f705e 100644 --- a/frontend/src/components/users/UserList.tsx +++ b/frontend/src/components/users/UserList.tsx @@ -11,6 +11,7 @@ type UserListProps = { * UserList - Displays a list of users with selection */ export default function UserList({users, selectedUser, onSelectUser}: UserListProps) { + const formatUserName = (user: UserDto) => { const parts = []; if (user.lastName) parts.push(user.lastName); @@ -29,7 +30,10 @@ export default function UserList({users, selectedUser, onSelectUser}: UserListPr "px-4 py-3 cursor-pointer hover:bg-gray-200 transition-colors border-b border-gray-300", selectedUser?.id === user.id && "bg-blue-100 hover:bg-blue-200" )} - onClick={() => onSelectUser(user)} + onClick={() => { + console.log("selecting user", user.userName); + onSelectUser(user) + }} >
(null); - //@todo API enum const isAdmin = currentUser?.role === UserRole.ADMIN; // Load current user and user list (if admin) @@ -47,6 +46,12 @@ export default function UserManagementPage() { loadData(); }, []); + /** + * Load data for user management page + * + * An admin can see all users while a normal user can only see his own user data. + * Initially, the current user is selected. + */ const loadData = async () => { try { const me = await fetchCurrentUser(); @@ -133,7 +138,7 @@ export default function UserManagementPage() { setIsPasswordModalOpen(true); }; - /** Handles creating a new user (admin only) */ + /** Add new empty user */ const handleAddUser = () => { setSelectedUser({ userName: "", @@ -158,6 +163,7 @@ export default function UserManagementPage() { buttonType={ButtonType.PrimaryButton} /> )} + {/* Leave user management and return to recipe list. @todo handle unsaved changes?*/}