// JavaScript code let accessToken = ""; console.log(accessToken) function showSection(sectionId) { document .querySelectorAll(".container > div") .forEach((div) => (div.style.display = "none")); document.getElementById(sectionId).style.display = "block"; } // Show default section on page load document.addEventListener("DOMContentLoaded", () => { showSection("defaultSection"); }); document .getElementById("loginLink") .addEventListener("click", () => showSection("loginForm")); document .getElementById("registerLink") .addEventListener("click", () => showSection("registerForm")); document .getElementById("uploadLink") .addEventListener("click", () => showSection("uploadForm")); document .getElementById("chatLink") .addEventListener("click", () => showSection("chatSection")); document.getElementById("historyLink").addEventListener("click", () => { showSection("historySection"); loadChatHistory(); }); document .getElementById("instituitionLink") .addEventListener("click", () => showSection("historyByInstituteSection")); function getCookie(name) { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(';').shift(); } // Extract JWT token from cookie and store it in accessToken accessToken = getCookie('jwt_token'); console.log(accessToken); async function register() { const username = document.getElementById("registerUsername").value; const password = document.getElementById("registerPassword").value; const institute = document.getElementById("registerInstitute").value; try { const response = await fetch("/register", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ username, password,institute }), }); const data = await response.json(); alert(data.msg); if (response.ok) { showSection("loginForm"); } } catch (error) { console.error("Error:", error); alert("An error occurred during registration"); } } async function login() { const username = document.getElementById("loginUsername").value; const password = document.getElementById("loginPassword").value; try { const response = await fetch("/login", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ username, password }), }); const data = await response.json(); if (response.ok) { accessToken = data.access_token; console.log(accessToken); alert("Login successful"); showSection("uploadForm"); } else { alert(data.msg); } } catch (error) { console.error("Error:", error); alert("An error occurred during login"); } } async function uploadFile() { const fileInput = document.getElementById("fileUpload"); const file = fileInput.files[0]; if (!file) { alert("Please select a file"); return; } const formData = new FormData(); formData.append("file", file); try { const response = await fetch("/upload", { method: "POST", headers: { Authorization: `Bearer ${accessToken}`, }, body: formData, }); const data = await response.json(); alert(data.msg); if (response.ok) { showSection("chatSection"); } } catch (error) { console.error("Error:", error); alert("An error occurred during file upload"); } } async function loadChatHistoryByInstitute() { const institute = document.getElementById("instituteInput").value; if (!institute) { alert("Please enter an institute"); return; } try { const response = await fetch( `/history_by_institute?institute=${encodeURIComponent(institute)}`, { headers: { Authorization: `Bearer ${accessToken}`, }, } ); const data = await response.json(); if (response.ok) { const historyList = document.getElementById("chatHistoryByInstituteList"); historyList.innerHTML = ""; data.history.forEach((entry) => { const historyItem = document.createElement("div"); historyItem.innerHTML = `
${ entry.username }
${entry.message}
${entry.response}
${entry.message}
${entry.response}
${message}