edit in the curriculum_PDF_uploader.html

parent ae47fc46
...@@ -105,68 +105,70 @@ ...@@ -105,68 +105,70 @@
<pre id="response" style="display:none;"></pre> <pre id="response" style="display:none;"></pre>
</div> </div>
<script> <script>
const API_URL = 'http://localhost:8000/process-curriculum'; // The API_URL constant is no longer needed and has been removed.
const pdfFileInput = document.getElementById('pdfFile'); const pdfFileInput = document.getElementById('pdfFile');
const uploadButton = document.getElementById('uploadButton'); const uploadButton = document.getElementById('uploadButton');
const statusDiv = document.getElementById('status'); const statusDiv = document.getElementById('status');
const responsePre = document.getElementById('response'); const responsePre = document.getElementById('response');
const gradeInput = document.getElementById('gradeInput'); const gradeInput = document.getElementById('gradeInput');
const subjectInput = document.getElementById('subjectInput'); // <-- Get the new subject field const subjectInput = document.getElementById('subjectInput');
uploadButton.addEventListener('click', async () => { uploadButton.addEventListener('click', async () => {
const selectedFile = pdfFileInput.files[0]; const selectedFile = pdfFileInput.files[0];
const grade = gradeInput.value; const grade = gradeInput.value;
const subject = subjectInput.value; // <-- Get the subject value const subject = subjectInput.value;
// --- Update validation --- if (!selectedFile) { showStatus('Please select a PDF file first.', 'error'); return; }
if (!selectedFile) { showStatus('Please select a PDF file first.', 'error'); return; } if (!grade) { showStatus('Please enter a grade.', 'error'); return; }
if (!grade) { showStatus('Please enter a grade.', 'error'); return; } if (!subject) { showStatus('Please enter a subject.', 'error'); return; }
if (!subject) { showStatus('Please enter a subject.', 'error'); return; }
const formData = new FormData(); const formData = new FormData();
formData.append('file', selectedFile); formData.append('file', selectedFile);
formData.append('grade', grade); formData.append('grade', grade);
formData.append('subject', subject); formData.append('subject', subject);
// 3. Update UI to show processing state showStatus('Uploading and starting background processing...', 'processing');
showStatus('Uploading and starting background processing...', 'processing'); uploadButton.disabled = true;
uploadButton.disabled = true; responsePre.style.display = 'none';
responsePre.style.display = 'none';
try { try {
// 4. Send the file AND grade to the API // --- THIS IS THE FIX ---
const response = await fetch(API_URL, { // We now `await` the fetch call and assign its result to `response`.
method: 'POST', const response = await fetch('/process-curriculum', {
body: formData, method: 'POST',
}); body: formData,
});
const responseData = await response.json(); // This line can now work correctly because the 'response' variable exists.
const responseData = await response.json();
// 5. Handle the server's response if (!response.ok) {
if (!response.ok) { throw new Error(responseData.detail || `Server error: ${response.statusText}`);
throw new Error(responseData.detail || `Server error: ${response.statusText}`);
}
showStatus('Success! The server has started processing your file in the background.', 'success');
responsePre.textContent = JSON.stringify(responseData, null, 2);
responsePre.style.display = 'block';
} catch (error) {
showStatus(`An error occurred: ${error.message}`, 'error');
} finally {
// 6. Re-enable the button
uploadButton.disabled = false;
} }
});
// Helper function to show status messages showStatus('Success! The server has started processing your file in the background.', 'success');
function showStatus(message, type) { responsePre.textContent = JSON.stringify(responseData, null, 2);
statusDiv.textContent = message; responsePre.style.display = 'block';
statusDiv.className = `status ${type}`;
statusDiv.style.display = 'block'; } catch (error) {
// Network errors or other exceptions will be caught here.
showStatus(`An error occurred: ${error.message}`, 'error');
} finally {
uploadButton.disabled = false;
} }
</script> });
// Helper function to show status messages
function showStatus(message, type) {
statusDiv.textContent = message;
statusDiv.className = `status ${type}`;
statusDiv.style.display = 'block';
}
</script>
</body> </body>
</html> </html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment