diff --git a/static/js/pages/irr_tools.js b/static/js/pages/irr_tools.js new file mode 100644 index 0000000..712ee26 --- /dev/null +++ b/static/js/pages/irr_tools.js @@ -0,0 +1,122 @@ +document.addEventListener('DOMContentLoaded', function () { + const saveBtn = document.getElementById('saveBtn'); + const runBtn = document.getElementById('runBtn'); + const logBtn = document.getElementById('logBtn'); + const asnsContent = document.getElementById('asnsContent'); + const logOutput = document.getElementById('logOutput'); + const statusMessage = document.getElementById('statusMessage'); + + let logPollInterval = null; + let runBtnOriginalHtml = runBtn.innerHTML; + + function showLoading(button) { + button.disabled = true; + button.innerHTML = ' Loading...'; + } + + function hideLoading(button, originalHtml) { + button.disabled = false; + button.innerHTML = originalHtml; + } + + function showStatus(message, isError = false) { + statusMessage.textContent = message; + statusMessage.className = isError ? 'alert alert-danger' : 'alert alert-success'; + statusMessage.style.display = 'block'; + } + + function showLog(message) { + logOutput.textContent = message; + logOutput.scrollTop = logOutput.scrollHeight; + } + + async function fetchLog() { + try { + const response = await fetch('/irr-tools/log'); + const result = await response.json(); + + if (result.log || result.log === "") { + showLog(result.log || "[Log file is empty. Waiting for data...]"); + + if (result.log.includes("IRR filter update process finished.")) { + stopLogPoller(); + showStatus('IRR Update Complete.', false); + } + } else { + showStatus(`Error retrieving log: ${result.error || 'Unknown error'}`, true); + stopLogPoller(); + } + } catch (e) { + showStatus(`Client-side error: ${e.message}`, true); + stopLogPoller(); + } + } + + function stopLogPoller() { + if (logPollInterval) { + clearInterval(logPollInterval); + logPollInterval = null; + } + hideLoading(runBtn, runBtnOriginalHtml); + } + + saveBtn.addEventListener('click', async () => { + const originalHtml = showLoading(saveBtn); + showLog('Saving asns.txt...'); + + try { + const response = await fetch('/irr-tools/save', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ content: asnsContent.value }) + }); + const result = await response.json(); + + if (result.success) { + showStatus(`Success: ${result.message}`); + showLog(`asns.txt saved.\n\n${asnsContent.value}`); + } else { + showStatus(`Error saving: ${result.error || 'Unknown error'}`, true); + } + } catch (e) { + showStatus(`Client-side fout: ${e.message}`, true); + } + + hideLoading(saveBtn, originalHtml); + }); + + runBtn.addEventListener('click', async () => { + showLoading(runBtn); + stopLogPoller(); + showStatus('Starting IRR update script...'); + showLog('Log is being cleared and script is starting...'); + + try { + const response = await fetch('/irr-tools/run', { method: 'POST' }); + const result = await response.json(); + + if (result.success) { + showStatus(result.message + " Log is updated live."); + logPollInterval = setInterval(fetchLog, 2000); + } else { + showStatus(`Error during startup: ${result.error || 'Unknown error'}`, true); + hideLoading(runBtn, runBtnOriginalHtml); + } + } catch (e) { + showStatus(`Client-side fout: ${e.message}`, true); + hideLoading(runBtn, runBtnOriginalHtml); + } + }); + + logBtn.addEventListener('click', async () => { + stopLogPoller(); + showStatus('Manually retrieve log...'); + showLog('Retrieving log...'); + + await fetchLog(); + + if (!logPollInterval) { + showStatus('Log from last run retrieved.'); + } + }); +}); \ No newline at end of file