Skip to content

Commit 047873b

Browse files
committed
style: Implement critical fixes to ensure Admin users are never locked and can always change branches
1 parent 0186eb3 commit 047873b

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/components/Layout/Navbar.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ const Navbar = () => {
4040
};
4141

4242
// Extra safety: Admin should NEVER be locked, regardless of isLocked value
43-
const shouldShowDropdown = user?.role === 'Admin' || !isLocked;
43+
// CRITICAL FIX: Force dropdown for Admin role, even if isLocked is incorrectly true
44+
const isAdmin = user?.role === 'Admin';
45+
const shouldShowDropdown = isAdmin || !isLocked;
4446

4547
console.log('🎨 Navbar Render Decision:', {
4648
userRole: user?.role,
47-
isAdmin: user?.role === 'Admin',
49+
isAdmin: isAdmin,
4850
isLocked: isLocked,
49-
shouldShowDropdown: shouldShowDropdown
51+
shouldShowDropdown: shouldShowDropdown,
52+
forceAdminDropdown: isAdmin ? 'YES - Admin always gets dropdown' : 'NO'
5053
});
5154

5255
return (

src/context/BranchContext.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,24 @@ export const BranchProvider = ({ children }) => {
2222
useEffect(() => {
2323
if (user) {
2424
console.log('🔍 BranchContext - User logged in:', user.username);
25+
console.log('🔍 BranchContext - User role:', user.role, '(type:', typeof user.role, ')');
26+
console.log('🔍 BranchContext - User branch_id:', user.branch_id, '(type:', typeof user.branch_id, ')');
27+
console.log('🔍 BranchContext - Is Admin check:', user.role === 'Admin', user.role === 'admin');
2528

2629
// Check if user has a branch_id (could be string or number)
2730
const hasBranchId = user.branch_id !== null && user.branch_id !== undefined && user.branch_id !== '';
2831

29-
// If user is not Admin and has a branch_id, lock them to their branch
32+
// CRITICAL: Admin should NEVER be locked, even if they have a branch_id
3033
if (user.role !== 'Admin' && hasBranchId) {
3134
console.log(`🔒 User locked to branch ${user.branch_id}`);
3235
// Convert to number if it's a string
3336
const branchId = typeof user.branch_id === 'string' ? parseInt(user.branch_id, 10) : user.branch_id;
3437
setSelectedBranchId(branchId);
3538
} else if (user.role === 'Admin') {
36-
console.log('👑 Admin user - can access all branches');
39+
console.log('👑 Admin user - can access all branches (branch_id ignored)');
40+
setSelectedBranchId('All');
41+
} else {
42+
console.log('🌐 User has no branch restriction - showing all branches');
3743
setSelectedBranchId('All');
3844
}
3945
} else {
@@ -70,6 +76,13 @@ export const BranchProvider = ({ children }) => {
7076

7177
// Prevent non-admin users from changing branch
7278
const handleSetSelectedBranchId = (branchId) => {
79+
// CRITICAL FIX: Admin should ALWAYS be able to change branch
80+
if (user && user.role === 'Admin') {
81+
console.log('👑 Admin changing branch to:', branchId);
82+
setSelectedBranchId(branchId);
83+
return;
84+
}
85+
7386
const hasBranchId = user?.branch_id !== null && user?.branch_id !== undefined && user?.branch_id !== '';
7487

7588
if (user && user.role !== 'Admin' && hasBranchId) {
@@ -81,8 +94,9 @@ export const BranchProvider = ({ children }) => {
8194
};
8295

8396
// Calculate isLocked based on current user state
97+
// CRITICAL FIX: Admin should NEVER be locked, regardless of branch_id
8498
const hasBranchId = user?.branch_id !== null && user?.branch_id !== undefined && user?.branch_id !== '';
85-
const isLocked = user && user.role !== 'Admin' && hasBranchId;
99+
const isLocked = user && user.role !== 'Admin' && hasBranchId; // Admin is never locked
86100
const isNotAdmin = user ? user.role !== 'Admin' : false;
87101

88102
// Find the selected branch with proper type comparison

0 commit comments

Comments
 (0)