diff --git a/frontend/js/textbook-tracker.js b/frontend/js/textbook-tracker.js index fdb8f0b..a144bdb 100644 --- a/frontend/js/textbook-tracker.js +++ b/frontend/js/textbook-tracker.js @@ -31,26 +31,32 @@ let syncPending = false; let pendingExtra = null; function syncToServer(extra) { - if (typeof LS === 'undefined' || !LS.getToken || !LS.getToken()) return; + if (typeof LS === 'undefined' || !LS.getToken) { console.warn('[tracker] LS не загружен — пропускаем sync'); return; } + if (!LS.getToken()) { console.warn('[tracker] нет токена в localStorage — пользователь не залогинен'); return; } if (syncPending) { pendingExtra = Object.assign(pendingExtra || {}, extra || {}); return; } syncPending = true; + const body = JSON.stringify({ last_para: localState.last, ...(extra || {}) }); + console.log('[tracker]', slug, '→ POST', body); fetch('/api/textbooks/' + slug + '/progress', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + LS.getToken(), }, - body: JSON.stringify({ last_para: localState.last, ...(extra || {}) }), + body, + }).then(r => { + console.log('[tracker]', slug, '← HTTP', r.status); + if (!r.ok) r.text().then(t => console.warn('[tracker] ошибка:', t)); }).finally(() => { syncPending = false; if (pendingExtra) { const next = pendingExtra; pendingExtra = null; syncToServer(next); } - }).catch(() => {}); + }).catch(e => console.warn('[tracker] fetch упал:', e)); } /* ── 2. Initial load: merge server data into local state + push back ── @@ -224,6 +230,7 @@ const pill = e.target.closest('.para-pill[data-para]'); if (!pill) return; const key = pill.dataset.para; + console.log('[tracker] клик по пилюле', key); localState.last = key; if (!localState.read.includes(key)) { localState.read.push(key); @@ -456,6 +463,7 @@ } function boot() { + console.log('[tracker] boot, slug =', slug, '| LS:', typeof LS !== 'undefined', '| token:', typeof LS !== 'undefined' && LS.getToken && !!LS.getToken()); injectStyles(); installBackButton(); installBookmarksBtn();