-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatienthistory.js
More file actions
157 lines (129 loc) · 6.37 KB
/
patienthistory.js
File metadata and controls
157 lines (129 loc) · 6.37 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Initialize and open the IndexedDB database
let db1;
// DOMCONTENTLOADED: run the following code when the html page has been fully loaded
document.addEventListener('DOMContentLoaded', () => {
let request = window.indexedDB.open('DoctorsDB', 4); // Open 'doctorsDB' with version 2
request.onerror = function(event) {
console.error('Database failed to open', event);
};
request.onsuccess = function(event) {
db1 = event.target.result; // Assign the opened database to db1
console.log('Database opened successfully'); // debugging
// Check if the 'userLogins' store is populated
let transaction = db1.transaction(['userLogins'], 'readonly');
let objectStore = transaction.objectStore('userLogins');
let countRequest = objectStore.count(); // count number of items in object store called userLogins
countRequest.onsuccess = function() {
if (countRequest.result === 0) {
console.log('Populating users...');
populateUsers(); // Populate users if the store is empty
} else {
console.log('User logins store already populated');
}
};
};
// Upgrade event for IndexedDB version changes
request.onupgradeneeded = function(event) {
db1 = event.target.result;
// Create 'doctors' store if it doesn't exist
if (!db1.objectStoreNames.contains('doctors')) {
let objectStore = db1.createObjectStore('doctors', { keyPath: 'id', autoIncrement: true });
objectStore.createIndex('first_name', 'first_name', { unique: false });
objectStore.createIndex('last_name', 'last_name', { unique: false });
console.log('Doctors object store created');
}
// Create 'userLogins' store if it doesn't exist
if (!db1.objectStoreNames.contains('userLogins')) {
let loginStore = db1.createObjectStore('userLogins', { keyPath: 'id', autoIncrement: true });
loginStore.createIndex('username', 'username', { unique: true }); // Add index for 'username'
console.log('User logins object store created');
}
};
});
// Display appointments for the logged-in patient
function displayUserAppointments() {
const username = localStorage.getItem('loggedInPatientUsername');
if (!username) {
console.log('No user is logged in.');
return;
}
const transaction = db.transaction(['appointments'], 'readonly');
const objectStore = transaction.objectStore('appointments');
const request = objectStore.getAll();
request.onsuccess = function (event) {
const appointments = event.target.result;
const userAppointments = appointments.filter(appointment => appointment.username === username);
const tableBody = document.querySelector('#appointments-list tbody');
tableBody.innerHTML = ''; // Clear existing rows
if (userAppointments.length > 0) {
userAppointments.forEach((appointment) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${appointment.date}</td>
<td>${appointment.time}</td>
<td>${appointment.doctor}</td>
<td>${appointment.reason}</td>
<td>${appointment.notes}</td>
`;
tableBody.appendChild(row);
});
} else {
const row = document.createElement('tr');
row.innerHTML = `<td colspan="4">No appointments found for ${username}.</td>`;
tableBody.appendChild(row);
}
};
request.onerror = function (event) {
console.error('Error fetching appointments:', event.target.error);
};
}
// Function to populate the 'userLogins' store with sample data
function populateUsers() {
// populate with sample data if no data exists
let users = [
{ username: 'john.doe@example.com', password: 'password123' },
{ username: 'jane.doe@example.com', password: 'securepass' }
];
let transaction = db1.transaction(['userLogins'], 'readwrite'); // read write permissions for db's object store userLogins
let objectStore = transaction.objectStore('userLogins'); // object store called userLogins
users.forEach(user => objectStore.add(user)); // Add user with their data for each
transaction.oncomplete = function() {
console.log('Users added successfully');
};
// error handling and debugging
transaction.onerror = function(event) {
console.error('Error adding users:', event.target.error);
};
}
// Login form submission event listener
document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent default form submission
// create variables and store values as values of form elements
let username = document.getElementById('username').value;
let password = document.getElementById('password').value;
console.log(`Attempting login with username: ${username}`); // Debugging line
let request = window.indexedDB.open('DoctorsDB', 4); // Open 'doctorsDB'
request.onerror = function(event) {
console.error('Database failed to open', event); // debugging
};
request.onsuccess = function(event) {
let db = event.target.result;
// Transaction to check if the user exists in the userLogins store
let transaction = db.transaction(['userLogins'], 'readonly');
let objectStore = transaction.objectStore('userLogins');
let index = objectStore.index('username');
let getRequest = index.get(username);
getRequest.onsuccess = function(event) {
let user = event.target.result;
console.log('User found:', user); // Debugging line
if (user && user.password === password) {
window.location.href = 'dashboard.html'; // Redirect to dashboard on successful login
} else {
alert('Invalid username or password'); // use alert() function to display msg to user
}
};
getRequest.onerror = function(event) {
console.error('Error fetching user:', event.target.errorCode);
};
};
});