edit in the curriculum_PDF_uploader.html

parent ae47fc46
......@@ -105,68 +105,70 @@
<pre id="response" style="display:none;"></pre>
</div>
<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 uploadButton = document.getElementById('uploadButton');
const statusDiv = document.getElementById('status');
const responsePre = document.getElementById('response');
const gradeInput = document.getElementById('gradeInput');
const subjectInput = document.getElementById('subjectInput'); // <-- Get the new subject field
const pdfFileInput = document.getElementById('pdfFile');
const uploadButton = document.getElementById('uploadButton');
const statusDiv = document.getElementById('status');
const responsePre = document.getElementById('response');
const gradeInput = document.getElementById('gradeInput');
const subjectInput = document.getElementById('subjectInput');
uploadButton.addEventListener('click', async () => {
const selectedFile = pdfFileInput.files[0];
const grade = gradeInput.value;
const subject = subjectInput.value; // <-- Get the subject value
uploadButton.addEventListener('click', async () => {
const selectedFile = pdfFileInput.files[0];
const grade = gradeInput.value;
const subject = subjectInput.value;
// --- Update validation ---
if (!selectedFile) { showStatus('Please select a PDF file first.', 'error'); return; }
if (!grade) { showStatus('Please enter a grade.', 'error'); return; }
if (!subject) { showStatus('Please enter a subject.', 'error'); return; }
if (!selectedFile) { showStatus('Please select a PDF file first.', 'error'); return; }
if (!grade) { showStatus('Please enter a grade.', 'error'); return; }
if (!subject) { showStatus('Please enter a subject.', 'error'); return; }
const formData = new FormData();
formData.append('file', selectedFile);
formData.append('grade', grade);
formData.append('subject', subject);
const formData = new FormData();
formData.append('file', selectedFile);
formData.append('grade', grade);
formData.append('subject', subject);
// 3. Update UI to show processing state
showStatus('Uploading and starting background processing...', 'processing');
uploadButton.disabled = true;
responsePre.style.display = 'none';
showStatus('Uploading and starting background processing...', 'processing');
uploadButton.disabled = true;
responsePre.style.display = 'none';
try {
// 4. Send the file AND grade to the API
const response = await fetch(API_URL, {
method: 'POST',
body: formData,
});
try {
// --- THIS IS THE FIX ---
// We now `await` the fetch call and assign its result to `response`.
const response = await fetch('/process-curriculum', {
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) {
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;
if (!response.ok) {
throw new Error(responseData.detail || `Server error: ${response.statusText}`);
}
});
// Helper function to show status messages
function showStatus(message, type) {
statusDiv.textContent = message;
statusDiv.className = `status ${type}`;
statusDiv.style.display = 'block';
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) {
// 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>
</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