feat: implement real user login state and fix profile view
All checks were successful
Build and Deploy / build-and-push (push) Successful in 2m12s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 2m12s
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { NavLink, useLocation, useNavigate } from 'react-router-dom';
|
||||
import { LayoutDashboard, Users, UserCircle, Bell, Search, Menu, X, LogOut, Hexagon, Settings, Building2 } from 'lucide-react';
|
||||
import { USERS } from '../constants';
|
||||
import { getAttendances, getUsers, getUserById } from '../services/dataService';
|
||||
import { User } from '../types';
|
||||
|
||||
const SidebarItem = ({ to, icon: Icon, label, collapsed }: { to: string, icon: any, label: string, collapsed: boolean }) => (
|
||||
@@ -24,33 +24,33 @@ export const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) =>
|
||||
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
const [currentUser, setCurrentUser] = useState<User>(USERS[1]); // Default to standard user fallback
|
||||
const [currentUser, setCurrentUser] = useState<User | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const storedUserId = localStorage.getItem('ctms_user_id');
|
||||
if (storedUserId) {
|
||||
const user = USERS.find(u => u.id === storedUserId);
|
||||
const fetchUser = async () => {
|
||||
const storedUserId = localStorage.getItem('ctms_user_id');
|
||||
if (!storedUserId) {
|
||||
navigate('/login');
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await getUserById(storedUserId);
|
||||
if (user) {
|
||||
setCurrentUser(user);
|
||||
} else {
|
||||
navigate('/login');
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
};
|
||||
fetchUser();
|
||||
}, [navigate]);
|
||||
|
||||
const handleLogout = () => {
|
||||
localStorage.removeItem('ctms_user_id');
|
||||
localStorage.removeItem('ctms_tenant_id');
|
||||
navigate('/login');
|
||||
};
|
||||
|
||||
// Simple title mapping based on route
|
||||
const getPageTitle = () => {
|
||||
if (location.pathname === '/') return 'Dashboard';
|
||||
if (location.pathname.includes('/admin/users')) return 'Gestão de Equipe';
|
||||
if (location.pathname.includes('/users/')) return 'Histórico do Usuário';
|
||||
if (location.pathname.includes('/attendances')) return 'Detalhes do Atendimento';
|
||||
if (location.pathname.includes('/super-admin')) return 'Gestão de Organizações';
|
||||
if (location.pathname.includes('/profile')) return 'Meu Perfil';
|
||||
return 'CTMS';
|
||||
};
|
||||
if (!currentUser) return null; // Brief null return while navigating or fetching
|
||||
|
||||
const isSuperAdmin = currentUser.role === 'super_admin';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user