-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
128 lines (116 loc) · 5.18 KB
/
script.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
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
document.addEventListener('DOMContentLoaded', () => {
const initialView = document.getElementById('initial-view');
const loginForms = document.getElementById('login-forms');
const appDiv = document.getElementById('app');
// Show login forms
document.getElementById('show-admin-login').addEventListener('click', () => {
initialView.style.display = 'none';
loginForms.style.display = 'block';
document.getElementById('user-login-form').style.display = 'none';
document.getElementById('admin-login-form').style.display = 'block';
});
document.getElementById('show-user-login').addEventListener('click', () => {
initialView.style.display = 'none';
loginForms.style.display = 'block';
document.getElementById('admin-login-form').style.display = 'none';
document.getElementById('user-login-form').style.display = 'block';
});
// Admin Login Form
document.getElementById('admin-login-form').addEventListener('submit', (e) => {
e.preventDefault();
const clubName = document.getElementById('admin-club-name').value;
const password = document.getElementById('admin-password').value;
fetch('/api/admin/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ clubName, password })
})
.then(response => response.json())
.then(data => {
alert(data.message);
if (data.message === 'Admin authenticated successfully') {
loginForms.style.display = 'none';
appDiv.style.display = 'block';
showAdminControls(clubName);
}
})
.catch(error => console.error('Error:', error));
});
// User Login Form
document.getElementById('user-login-form').addEventListener('submit', (e) => {
e.preventDefault();
const enrollmentNumber = document.getElementById('enrollment-number').value;
fetch('/api/users/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enrollmentNumber })
})
.then(response => response.json())
.then(data => {
alert(data.message);
if (data.message === 'User authenticated successfully') {
loginForms.style.display = 'none';
appDiv.style.display = 'block';
showClubsForUser(data.clubName);
}
})
.catch(error => console.error('Error:', error));
});
function showAdminControls(clubName) {
// Show admin controls and allow them to add notifications
appDiv.innerHTML = `
<h2>Admin Controls for ${clubName}</h2>
<form id="add-notification-form">
<label for="notification">Add Notification:</label>
<input type="text" id="notification" required>
<button type="submit">Add Notification</button>
</form>
<div id="notifications"></div>
`;
document.getElementById('add-notification-form').addEventListener('submit', (e) => {
e.preventDefault();
const notification = document.getElementById('notification').value;
fetch(`/api/clubs/${clubName}/notifications`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ notification })
})
.then(response => response.json())
.then(notifications => {
alert('Notification added');
displayNotifications(notifications);
})
.catch(error => console.error('Error:', error));
});
fetch(`/api/clubs/${clubName}/notifications`)
.then(response => response.json())
.then(notifications => {
displayNotifications(notifications);
});
}
function showClubsForUser(clubName) {
fetch('/api/clubs')
.then(response => response.json())
.then(clubs => {
const appDiv = document.getElementById('app');
appDiv.innerHTML = `<h2>Clubs for ${clubName}</h2>`;
clubs.forEach(club => {
const clubDiv = document.createElement('div');
clubDiv.innerHTML = `
<h3>${club.clubName}</h3>
<button onclick="viewNotifications('${club.clubName}')">View Notifications</button>
`;
appDiv.appendChild(clubDiv);
});
});
}
function displayNotifications(notifications) {
const notificationsDiv = document.getElementById('notifications');
notificationsDiv.innerHTML = `<h3>Notifications</h3>`;
notifications.forEach(notification => {
const p = document.createElement('p');
p.textContent = notification;
notificationsDiv.appendChild(p);
});
}
});