74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
// Caminho: app/(finance)/layout.tsx
|
|
|
|
"use client";
|
|
|
|
import type React from "react";
|
|
import { useState, useEffect } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
|
|
// Nossas importações centralizadas
|
|
import { usuariosApi } from "@/services/usuariosApi";
|
|
import DashboardLayout, { UserProfile } from "@/components/layout/DashboardLayout";
|
|
import { dashboardConfig } from "@/config/dashboard.config";
|
|
|
|
interface FinanceLayoutProps {
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
export default function FinanceLayout({ children }: FinanceLayoutProps) {
|
|
const [userProfile, setUserProfile] = useState<UserProfile | null>(null);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
const checkAuthentication = async () => {
|
|
try {
|
|
// 1. Busca o usuário logado via API
|
|
const userData = await usuariosApi.getCurrentUser();
|
|
|
|
// 2. Pega a configuração específica do "financeiro"
|
|
// Nota: No nosso config, a chave é 'financier'
|
|
const config = dashboardConfig.financier;
|
|
if (!config) {
|
|
throw new Error("Configuração para o perfil 'financier' não encontrada.");
|
|
}
|
|
|
|
// 3. Formata os dados para o perfil
|
|
setUserProfile(config.getUserProfile(userData));
|
|
|
|
} catch (error) {
|
|
// 4. Se falhar, redireciona para o login
|
|
console.error("Falha na autenticação para financeiro:", error);
|
|
router.push("/login");
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
checkAuthentication();
|
|
}, [router]);
|
|
|
|
// Enquanto a verificação estiver em andamento, mostra uma tela de carregamento
|
|
if (isLoading) {
|
|
return (
|
|
<div className="flex h-screen w-full items-center justify-center bg-background">
|
|
<p className="text-muted-foreground">Verificando autenticação...</p>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// Se não tiver perfil (redirect em andamento), não renderiza nada para evitar erros
|
|
if (!userProfile) {
|
|
return null;
|
|
}
|
|
|
|
// Pega os itens de menu da configuração
|
|
const menuItems = dashboardConfig.financier.menuItems;
|
|
|
|
// Renderiza o layout genérico com as props corretas
|
|
return (
|
|
<DashboardLayout menuItems={menuItems} userProfile={userProfile}>
|
|
{children}
|
|
</DashboardLayout>
|
|
);
|
|
} |