feat: fix profile viewing and implement real user login state
All checks were successful
Build and Deploy / build-and-push (push) Successful in 2m47s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 2m47s
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import { NavLink, useLocation, useNavigate } from 'react-router-dom';
|
import { NavLink, useLocation, useNavigate } from 'react-router-dom';
|
||||||
import { LayoutDashboard, Users, UserCircle, Bell, Search, Menu, X, LogOut, Hexagon, Settings, Building2 } from 'lucide-react';
|
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';
|
import { User } from '../types';
|
||||||
|
|
||||||
const SidebarItem = ({ to, icon: Icon, label, collapsed }: { to: string, icon: any, label: string, collapsed: boolean }) => (
|
const SidebarItem = ({ to, icon: Icon, label, collapsed }: { to: string, icon: any, label: string, collapsed: boolean }) => (
|
||||||
@@ -24,16 +24,19 @@ export const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) =>
|
|||||||
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
|
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [currentUser, setCurrentUser] = useState<User>(USERS[1]); // Default to standard user fallback
|
const [currentUser, setCurrentUser] = useState<User | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const fetchCurrentUser = async () => {
|
||||||
const storedUserId = localStorage.getItem('ctms_user_id');
|
const storedUserId = localStorage.getItem('ctms_user_id');
|
||||||
if (storedUserId) {
|
if (storedUserId) {
|
||||||
const user = USERS.find(u => u.id === storedUserId);
|
const user = await getUserById(storedUserId);
|
||||||
if (user) {
|
if (user) {
|
||||||
setCurrentUser(user);
|
setCurrentUser(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
fetchCurrentUser();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleLogout = () => {
|
const handleLogout = () => {
|
||||||
@@ -41,16 +44,9 @@ export const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) =>
|
|||||||
navigate('/login');
|
navigate('/login');
|
||||||
};
|
};
|
||||||
|
|
||||||
// Simple title mapping based on route
|
if (!currentUser) {
|
||||||
const getPageTitle = () => {
|
return <div className="flex h-screen items-center justify-center bg-slate-50">Carregando...</div>;
|
||||||
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';
|
|
||||||
};
|
|
||||||
|
|
||||||
const isSuperAdmin = currentUser.role === 'super_admin';
|
const isSuperAdmin = currentUser.role === 'super_admin';
|
||||||
|
|
||||||
|
|||||||
@@ -40,10 +40,11 @@ export const Dashboard: React.FC = () => {
|
|||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
|
const tenantId = localStorage.getItem('ctms_tenant_id') || CURRENT_TENANT_ID;
|
||||||
// Fetch users and attendances in parallel
|
// Fetch users and attendances in parallel
|
||||||
const [fetchedUsers, fetchedData] = await Promise.all([
|
const [fetchedUsers, fetchedData] = await Promise.all([
|
||||||
getUsers(CURRENT_TENANT_ID),
|
getUsers(tenantId),
|
||||||
getAttendances(CURRENT_TENANT_ID, filters)
|
getAttendances(tenantId, filters)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
setUsers(fetchedUsers);
|
setUsers(fetchedUsers);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { Hexagon, Lock, Mail, ArrowRight, Loader2, Info } from 'lucide-react';
|
import { Hexagon, Lock, Mail, ArrowRight, Loader2, Info } from 'lucide-react';
|
||||||
import { USERS } from '../constants';
|
import { getUsers } from '../services/dataService';
|
||||||
|
|
||||||
export const Login: React.FC = () => {
|
export const Login: React.FC = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@@ -10,18 +10,20 @@ export const Login: React.FC = () => {
|
|||||||
const [password, setPassword] = useState('password');
|
const [password, setPassword] = useState('password');
|
||||||
const [error, setError] = useState('');
|
const [error, setError] = useState('');
|
||||||
|
|
||||||
const handleLogin = (e: React.FormEvent) => {
|
const handleLogin = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setError('');
|
setError('');
|
||||||
|
|
||||||
// Simulate API call and validation
|
try {
|
||||||
setTimeout(() => {
|
// Fetch all users to find match (simplified auth for demo)
|
||||||
const user = USERS.find(u => u.email.toLowerCase() === email.toLowerCase());
|
const users = await getUsers('all');
|
||||||
|
const user = users.find(u => u.email.toLowerCase() === email.toLowerCase());
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
// Mock Login: Save to local storage
|
// Mock Login: Save to local storage
|
||||||
localStorage.setItem('ctms_user_id', user.id);
|
localStorage.setItem('ctms_user_id', user.id);
|
||||||
|
localStorage.setItem('ctms_tenant_id', user.tenant_id);
|
||||||
|
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
|
||||||
@@ -33,9 +35,13 @@ export const Login: React.FC = () => {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
setError('Usuário não encontrado. Tente lidya@fasto.com ou root@system.com');
|
setError('Usuário não encontrado. Tente os e-mails cadastrados.');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Login error:", err);
|
||||||
|
setIsLoading(false);
|
||||||
|
setError('Erro ao conectar ao servidor.');
|
||||||
}
|
}
|
||||||
}, 1000);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const fillCredentials = (type: 'admin' | 'super') => {
|
const fillCredentials = (type: 'admin' | 'super') => {
|
||||||
|
|||||||
@@ -1,13 +1,27 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import { Users, Plus, MoreHorizontal, Mail, Shield, Search, X, Edit, Trash2, Save } from 'lucide-react';
|
import { Users, Plus, MoreHorizontal, Mail, Shield, Search, X, Edit, Trash2, Save } from 'lucide-react';
|
||||||
import { USERS } from '../constants';
|
import { getUsers } from '../services/dataService';
|
||||||
|
import { CURRENT_TENANT_ID } from '../constants';
|
||||||
import { User } from '../types';
|
import { User } from '../types';
|
||||||
|
|
||||||
export const TeamManagement: React.FC = () => {
|
export const TeamManagement: React.FC = () => {
|
||||||
const [users, setUsers] = useState<User[]>(USERS.filter(u => u.role !== 'super_admin')); // Default hide super admin from list
|
const [users, setUsers] = useState<User[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||||
const [searchTerm, setSearchTerm] = useState('');
|
const [searchTerm, setSearchTerm] = useState('');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchUsers = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
const tenantId = localStorage.getItem('ctms_tenant_id') || CURRENT_TENANT_ID;
|
||||||
|
const data = await getUsers(tenantId);
|
||||||
|
// Hide super admin from the list for standard admin view
|
||||||
|
setUsers(data.filter(u => u.role !== 'super_admin'));
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
fetchUsers();
|
||||||
|
}, []);
|
||||||
|
|
||||||
// State for handling Add/Edit
|
// State for handling Add/Edit
|
||||||
const [editingUser, setEditingUser] = useState<User | null>(null);
|
const [editingUser, setEditingUser] = useState<User | null>(null);
|
||||||
const [formData, setFormData] = useState({
|
const [formData, setFormData] = useState({
|
||||||
@@ -23,6 +37,10 @@ export const TeamManagement: React.FC = () => {
|
|||||||
u.email.toLowerCase().includes(searchTerm.toLowerCase())
|
u.email.toLowerCase().includes(searchTerm.toLowerCase())
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (loading && users.length === 0) {
|
||||||
|
return <div className="flex h-full items-center justify-center text-slate-400 p-12 text-center">Carregando equipe...</div>;
|
||||||
|
}
|
||||||
|
|
||||||
const getRoleBadge = (role: string) => {
|
const getRoleBadge = (role: string) => {
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case 'super_admin': return 'bg-slate-900 text-white border-slate-700';
|
case 'super_admin': return 'bg-slate-900 text-white border-slate-700';
|
||||||
|
|||||||
@@ -20,11 +20,12 @@ export const UserDetail: React.FC = () => {
|
|||||||
|
|
||||||
const loadData = async () => {
|
const loadData = async () => {
|
||||||
try {
|
try {
|
||||||
|
const tenantId = localStorage.getItem('ctms_tenant_id') || CURRENT_TENANT_ID;
|
||||||
const u = await getUserById(id);
|
const u = await getUserById(id);
|
||||||
setUser(u);
|
setUser(u);
|
||||||
|
|
||||||
if (u) {
|
if (u) {
|
||||||
const data = await getAttendances(CURRENT_TENANT_ID, {
|
const data = await getAttendances(tenantId, {
|
||||||
userId: id,
|
userId: id,
|
||||||
dateRange: { start: new Date(0), end: new Date() } // All time
|
dateRange: { start: new Date(0), end: new Date() } // All time
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import { Camera, Save, Mail, User as UserIcon, Building, Shield, Loader2, CheckCircle2 } from 'lucide-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';
|
import { User } from '../types';
|
||||||
|
|
||||||
export const UserProfile: React.FC = () => {
|
export const UserProfile: React.FC = () => {
|
||||||
@@ -14,11 +14,18 @@ export const UserProfile: React.FC = () => {
|
|||||||
const [bio, setBio] = useState('');
|
const [bio, setBio] = useState('');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Simulate fetching user data
|
const fetchUser = async () => {
|
||||||
const currentUser = USERS[0];
|
const storedId = localStorage.getItem('ctms_user_id');
|
||||||
setUser(currentUser);
|
if (storedId) {
|
||||||
setName(currentUser.name);
|
const fetchedUser = await getUserById(storedId);
|
||||||
setBio(currentUser.bio || '');
|
if (fetchedUser) {
|
||||||
|
setUser(fetchedUser);
|
||||||
|
setName(fetchedUser.name);
|
||||||
|
setBio(fetchedUser.bio || '');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
fetchUser();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleSubmit = (e: React.FormEvent) => {
|
const handleSubmit = (e: React.FormEvent) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user