-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
202 lines (178 loc) · 8.12 KB
/
script.js
File metadata and controls
202 lines (178 loc) · 8.12 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
document.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById('new-assignment-form');
const regularListContainer = document.getElementById('list-container');
const urgentListContainer = document.getElementById('urgent-list-container');
function isUrgent(dueDate) {
const now = new Date();
const oneDay = 24 * 60 * 60 * 1000; // milliseconds in one day
const dueDateTime = new Date(dueDate).getTime();
return (dueDateTime - now.getTime()) < oneDay;
}
function createPriorityLabel(priority) {
const label = document.createElement('span');
label.classList.add('priority-label', `priority-${priority.toLowerCase()}`);
label.textContent = priority;
return label;
}
function createCountdownTimer(dueDate) {
const timer = document.createElement('span');
timer.classList.add('countdown-timer');
const dueTime = new Date(dueDate).getTime();
const updateTime = () => {
const currentTime = new Date().getTime();
const timeLeft = dueTime - currentTime;
const days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
timer.textContent = `${days} days left`;
if (timeLeft < 0) {
timer.textContent = "Expired";
clearInterval(interval);
}
};
const interval = setInterval(updateTime, 1000);
updateTime();
return timer;
}
function createListItem(name, dueDate, priority = 'High') {
const listItem = document.createElement('li');
listItem.textContent = `${name} - Due: ${dueDate}`;
const priorityLabel = createPriorityLabel(priority);
const countdownTimer = createCountdownTimer(dueDate);
listItem.prepend(priorityLabel);
listItem.appendChild(countdownTimer);
listItem.setAttribute('data-due-date', dueDate);
return listItem;
}
// get priority
function getPriority(dueDate) {
const now = new Date();
const dueTime = new Date(dueDate).getTime();
const timeDiff = dueTime - now.getTime();
const daysLeft = timeDiff / (1000 * 60 * 60 * 24);
if (daysLeft <= 2) return 'High';
if (daysLeft <= 7) return 'Medium';
return 'Low';
}
function addAssignment(name, dueDate) {
const priority = getPriority(dueDate);
const listItem = createListItem(name, dueDate, priority);
const moveToCompletedBtn = document.createElement('button');
moveToCompletedBtn.classList.add('move-to-completed-btn');
moveToCompletedBtn.textContent = 'Complete';
moveToCompletedBtn.onclick = function() {
moveToCompleted(name, dueDate);
listItem.remove();
};
listItem.appendChild(moveToCompletedBtn);
if (isUrgent(dueDate)) {
urgentListContainer.appendChild(listItem);
} else {
regularListContainer.appendChild(listItem);
}
}
function moveToCompleted(name, dueDate) {
const completedListContainer = document.getElementById('completed-list-container');
const completedItem = createListItem(name, dueDate);
completedListContainer.appendChild(completedItem);
}
form.addEventListener('submit', function(event) {
event.preventDefault();
const assignmentName = document.getElementById('assignment-name').value;
const dueDate = document.getElementById('due-date').value;
addAssignment(assignmentName, dueDate);
form.reset(); // Reset form fields after adding the assignment
});
document.getElementById('contact-us').addEventListener('click', function() {
window.location.href = 'mailto:HelpDesk@CourseCompass.com';
});
document.getElementById('search-bar').addEventListener('input', function(e) {
const searchTerm = e.target.value.toLowerCase();
const tasks = document.querySelectorAll('#list-container li');
tasks.forEach(task => {
const text = task.textContent.toLowerCase();
const isVisible = text.includes(searchTerm);
task.style.display = isVisible ? 'block' : 'none';
});
});
// generates schedule
document.getElementById('generate-schedule-btn').addEventListener('click', function() {
generateWeeklySchedule();
});
function generateWeeklySchedule() {
const now = new Date();
const endOfWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 7);
const scheduleContainer = document.getElementById('weekly-schedule');
scheduleContainer.innerHTML = ''; // Clear existing schedule
const assignments = document.querySelectorAll('#list-container li, #urgent-list-container li');
const weeklyAssignments = Array.from(assignments).filter(assignment => {
const dueDate = new Date(assignment.getAttribute('data-due-date'));
return dueDate >= now && dueDate <= endOfWeek;
});
if (weeklyAssignments.length === 0) {
scheduleContainer.textContent = 'No assignments due this week.';
return;
}
// //////
const scheduleList = document.createElement('ul');
scheduleList.classList.add('schedule-list');
weeklyAssignments.forEach(assignment => {
const listItem = document.createElement('li');
listItem.classList.add('schedule-item');
const dueDateText = document.createElement('span');
dueDateText.classList.add('due-date');
dueDateText.textContent = assignment.getAttribute('data-due-date');
const daysLeftText = document.createElement('span');
daysLeftText.classList.add('days-left');
const dueDate = new Date(assignment.getAttribute('data-due-date'));
const daysLeft = Math.floor((dueDate - new Date()) / (1000 * 60 * 60 * 24));
daysLeftText.textContent = `${daysLeft} days left`;
const assignmentName = document.createElement('span');
assignmentName.classList.add('assignment-name');
assignmentName.textContent = assignment.textContent;
listItem.appendChild(dueDateText);
listItem.appendChild(assignmentName);
listItem.appendChild(daysLeftText);
scheduleList.appendChild(listItem);
});
scheduleContainer.appendChild(scheduleList);
}
fetch('assignments.json')
.then(response => response.json())
.then(assignments => {
assignments.forEach(course => {
course.assignments.forEach(assignment => {
addAssignmentFromData(assignment, course.courseId);
});
});
}).catch(error => {
console.error('Error fetching assignments:', error);
// Handle error (e.g., show an error message)
});
function addAssignmentFromData(assignment, courseId) {
const name = `${assignment.title} (${courseId})`;
const dueDate = assignment.dueDate;
// Determine priority based on due date
const priority = getPriority(dueDate);
const listItem = createListItem(name, dueDate, priority);
// Determine whether to add to regular or urgent list based on due date
if (isUrgent(dueDate)) {
urgentListContainer.appendChild(listItem);
} else {
regularListContainer.appendChild(listItem);
}
}
function processFetchedAssignments(assignments) {
assignments.forEach(course => {
course.assignments.forEach(assignment => {
const name = `${assignment.title} - ${course.courseId}`;
const dueDate = assignment.dueDate;
const priority = getPriority(dueDate);
addAssignment(name, dueDate, priority);
});
});
}
// Fetching assignments data from the server
fetch('/api/assignments')
.then(response => response.json())
.then(assignmentsData => processFetchedAssignments(assignmentsData))
.catch(error => console.error('Error fetching assignments:', error));
});