-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprescriptions.js
61 lines (53 loc) · 1.75 KB
/
prescriptions.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
// Get jwt from local storage
const jwt = localStorage.getItem('jwt');
function jwt_decode(jwt) {
let handler = new JwtSecurityTokenHandler();
}
// Check if user is logged in
if (!jwt) {
document.getElementById('not-logged-in').classList.remove('hidden');
} else {
// Decode JWT to get patient_id and is_doctor
const decoded = jwt_decode(jwt);
const patient_id = decoded.patient_id;
const is_doctor = decoded.is_doctor;
if (is_doctor) {
document.getElementById('coming-soon').classList.remove('hidden');
} else {
// Make API call to http://127.0.0.1:3000/prescriptions
fetch('http://127.0.0.1:3000/prescriptions', {
method: 'POST',
headers: {
'Authorization': jwt,
'Content-Type': 'application/json'
},
body: JSON.stringify({
patient_id: patient_id
})
})
.then(response => response.json())
.then(data => {
// Populate table with data
const tableBody = document.getElementById('table-body');
data.forEach(item => {
const row = document.createElement('tr');
const docnameCell = document.createElement('td');
docnameCell.innerHTML = item.docname;
row.appendChild(docnameCell);
const dateCell = document.createElement('td');
const date = new Date(item.timestamp);
dateCell.innerHTML = date.toLocaleDateString();
row.appendChild(dateCell);
const prescriptionCell = document.createElement('td');
prescriptionCell.innerHTML = item.prescription;
row.appendChild(prescriptionCell);
tableBody.appendChild(row);
});
// Show table
document.getElementById('table-container').classList.remove('hidden');
})
.catch(error => {
console.error(error);
});
}
}