Untitled
unknown
plain_text
a year ago
5.5 kB
7
Indexable
// Data structure to store users and appointments
let users = JSON.parse(localStorage.getItem('users')) || [];
let appointments = JSON.parse(localStorage.getItem('appointments')) || [];
// Current session data
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
// Utility functions
function saveData() {
localStorage.setItem('users', JSON.stringify(users));
localStorage.setItem('appointments', JSON.stringify(appointments));
localStorage.setItem('currentUser', JSON.stringify(currentUser));
}
// Sign Up Function
function signUp() {
let username = document.getElementById('signup-username').value;
let password = document.getElementById('signup-password').value;
if (username && password) {
if (users.find(user => user.username === username)) {
alert('Username already exists.');
} else {
users.push({ username, password, role: 'Patient' });
saveData();
alert('Sign Up successful! You can now log in.');
}
} else {
alert('Please fill in all fields.');
}
}
// Log In Function
function logIn() {
let username = document.getElementById('login-username').value;
let password = document.getElementById('login-password').value;
let user = users.find(user => user.username === username && user.password === password);
if (user) {
currentUser = user;
saveData();
displayDashboard();
} else {
alert('Invalid username or password.');
}
}
// Log Out Function
function logOut() {
currentUser = null;
saveData();
displayLoginForm();
}
// Display Dashboard
function displayDashboard() {
document.getElementById('signup-form').style.display = 'none';
document.getElementById('login-form').style.display = 'none';
if (currentUser.role === 'Patient') {
document.getElementById('patient-dashboard').style.display = 'block';
loadPatientDashboard();
} else if (currentUser.role === 'Doctor') {
document.getElementById('doctor-dashboard').style.display = 'block';
loadDoctorDashboard();
}
}
// Display Login Form
function displayLoginForm() {
document.getElementById('signup-form').style.display = 'none';
document.getElementById('login-form').style.display = 'block';
document.getElementById('patient-dashboard').style.display = 'none';
document.getElementById('doctor-dashboard').style.display = 'none';
}
// Book Appointment Function
function bookAppointment() {
let doctor = document.getElementById('doctor-list').value;
let appointmentTime = new Date(document.getElementById('appointment-time').value);
if (doctor && appointmentTime) {
if (!appointments.find(app => app.doctor === doctor && app.time.getTime() === appointmentTime.getTime())) {
appointments.push({ patient: currentUser.username, doctor, time: appointmentTime });
saveData();
loadPatientDashboard();
alert('Appointment booked successfully.');
} else {
alert('This slot is already booked.');
}
} else {
alert('Please fill in all fields.');
}
}
// Load Patient Dashboard
function loadPatientDashboard() {
let appointmentsList = document.getElementById('patient-appointments');
appointmentsList.innerHTML = '';
let patientAppointments = appointments.filter(app => app.patient === currentUser.username);
patientAppointments.forEach(app => {
let li = document.createElement('li');
li.innerText = `Doctor: ${app.doctor}, Time: ${app.time}`;
appointmentsList.appendChild(li);
});
}
// Load Doctor Dashboard
function loadDoctorDashboard() {
let appointmentsList = document.getElementById('doctor-appointments');
appointmentsList.innerHTML = '';
let doctorAppointments = appointments.filter(app => app.doctor === currentUser.username);
doctorAppointments.forEach(app => {
let li = document.createElement('li');
li.innerText = `Patient: ${app.patient}, Time: ${app.time}`;
appointmentsList.appendChild(li);
});
}
// Initialize App
function initApp() {
if (currentUser) {
displayDashboard();
} else {
displayLoginForm();
}
// Initialize doctor list for booking
let doctorList = document.getElementById('doctor-list');
doctorList.innerHTML = '';
users.filter(user => user.role === 'Doctor').forEach(doctor => {
let option = document.createElement('option');
option.value = doctor.username;
option.innerText = doctor.username;
doctorList.appendChild(option);
});
// Set up appointment alerts
setInterval(() => {
let now = new Date();
appointments.forEach(app => {
let timeDiff = new Date(app.time) - now;
if (timeDiff > 0 && timeDiff <= 300000) { // 5 minutes
if (currentUser.username === app.patient) {
alert(`Reminder: You have an appointment with Dr. ${app.doctor} in 5 minutes.`);
}
if (currentUser.username === app.doctor) {
alert(`Reminder: You have an appointment with patient ${app.patient} in 5 minutes.`);
}
}
});
}, 60000);
}
// Start the application
initApp();
Editor is loading...
Leave a Comment