feat: expand admin and instructor tools
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
import { Loader2, RefreshCw, ShieldCheck, UserCheck, UserX, Users } from 'lucide-react';
|
||||
import { adminApi, ManagedUser } from '../services/api';
|
||||
import { adminApi, ManagedUser, ManagedUserDetail, PlatformAnalytics } from '../services/api';
|
||||
import { useAuth } from '../context/AuthContext';
|
||||
import { useToast } from '../context/ToastContext';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
@@ -19,12 +19,20 @@ export const SuperAdmin: React.FC = () => {
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [updatingUserId, setUpdatingUserId] = useState<string | null>(null);
|
||||
const [query, setQuery] = useState('');
|
||||
const [analytics, setAnalytics] = useState<PlatformAnalytics | null>(null);
|
||||
const [inviteEmail, setInviteEmail] = useState('');
|
||||
const [inviteRole, setInviteRole] = useState<'student' | 'instructor'>('student');
|
||||
const [accessLink, setAccessLink] = useState('');
|
||||
const [selectedUser, setSelectedUser] = useState<ManagedUserDetail | null>(null);
|
||||
const [loadingDetailId, setLoadingDetailId] = useState<string | null>(null);
|
||||
|
||||
const loadUsers = async () => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const response = await adminApi.listUsers();
|
||||
setUsers(response.data);
|
||||
const dashboard = await adminApi.dashboard();
|
||||
setAnalytics(dashboard.data);
|
||||
} catch {
|
||||
showToast('Não foi possível carregar os usuários.', 'error');
|
||||
} finally {
|
||||
@@ -59,6 +67,28 @@ export const SuperAdmin: React.FC = () => {
|
||||
|
||||
const activeUsers = users.filter((account) => account.isActive).length;
|
||||
const instructorUsers = users.filter((account) => account.role === 'instructor' && account.isActive).length;
|
||||
const createInvite = async (event: React.FormEvent) => {
|
||||
event.preventDefault();
|
||||
try {
|
||||
const response = await adminApi.invite(inviteEmail, inviteRole);
|
||||
setAccessLink(response.data.inviteUrl); setInviteEmail(''); showToast('Link de convite criado.', 'success');
|
||||
} catch { showToast('Não foi possível criar o convite.', 'error'); }
|
||||
};
|
||||
const resetPassword = async (account: ManagedUser) => {
|
||||
try { const response = await adminApi.passwordReset(account.id); setAccessLink(response.data.resetUrl); showToast('Link de redefinição criado.', 'success'); }
|
||||
catch { showToast('Não foi possível criar o link.', 'error'); }
|
||||
};
|
||||
const showUserDetail = async (account: ManagedUser) => {
|
||||
setLoadingDetailId(account.id);
|
||||
try {
|
||||
const response = await adminApi.userDetail(account.id);
|
||||
setSelectedUser(response.data);
|
||||
} catch {
|
||||
showToast('Não foi possível carregar os detalhes do usuário.', 'error');
|
||||
} finally {
|
||||
setLoadingDetailId(null);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<main className="min-h-screen pt-28 pb-20 px-6 md:px-12 max-w-[1440px] mx-auto">
|
||||
@@ -86,12 +116,42 @@ export const SuperAdmin: React.FC = () => {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-4 mb-8">
|
||||
<StatCard icon={<Users className="w-5 h-5" />} label="Usuários cadastrados" value={users.length} />
|
||||
<StatCard icon={<UserCheck className="w-5 h-5" />} label="Contas ativas" value={activeUsers} />
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 lg:grid-cols-5 gap-4 mb-8">
|
||||
<StatCard icon={<Users className="w-5 h-5" />} label="Usuários cadastrados" value={analytics?.totalUsers ?? users.length} />
|
||||
<StatCard icon={<UserCheck className="w-5 h-5" />} label="Contas ativas" value={analytics?.activeUsers ?? activeUsers} />
|
||||
<StatCard icon={<ShieldCheck className="w-5 h-5" />} label="Instrutores ativos" value={instructorUsers} />
|
||||
<StatCard icon={<Users className="w-5 h-5" />} label="Cursos publicados" value={analytics?.publishedCourses ?? 0} />
|
||||
<StatCard icon={<UserCheck className="w-5 h-5" />} label="Aulas concluídas" value={analytics?.completedLessons ?? 0} />
|
||||
</div>
|
||||
|
||||
<section className="rounded-2xl border border-white/10 bg-zinc-950/80 p-5 mb-8">
|
||||
<h2 className="text-lg font-bold text-white mb-4">Convidar usuário</h2>
|
||||
<form onSubmit={createInvite} className="flex flex-col md:flex-row gap-3">
|
||||
<input required type="email" value={inviteEmail} onChange={(event) => setInviteEmail(event.target.value)} placeholder="E-mail do convidado" className="flex-1 rounded-xl bg-zinc-900 border border-white/10 px-4 py-2.5 text-sm text-white" />
|
||||
<select value={inviteRole} onChange={(event) => setInviteRole(event.target.value as 'student' | 'instructor')} className="rounded-xl bg-zinc-900 border border-white/10 px-4 py-2.5 text-sm text-white"><option value="student">Aluno</option><option value="instructor">Instrutor</option></select>
|
||||
<button className="rounded-xl bg-orange-500 hover:bg-orange-600 px-5 py-2.5 text-sm font-semibold text-white">Gerar convite</button>
|
||||
</form>
|
||||
{accessLink && <div className="mt-4 flex gap-2"><input readOnly value={accessLink} className="flex-1 rounded-xl bg-black border border-white/10 px-3 py-2 text-xs text-gray-300" /><button onClick={() => navigator.clipboard.writeText(accessLink)} className="rounded-xl bg-white/10 px-4 text-xs font-semibold">Copiar</button></div>}
|
||||
</section>
|
||||
|
||||
{selectedUser && (
|
||||
<section className="rounded-2xl border border-orange-500/30 bg-zinc-950/80 p-5 mb-8">
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<div>
|
||||
<p className="text-xs uppercase tracking-wider text-orange-400 font-semibold">Detalhes do usuário</p>
|
||||
<h2 className="text-lg font-bold text-white mt-1">{selectedUser.name}</h2>
|
||||
<p className="text-sm text-gray-400">{selectedUser.email} · {roleLabel[selectedUser.role]}</p>
|
||||
</div>
|
||||
<button onClick={() => setSelectedUser(null)} className="text-xs text-gray-400 hover:text-white">Fechar</button>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-4 mt-5 text-sm">
|
||||
<DetailMetric label="Aulas concluídas" value={selectedUser.completedLessons.toString()} />
|
||||
<DetailMetric label="Última atividade" value={selectedUser.lastLearningAt ? new Intl.DateTimeFormat('pt-BR', { dateStyle: 'medium', timeStyle: 'short' }).format(new Date(selectedUser.lastLearningAt)) : 'Sem atividade'} />
|
||||
<DetailMetric label="Status" value={selectedUser.isActive ? 'Ativo' : 'Desativado'} />
|
||||
</div>
|
||||
</section>
|
||||
)}
|
||||
|
||||
<section className="rounded-2xl border border-white/10 bg-zinc-950/80 overflow-hidden">
|
||||
<div className="p-5 border-b border-white/10 flex flex-col sm:flex-row gap-4 sm:items-center sm:justify-between">
|
||||
<h2 className="text-lg font-bold text-white">Usuários</h2>
|
||||
@@ -116,6 +176,7 @@ export const SuperAdmin: React.FC = () => {
|
||||
<th className="px-5 py-4">Perfil</th>
|
||||
<th className="px-5 py-4">Status</th>
|
||||
<th className="px-5 py-4">Cadastro</th>
|
||||
<th className="px-5 py-4">Ações</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-white/5">
|
||||
@@ -151,6 +212,12 @@ export const SuperAdmin: React.FC = () => {
|
||||
</button>
|
||||
</td>
|
||||
<td className="px-5 py-4 text-xs text-gray-500">{new Intl.DateTimeFormat('pt-BR').format(new Date(account.createdAt))}</td>
|
||||
<td className="px-5 py-4">
|
||||
<div className="flex items-center gap-3">
|
||||
<button disabled={loadingDetailId === account.id} onClick={() => void showUserDetail(account)} className="text-xs font-semibold text-gray-300 hover:text-white disabled:opacity-50">{loadingDetailId === account.id ? 'Carregando...' : 'Detalhes'}</button>
|
||||
<button disabled={isUpdating} onClick={() => void resetPassword(account)} className="text-xs font-semibold text-orange-400 hover:text-orange-300">Redefinir senha</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
@@ -169,3 +236,10 @@ const StatCard: React.FC<{ icon: React.ReactNode; label: string; value: number }
|
||||
<div className="text-3xl font-bold text-white">{value}</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
const DetailMetric: React.FC<{ label: string; value: string }> = ({ label, value }) => (
|
||||
<div className="rounded-xl bg-white/[0.03] px-4 py-3">
|
||||
<div className="text-xs text-gray-500">{label}</div>
|
||||
<div className="text-sm font-semibold text-white mt-1">{value}</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user