diff --git a/components/Layout.tsx b/components/Layout.tsx index c2d3949..833ceff 100644 --- a/components/Layout.tsx +++ b/components/Layout.tsx @@ -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,20 +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(USERS[1]); // Default to standard user fallback + const [currentUser, setCurrentUser] = useState(null); useEffect(() => { - const storedUserId = localStorage.getItem('ctms_user_id'); - if (storedUserId) { - const user = USERS.find(u => u.id === storedUserId); - if (user) { - setCurrentUser(user); + const fetchCurrentUser = async () => { + const storedUserId = localStorage.getItem('ctms_user_id'); + if (!storedUserId) { + navigate('/login'); + return; } - } - }, []); + + try { + const user = await getUserById(storedUserId); + if (user) { + setCurrentUser(user); + } else { + navigate('/login'); + } + } catch (err) { + console.error("Layout fetch failed:", err); + } + }; + fetchCurrentUser(); + }, [navigate]); const handleLogout = () => { localStorage.removeItem('ctms_user_id'); + localStorage.removeItem('ctms_tenant_id'); navigate('/login'); }; @@ -52,6 +65,8 @@ export const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) => return 'CTMS'; }; + if (!currentUser) return null; + const isSuperAdmin = currentUser.role === 'super_admin'; return ( diff --git a/pages/Login.tsx b/pages/Login.tsx index 3884325..c4d8ab2 100644 --- a/pages/Login.tsx +++ b/pages/Login.tsx @@ -1,7 +1,7 @@ import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Hexagon, Lock, Mail, ArrowRight, Loader2, Info } from 'lucide-react'; -import { USERS } from '../constants'; +import { getUsers } from '../services/dataService'; export const Login: React.FC = () => { const navigate = useNavigate(); @@ -10,22 +10,22 @@ export const Login: React.FC = () => { const [password, setPassword] = useState('password'); const [error, setError] = useState(''); - const handleLogin = (e: React.FormEvent) => { + const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); setError(''); - // Simulate API call and validation - setTimeout(() => { - const user = USERS.find(u => u.email.toLowerCase() === email.toLowerCase()); + try { + // Fetch all users to find match (simplified auth for demo) + const users = await getUsers('all'); + const user = users.find(u => u.email.toLowerCase() === email.toLowerCase()); if (user) { - // Mock Login: Save to local storage localStorage.setItem('ctms_user_id', user.id); + localStorage.setItem('ctms_tenant_id', user.tenant_id || ''); setIsLoading(false); - // Redirect based on role if (user.role === 'super_admin') { navigate('/super-admin'); } else { @@ -33,9 +33,13 @@ export const Login: React.FC = () => { } } else { setIsLoading(false); - setError('Usuário não encontrado. Tente lidya@fasto.com ou root@system.com'); + setError('Usuário não encontrado.'); } - }, 1000); + } catch (err) { + console.error("Login error:", err); + setIsLoading(false); + setError('Erro ao conectar ao servidor.'); + } }; const fillCredentials = (type: 'admin' | 'super') => { diff --git a/pages/UserProfile.tsx b/pages/UserProfile.tsx index 8fc90a6..ae073ff 100644 --- a/pages/UserProfile.tsx +++ b/pages/UserProfile.tsx @@ -1,24 +1,33 @@ import React, { useState, useEffect } from 'react'; import { Camera, Save, Mail, User as UserIcon, Building, Shield, Loader2, CheckCircle2 } from 'lucide-react'; -import { USERS } from '../constants'; +import { getUserById } from '../services/dataService'; import { User } from '../types'; export const UserProfile: React.FC = () => { - // Simulating logged-in user state const [user, setUser] = useState(null); const [isLoading, setIsLoading] = useState(false); const [isSuccess, setIsSuccess] = useState(false); - // Form State const [name, setName] = useState(''); const [bio, setBio] = useState(''); useEffect(() => { - // Simulate fetching user data - const currentUser = USERS[0]; - setUser(currentUser); - setName(currentUser.name); - setBio(currentUser.bio || ''); + const fetchUser = async () => { + const storedId = localStorage.getItem('ctms_user_id'); + if (storedId) { + try { + const fetchedUser = await getUserById(storedId); + if (fetchedUser) { + setUser(fetchedUser); + setName(fetchedUser.name); + setBio(fetchedUser.bio || ''); + } + } catch (err) { + console.error("Error fetching profile:", err); + } + } + }; + fetchUser(); }, []); const handleSubmit = (e: React.FormEvent) => {