first commit
This commit is contained in:
commit
c9fb67e751
423 files changed
+53287
No files matched your search
@@ -0,0 +1,364 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { useParams, useNavigate, Link } from 'react-router-dom';
|
||||
import { DocumentIcon, PlusIcon, TrashIcon, EyeIcon, XMarkIcon, DocumentArrowDownIcon } from '@heroicons/react/24/outline';
|
||||
import { DocumentoEditor } from './documentos/DocumentoEditor';
|
||||
import { api } from '../lib/api';
|
||||
import { toast } from 'react-toastify';
|
||||
import { useAuth } from '../contexts/AuthContext';
|
||||
import { Dialog } from '@headlessui/react';
|
||||
import { DocumentoViewer } from './documentos/DocumentoViewer';
|
||||
|
||||
interface Documento {
|
||||
id: string;
|
||||
nome: string;
|
||||
conteudo: string;
|
||||
processoId?: string;
|
||||
atendimentoId?: string;
|
||||
criadoEm: string;
|
||||
}
|
||||
|
||||
interface DocumentoPayload {
|
||||
nome: string;
|
||||
conteudo: string;
|
||||
processoId?: string;
|
||||
atendimentoId?: string;
|
||||
}
|
||||
|
||||
export function DocumentosPage() {
|
||||
const [isEditorOpen, setIsEditorOpen] = useState(false);
|
||||
const [documentos, setDocumentos] = useState([]);
|
||||
const [modelos, setModelos] = useState([]);
|
||||
const [tabValue, setTabValue] = useState(0);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const { processoId, atendimentoId } = useParams();
|
||||
const navigate = useNavigate();
|
||||
const { user, signOut } = useAuth();
|
||||
const [isVisualizadorOpen, setVisualizadorOpen] = useState(false);
|
||||
const [documentoVisualizado, setDocumentoVisualizado] = useState(null);
|
||||
const [documentoEmEdicao, setDocumentoEmEdicao] = useState(null);
|
||||
|
||||
const loadData = useCallback(async () => {
|
||||
if (!user) {
|
||||
navigate('/login');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setIsLoading(true);
|
||||
await Promise.all([loadDocumentos(), loadModelos()]);
|
||||
} catch (error) {
|
||||
console.error('Erro ao carregar dados:', error);
|
||||
if (error?.response?.status === 401) {
|
||||
signOut(); // Usar a função do AuthContext
|
||||
navigate('/login');
|
||||
} else {
|
||||
toast.error('Erro ao carregar dados');
|
||||
}
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [user, navigate, signOut]); // Adicionar signOut nas dependências
|
||||
|
||||
useEffect(() => {
|
||||
loadData();
|
||||
}, [loadData, processoId, atendimentoId]);
|
||||
|
||||
const loadDocumentos = async () => {
|
||||
const params = {};
|
||||
if (processoId) params.processoId = processoId;
|
||||
if (atendimentoId) params.atendimentoId = atendimentoId;
|
||||
|
||||
const response = await api.get('/documentos', { params });
|
||||
setDocumentos(response.data);
|
||||
};
|
||||
|
||||
const loadModelos = async () => {
|
||||
const response = await api.get('/documentos-modelos');
|
||||
setModelos(response.data);
|
||||
};
|
||||
|
||||
const handleSave = async (data: DocumentoPayload) => {
|
||||
try {
|
||||
const payload = {
|
||||
nome: data.nome.trim(),
|
||||
conteudo: data.conteudo.trim(),
|
||||
tipo: documentoEmEdicao?.tipo || 'OUTROS',
|
||||
modeloId: documentoEmEdicao?.modeloId,
|
||||
processoId: data.processoId || processoId,
|
||||
atendimentoId: data.atendimentoId || atendimentoId
|
||||
};
|
||||
|
||||
if (documentoEmEdicao?.id) {
|
||||
await api.put(`/documentos/${documentoEmEdicao.id}`, payload);
|
||||
toast.success('Documento atualizado com sucesso');
|
||||
} else {
|
||||
await api.post('/documentos', payload);
|
||||
toast.success('Documento criado com sucesso');
|
||||
}
|
||||
|
||||
await loadDocumentos();
|
||||
setIsEditorOpen(false);
|
||||
setDocumentoEmEdicao(null);
|
||||
} catch (error: any) {
|
||||
console.error('Erro ao salvar documento:', error);
|
||||
toast.error(error?.response?.data?.message || 'Erro ao salvar documento');
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteDocumento = async (id) => {
|
||||
try {
|
||||
await api.delete(`/documentos/${id}`);
|
||||
toast.success('Documento excluído com sucesso');
|
||||
await loadDocumentos();
|
||||
} catch (error) {
|
||||
console.error('Erro ao excluir documento:', error);
|
||||
if (error?.response?.status === 401) {
|
||||
toast.error('Sessão expirada. Por favor, faça login novamente.');
|
||||
} else {
|
||||
toast.error('Erro ao excluir documento');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleVisualizar = async (doc) => {
|
||||
try {
|
||||
setDocumentoVisualizado(doc); // Simplificar para usar o documento diretamente
|
||||
setVisualizadorOpen(true);
|
||||
} catch (error) {
|
||||
console.error('Erro ao visualizar documento:', error);
|
||||
toast.error('Erro ao visualizar documento');
|
||||
}
|
||||
};
|
||||
|
||||
const handleExportPDF = async (documento) => {
|
||||
try {
|
||||
const configRes = await api.get('/documentos/configuracoes');
|
||||
const response = await api.post('/documentos/exportar-pdf', {
|
||||
nome: documento.nome,
|
||||
conteudo: documento.conteudo,
|
||||
escritorio: configRes.data
|
||||
}, { responseType: 'blob' });
|
||||
|
||||
// Criar URL do blob e fazer download
|
||||
const url = window.URL.createObjectURL(new Blob([response.data]));
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.setAttribute('download', `${documento.nome}.pdf`);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.parentNode.removeChild(link);
|
||||
window.URL.revokeObjectURL(url);
|
||||
|
||||
toast.success('PDF gerado com sucesso!');
|
||||
} catch (error) {
|
||||
console.error('Erro ao gerar PDF:', error);
|
||||
toast.error('Erro ao gerar PDF');
|
||||
}
|
||||
};
|
||||
|
||||
const handleEdit = (documento) => {
|
||||
// Garantir que todos os campos necessários estejam presentes
|
||||
setDocumentoEmEdicao({
|
||||
id: documento.id,
|
||||
nome: documento.nome,
|
||||
conteudo: documento.conteudo,
|
||||
tipo: documento.tipo || 'OUTROS',
|
||||
modeloId: documento.modeloId,
|
||||
processoId: documento.processoId || processoId,
|
||||
atendimentoId: documento.atendimentoId || atendimentoId
|
||||
});
|
||||
setVisualizadorOpen(false);
|
||||
setIsEditorOpen(true);
|
||||
};
|
||||
|
||||
const handleUseModelo = (modelo) => {
|
||||
setDocumentoEmEdicao({
|
||||
nome: modelo.nome,
|
||||
conteudo: modelo.conteudo,
|
||||
modeloId: modelo.id,
|
||||
tipo: 'OUTROS',
|
||||
processoId: processoId,
|
||||
atendimentoId: atendimentoId
|
||||
});
|
||||
setIsEditorOpen(true);
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return <div>Carregando...</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-6 space-y-6">
|
||||
<div className="flex justify-between items-center">
|
||||
<h1 className="text-2xl font-bold">Documentos</h1>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
onClick={() => setIsEditorOpen(true)}
|
||||
className="inline-flex items-center px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
<PlusIcon className="w-5 h-5 mr-2" />
|
||||
Novo Documento
|
||||
</button>
|
||||
{user?.perfil === 'ADVOGADO_ADMIN' && (
|
||||
<button
|
||||
onClick={() => navigate('/documentos/templates')}
|
||||
className="inline-flex items-center px-4 py-2 border border-blue-600 text-blue-600 rounded-md hover:bg-blue-50 transition-colors"
|
||||
>
|
||||
<DocumentIcon className="w-5 h-5 mr-2" />
|
||||
Gerenciar Templates
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="border-b border-gray-200">
|
||||
<nav className="flex -mb-px">
|
||||
<button
|
||||
onClick={() => setTabValue(0)}
|
||||
className={`px-4 py-2 font-medium text-sm border-b-2 transition-colors duration-200 ${
|
||||
tabValue === 0
|
||||
? 'border-blue-500 text-blue-600'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'
|
||||
}`}
|
||||
>
|
||||
Documentos
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setTabValue(1)}
|
||||
className={`ml-8 px-4 py-2 font-medium text-sm border-b-2 transition-colors duration-200 ${
|
||||
tabValue === 1
|
||||
? 'border-blue-500 text-blue-600'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'
|
||||
}`}
|
||||
>
|
||||
Modelos
|
||||
</button>
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
{tabValue === 0 && (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{documentos.map((doc) => (
|
||||
<div key={doc.id} className="bg-white dark:bg-gray-800 rounded-lg shadow-sm p-4">
|
||||
<div className="p-4 min-h-[180px] flex flex-col justify-between"> {/* Ajustado min-height e layout */}
|
||||
<div>
|
||||
<div className="flex items-center mb-3">
|
||||
<DocumentIcon className="w-6 h-6 text-gray-500 mr-2 flex-shrink-0" />
|
||||
<h3 className="font-semibold text-gray-900 dark:text-gray-100 line-clamp-2">{doc.nome}</h3>
|
||||
</div>
|
||||
<p className="text-sm text-gray-600 dark:text-gray-400">
|
||||
Criado em: {new Date(doc.criadoEm).toLocaleDateString()}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Botões em flex-wrap para melhor responsividade */}
|
||||
<div className="mt-4 flex flex-wrap gap-2">
|
||||
<button
|
||||
onClick={() => handleVisualizar(doc)}
|
||||
className="flex-1 min-w-[120px] inline-flex items-center justify-center px-3 py-1.5 text-sm text-blue-600 border border-blue-200 rounded-md hover:bg-blue-50"
|
||||
>
|
||||
<EyeIcon className="w-4 h-4 mr-1" />
|
||||
Visualizar
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleExportPDF(doc)}
|
||||
className="flex-1 min-w-[120px] inline-flex items-center justify-center px-3 py-1.5 text-sm text-blue-600 border border-blue-200 rounded-md hover:bg-blue-50"
|
||||
>
|
||||
<DocumentArrowDownIcon className="w-4 h-4 mr-1" />
|
||||
Exportar PDF
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleDeleteDocumento(doc.id)}
|
||||
className="flex-1 min-w-[120px] inline-flex items-center justify-center px-3 py-1.5 text-sm text-red-600 dark:text-red-400 border border-red-200 dark:border-red-900 rounded-md hover:bg-red-50 dark:hover:bg-red-900/20"
|
||||
>
|
||||
<TrashIcon className="w-4 h-4 mr-1" />
|
||||
Excluir
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{tabValue === 1 && (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{modelos.map((modelo) => (
|
||||
<div key={modelo.id} className="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 overflow-hidden">
|
||||
<div className="p-4">
|
||||
<div className="flex items-center mb-3">
|
||||
<DocumentIcon className="w-6 h-6 text-gray-500 mr-2" />
|
||||
<h3 className="font-semibold text-gray-900 dark:text-gray-100">{modelo.nome}</h3>
|
||||
</div>
|
||||
<p className="text-sm text-gray-600 dark:text-gray-400 mb-4">{modelo.descricao}</p>
|
||||
<button
|
||||
onClick={() => handleUseModelo(modelo)}
|
||||
className="w-full inline-flex items-center justify-center px-4 py-2 text-sm border border-blue-600 text-blue-600 rounded-md hover:bg-blue-50 dark:hover:bg-blue-900/20"
|
||||
>
|
||||
Usar Modelo
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Dialog
|
||||
open={isVisualizadorOpen}
|
||||
onClose={() => setVisualizadorOpen(false)}
|
||||
className="relative z-50"
|
||||
>
|
||||
<div className="fixed inset-0 bg-black/30" aria-hidden="true" />
|
||||
|
||||
<div className="fixed inset-0 flex items-center justify-center p-4">
|
||||
<Dialog.Panel className="w-full max-w-4xl bg-white rounded-lg shadow-xl">
|
||||
<div className="flex items-center justify-between p-4 border-b">
|
||||
<Dialog.Title className="text-lg font-semibold">
|
||||
{documentoVisualizado?.nome}
|
||||
</Dialog.Title>
|
||||
<button
|
||||
onClick={() => setVisualizadorOpen(false)}
|
||||
className="p-2 rounded hover:bg-gray-100"
|
||||
>
|
||||
<XMarkIcon className="w-6 h-6" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="p-6 max-h-[70vh] overflow-y-auto">
|
||||
<div
|
||||
className="prose max-w-none"
|
||||
dangerouslySetInnerHTML={{ __html: documentoVisualizado?.conteudo }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between p-4 border-t bg-gray-50">
|
||||
<div className="text-sm text-gray-500">
|
||||
Criado em: {documentoVisualizado && new Date(documentoVisualizado.criadoEm).toLocaleDateString()}
|
||||
</div>
|
||||
|
||||
<button
|
||||
onClick={() => handleEdit(documentoVisualizado)}
|
||||
className="px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700"
|
||||
>
|
||||
Editar
|
||||
</button>
|
||||
|
||||
</div>
|
||||
</Dialog.Panel>
|
||||
</div>
|
||||
</Dialog>
|
||||
|
||||
<DocumentoEditor
|
||||
isOpen={isEditorOpen}
|
||||
onClose={() => {
|
||||
setIsEditorOpen(false);
|
||||
setDocumentoEmEdicao(null);
|
||||
}}
|
||||
onSave={handleSave}
|
||||
processoId={processoId}
|
||||
atendimentoId={atendimentoId}
|
||||
documento={documentoEmEdicao} // Passando o documento em edição
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user