Adiciona possibilidade de editar e salvar informações na base de conhecimento adquirido

This commit is contained in:
2026-05-28 18:06:51 +00:00
parent 0b7891dbf9
commit 6993c3e330
2 changed files with 78 additions and 4 deletions
+65 -4
View File
@@ -222,18 +222,79 @@ const initApp = () => {
}
knowledgeList.innerHTML = window.conhecimento.facts.map(f => `
<div class="knowledge-item" data-id="${f.id}">
<div class="knowledge-item-content">
<div class="knowledge-item-content" id="knowledge-content-${f.id}">
<span class="knowledge-source">${f.source}</span>
<p>${f.content}</p>
<small>${new Date(f.addedAt).toLocaleDateString('pt-BR')}</small>
<p style="margin: 4px 0 0 0;">${f.content}</p>
<small style="display: block; margin-top: 4px;">${new Date(f.addedAt).toLocaleDateString('pt-BR')}</small>
</div>
<div class="knowledge-actions" id="knowledge-actions-${f.id}" style="display: flex; gap: 4px; align-self: flex-start; margin-left: 8px;">
<button class="knowledge-edit-btn" onclick="startEditKnowledge(${f.id})" title="Editar" style="background: none; border: none; color: var(--text-secondary); cursor: pointer; padding: 2px 4px; font-size: 0.9rem; transition: color 0.2s;">✏️</button>
<button class="knowledge-delete-btn" onclick="deleteKnowledge(${f.id})" title="Excluir">×</button>
</div>
<button class="knowledge-delete-btn" onclick="deleteKnowledge(${f.id})">×</button>
</div>
`).join('');
};
// Iniciar edição de item de conhecimento
window.startEditKnowledge = (id) => {
const fact = window.conhecimento.facts.find(f => f.id === id);
if (!fact) return;
const contentDiv = document.getElementById(`knowledge-content-${id}`);
const actionsDiv = document.getElementById(`knowledge-actions-${id}`);
if (!contentDiv) return;
if (actionsDiv) actionsDiv.style.display = 'none';
contentDiv.innerHTML = `
<span class="knowledge-source">${fact.source}</span>
<textarea id="edit-textarea-${id}" style="width: 100%; min-height: 70px; padding: 8px; border-radius: 6px; border: 1px solid var(--brand-green); background: var(--bg-primary); color: var(--text-primary); font-family: var(--font-sans); font-size: 0.9rem; resize: vertical; margin: 6px 0; outline: none; box-shadow: 0 0 4px var(--brand-glow);">${fact.content}</textarea>
<div style="display: flex; gap: 8px; margin-top: 4px;">
<button onclick="saveEditKnowledge(${id})" style="background: var(--brand-green); border: none; color: white; padding: 4px 10px; border-radius: 6px; font-size: 0.75rem; cursor: pointer; font-weight: 500; transition: opacity 0.2s;">Salvar</button>
<button onclick="renderKnowledgeList()" style="background: var(--bg-tertiary); border: none; color: var(--text-primary); padding: 4px 10px; border-radius: 6px; font-size: 0.75rem; cursor: pointer; font-weight: 500; transition: opacity 0.2s;">Cancelar</button>
</div>
`;
const textarea = document.getElementById(`edit-textarea-${id}`);
if (textarea) {
textarea.focus();
textarea.setSelectionRange(textarea.value.length, textarea.value.length);
}
};
// Salvar edição de item de conhecimento
window.saveEditKnowledge = async (id) => {
const textarea = document.getElementById(`edit-textarea-${id}`);
if (!textarea) return;
const newContent = textarea.value.trim();
if (!newContent) {
alert('O conteúdo do conhecimento não pode ser vazio.');
return;
}
try {
const res = await fetch(`/api/conhecimento/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content: newContent })
});
if (res.ok) {
const fact = window.conhecimento.facts.find(f => f.id === id);
if (fact) fact.content = newContent;
renderKnowledgeList();
} else {
const err = await res.json();
alert('Erro ao salvar conhecimento: ' + err.error);
}
} catch (e) {
console.error('Erro ao atualizar conhecimento:', e);
alert('Erro de conexão ao salvar conhecimento.');
}
};
// Deletar item de conhecimento (função global para onclick)
window.deleteKnowledge = async (id) => {
if (!confirm('Deseja realmente remover este conhecimento aprendido?')) return;
try {
const res = await fetch(`/api/conhecimento/${id}`, { method: 'DELETE' });
if (res.ok) {
+13
View File
@@ -185,6 +185,19 @@ app.delete('/api/conhecimento/:id', requireAuth, async (req, res) => {
res.json({ success: true });
});
app.put('/api/conhecimento/:id', requireAuth, async (req, res) => {
const { content } = req.body;
if (!content) return res.status(400).json({ error: 'Conteúdo é obrigatório' });
try {
const id = parseInt(req.params.id);
await dbPool.query("UPDATE escola.conhecimento SET content = $1 WHERE id = $2;", [content, id]);
res.json({ success: true });
} catch (err) {
console.error('Erro ao atualizar conhecimento:', err);
res.status(500).json({ error: err.message });
}
});
// Analisar mensagem final e extrair conhecimento aprendido (chamado pelo frontend)
app.post('/api/conhecimento/extrair', requireAuth, async (req, res) => {
const { message } = req.body;