From 8fb8f5e337e8152a8d28f958362ec1503e9ee8cd Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sat, 1 Aug 2026 19:09:24 +0100 Subject: [PATCH] Refactor UserProfilePopover and ActionsContext for improved state management and action handling - Integrated useEffect in UserProfilePopover to close the popover on route changes, enhancing user experience. - Simplified modal object data handling in ActionsContext to ensure consistent user profile access. - Updated User model action handling to correctly pass user data to the NewAppPassword component. - Streamlined dropdown action rendering in UserProfilePopover, improving code clarity and maintainability. --- .../Dashboard/common/UserProfilePopover.jsx | 67 ++++++++----------- .../Dashboard/context/ActionsContext.jsx | 2 +- src/database/models/User.js | 19 +++--- src/utils/modelActions.js | 1 - 4 files changed, 39 insertions(+), 50 deletions(-) diff --git a/src/components/Dashboard/common/UserProfilePopover.jsx b/src/components/Dashboard/common/UserProfilePopover.jsx index 570884b..a932b4a 100644 --- a/src/components/Dashboard/common/UserProfilePopover.jsx +++ b/src/components/Dashboard/common/UserProfilePopover.jsx @@ -1,13 +1,13 @@ import PropTypes from 'prop-types' -import { createElement } from 'react' -import { Flex, Typography, Button, Space, Dropdown, Divider, Card } from 'antd' +import { createElement, useContext, useEffect } from 'react' +import { Flex, Typography, Button, Space, Divider, Card } from 'antd' import { UserOutlined } from '@ant-design/icons' -import { useContext } from 'react' -import { useNavigate } from 'react-router-dom' +import { useNavigate, useLocation } from 'react-router-dom' import LogoutIcon from '../../Icons/LogoutIcon' import { User } from '../../../database/models/User' import { buildActionUrl } from '../../../utils/modelActions' import { AuthContext } from '../context/AuthContext' +import ObjectActions from './ObjectActions' const { Text } = Typography @@ -16,17 +16,20 @@ const ICON_ACTION_NAMES = ['info', 'edit'] const UserProfilePopover = ({ onClose }) => { const { userProfile, profileImageUrl, logout } = useContext(AuthContext) const navigate = useNavigate() + const location = useLocation() const modelActions = User.actions || [] const iconActions = modelActions.filter((a) => ICON_ACTION_NAMES.includes(a.name) ) - const dropdownActions = modelActions.filter( - (a) => !ICON_ACTION_NAMES.includes(a.name) - ) const objectData = { ...userProfile, _user: userProfile } + useEffect(() => { + onClose?.() + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [location.pathname, location.search]) + const runAction = (action) => { if (action.name === 'logout') { logout() @@ -50,36 +53,6 @@ const UserProfilePopover = ({ onClose }) => { .join(' ') const email = userProfile?.email - const dropdownItems = [ - ...dropdownActions.map((action) => ({ - key: action.name, - label: action.label, - icon: action.icon ? createElement(action.icon) : undefined, - disabled: isActionDisabled(action), - onClick: () => !isActionDisabled(action) && runAction(action) - })), - { type: 'divider' }, - { - key: 'logout', - label: 'Logout', - icon: createElement(LogoutIcon), - onClick: () => runAction({ name: 'logout' }) - } - ] - - const actionButton = - dropdownItems.length > 1 ? ( - - - - ) : null - return ( { size='small' icon={action.icon ? createElement(action.icon) : undefined} onClick={() => runAction(action)} + disabled={isActionDisabled(action)} aria-label={action.label} /> ))} - {actionButton} +