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:
+29
-2
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user