-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreportRenderer.js
83 lines (67 loc) · 2.38 KB
/
reportRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
console.log("reportRenderer script loaded");
document.addEventListener('DOMContentLoaded', () => {
console.log("DOM is fully loaded");
// A set up to the report generation button
document.getElementById('generateReportBtn').addEventListener('click', () => {
console.log("Generate report button clicked");
const studentId = '1';
console.log(`Requesting report generation for studentId: ${studentId}`);
requestReportGeneration(studentId);
});
});
// generate the student report
async function requestReportGeneration(studentId) {
try{
console.log('requestReportGeneration: Requesting report generation for studentId:', studentId); // Check if the function is called
const response = await fetch('http://localhost:3000/generate-report', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ studentId }),
});
if (!response.ok) {
throw new Error(`Network response was not ok, status: ${response.status}`);
}
const blob = await response.blob();
console.log('Blob received', blob);
// Create a URL for the blob object
const url = window.URL.createObjectURL(blob);
// Create a link to download the PDF
const a = document.createElement('a');
a.href = url;
a.download = `report_${studentId}.pdf`;
document.body.appendChild(a);
a.click();
// Clean up
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} catch(error){
console.error('Error generating report:', error);
}
}
function fetchDataFromAPI() {
fetch('http://localhost:3000/api/messages')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log(data);
displayMessages(data);
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
}
function displayMessages(messages) {
const messagesContainer = document.getElementById('messagesContainer');
messagesContainer.innerHTML = ''; // Clear previous messages
messages.forEach(message => {
const messageElement = document.createElement('div');
messageElement.innerText = message.content;
messagesContainer.appendChild(messageElement);
});
}