From f226c3f1445a5990b28706174b805fe01f2c2df6 Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 8 Oct 2026 21:25:35 +0300 Subject: [PATCH] lessons: auto-fill text from last report by topic in lessonModal When user enters a topic in the lesson modal, fetch the latest report with that topic via /api/lesson-reports/by-topic. If the text field is not empty, show a confirmation before replacing. On confirm, populate lessonText and uncheck lessonAiCheck. --- public/admin.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/public/admin.js b/public/admin.js index 443f98c..40533b0 100644 --- a/public/admin.js +++ b/public/admin.js @@ -711,7 +711,35 @@ function ensureLessonModal() { closeLessonModal(); openLessonModal({ id }); }); - modal.querySelector('#lessonForm').addEventListener('submit', e => submitLessonModal(e)); + modal.querySelector('#lessonForm').addEventListener('submit', e => submitLessonModal(e)); + + let topicDebounceTimer = null; + modal.querySelector('#lessonTopic').addEventListener('input', () => { + clearTimeout(topicDebounceTimer); + topicDebounceTimer = setTimeout(async () => { + const topicEl = modal.querySelector('#lessonTopic'); + const topic = topicEl.value.trim(); + if (!topic) return; + const textEl = modal.querySelector('#lessonText'); + const aiCheckEl = modal.querySelector('#lessonAiCheck'); + const existingText = textEl.value.trim(); + try { + const res = await fetch(`${API}/api/lesson-reports/by-topic?topic=${encodeURIComponent(topic)}`, { headers: hdr() }); + if (!res.ok) return; + const data = await res.json(); + const report = data?.report; + if (!report || report.topic !== topic) return; + if (existingText) { + const replace = confirm(`Заменить текст последним отчётом по теме "${topic}"?`); + if (!replace) return; + } + textEl.value = report.text || ''; + textEl.dispatchEvent(new Event('input')); + if (aiCheckEl) aiCheckEl.checked = false; + } catch {} + }, 400); + }); + return modal; } @@ -1013,7 +1041,6 @@ async function openLessonModal(opts = {}) { renderIcons(); setTimeout(() => modal.querySelector('#lessonText').focus(), 50); } - function closeLessonModal() { const modal = document.getElementById('lessonModal'); if (modal) modal.classList.remove('open');