do not notify if viewing deleted tasks

This commit is contained in:
Peter 2023-10-08 19:29:23 +08:00
parent c331deb49e
commit a6e7d0a970
2 changed files with 63 additions and 51 deletions

View File

@ -52,6 +52,11 @@ class TodosAdmin(AdminBase):
F("due_date").asc(nulls_last=True),
"-priority",
)
list_filter = (
"priority",
"due_date",
("deleted", DeletedListFilter),
)
form = TodosAdminForm
# def bump_priority(self, obj):

View File

@ -7,6 +7,7 @@ const createNotification = (title, body) => {
// Create a notification
var notification = new Notification(title, {
body: body,
// url: TODO: WHEN CLICKING, GO TO EDIT PAGE FOR THAT TASK http://localhost:8000/admin/expenses/todo/1/change/
});
// You can add event listeners to handle user interactions with the notification
@ -62,61 +63,67 @@ function parseDateString(dateString) {
}
const alerted = new Set();
const deletedParam = urlParams.get("deleted__exact");
document.addEventListener("DOMContentLoaded", function () {
// NOTE:
// This is to stop me from accidentally clicking away from the TODO list and disabling notifications!
// You can still use the top links to navigate away
const navSidebar = document.getElementById("nav-sidebar");
const toggleNavSidebar = document.getElementById("toggle-nav-sidebar");
navSidebar ? navSidebar.parentNode.removeChild(navSidebar) : undefined;
toggleNavSidebar
? toggleNavSidebar.parentNode.removeChild(toggleNavSidebar)
: undefined;
// DO NOT NOTIFY FOR DELETED ITEMS
if (deletedParam !== "1") {
document.addEventListener("DOMContentLoaded", function () {
// NOTE:
// This is to stop me from accidentally clicking away from the TODO list and disabling notifications!
// You can still use the top links to navigate away
const navSidebar = document.getElementById("nav-sidebar");
const toggleNavSidebar = document.getElementById("toggle-nav-sidebar");
navSidebar ? navSidebar.parentNode.removeChild(navSidebar) : undefined;
toggleNavSidebar
? toggleNavSidebar.parentNode.removeChild(toggleNavSidebar)
: undefined;
function checkDueDates() {
const rows = document.querySelectorAll("#result_list tbody tr");
const currentDate = new Date();
function checkDueDates() {
const rows = document.querySelectorAll("#result_list tbody tr");
const currentDate = new Date();
rows.forEach(function (row) {
const titleCell = row.querySelector("th.field-title a"); // Select the due_date cell
const title = titleCell ? titleCell.textContent : "??";
const dueDateCell = row.querySelector("td.field-due_date"); // Select the due_date cell
const dueDateValue = dueDateCell.textContent.trim(); // Get the due date value
rows.forEach(function (row) {
const titleCell = row.querySelector("th.field-title a"); // Select the due_date cell
const title = titleCell ? titleCell.textContent : "??";
const dueDateCell = row.querySelector("td.field-due_date"); // Select the due_date cell
const dueDateValue = dueDateCell.textContent.trim(); // Get the due date value
// Check if the due date cell contains a valid date
if (dueDateValue !== "-") {
var dueDate = parseDateString(dueDateValue);
// Check if the due date cell contains a valid date
if (dueDateValue !== "-") {
var dueDate = parseDateString(dueDateValue);
// Calculate the difference in days between the due date and current date
var timeDifference = dueDate - currentDate;
var daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
// Check if the due date is in 7 days
if (
daysDifference <= 7 &&
daysDifference >= 0 &&
!alerted.has(title + dueDate)
) {
alerted.add(title + dueDate);
// alert(
// `Task '${
// row.querySelector("th.field-title a").textContent
// }' is due in ${daysDifference} days.`
// );
createNotification(
`Task '${
row.querySelector("th.field-title a").textContent
}' is due in ${daysDifference} days.`,
"Finish this shit already!"
// Calculate the difference in days between the due date and current date
var timeDifference = dueDate - currentDate;
var daysDifference = Math.floor(
timeDifference / (1000 * 60 * 60 * 24)
);
// Check if the due date is in 7 days
if (
daysDifference <= 7 &&
daysDifference >= 0 &&
!alerted.has(title + dueDate)
) {
alerted.add(title + dueDate);
// alert(
// `Task '${
// row.querySelector("th.field-title a").textContent
// }' is due in ${daysDifference} days.`
// );
createNotification(
`Task '${
row.querySelector("th.field-title a").textContent
}' is due in ${daysDifference} days.`,
"Finish this shit already!"
);
}
}
}
});
}
const resetAlerted = () => alerted.clear();
checkDueDates();
// Run the checkDueDates function every 5 seconds (adjust the interval as needed)
setInterval(checkDueDates, 5000);
setInterval(resetAlerted, 6 * 60 * 60 * 1000);
});
});
}
const resetAlerted = () => alerted.clear();
checkDueDates();
// Run the checkDueDates function every 5 seconds (adjust the interval as needed)
setInterval(checkDueDates, 30 * 1000);
setInterval(resetAlerted, 6 * 60 * 60 * 1000);
});
}