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.
This commit is contained in:
dev
2026-10-08 21:25:35 +03:00
parent f07f15cdb0
commit f226c3f144
+29 -2
View File
@@ -711,7 +711,35 @@ function ensureLessonModal() {
closeLessonModal(); closeLessonModal();
openLessonModal({ id }); 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; return modal;
} }
@@ -1013,7 +1041,6 @@ async function openLessonModal(opts = {}) {
renderIcons(); renderIcons();
setTimeout(() => modal.querySelector('#lessonText').focus(), 50); setTimeout(() => modal.querySelector('#lessonText').focus(), 50);
} }
function closeLessonModal() { function closeLessonModal() {
const modal = document.getElementById('lessonModal'); const modal = document.getElementById('lessonModal');
if (modal) modal.classList.remove('open'); if (modal) modal.classList.remove('open');