-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement unread notifications filter and enhance NotificationViewSet (…
…#105) * feat: implement unread notifications filter in NotificationViewSet * refactor: Improve notifications
- Loading branch information
1 parent
2d73fa9
commit 4304b9d
Showing
10 changed files
with
182 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/notifications/templates/notifications/all-notifications.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
{% extends "base.html" %} {% load static %} {% block content %} | ||
<div class="bg-green-700"> | ||
<div class="container mx-auto px-4 py-8 min-h-96"> | ||
<h1 class="text-3xl font-bold mb-6">All Notifications</h1> | ||
|
||
<div id="all-notifications" class="space-y-4"> | ||
<!-- Notifications will be dynamically inserted here --> | ||
</div> | ||
|
||
<button | ||
id="mark-all-read" | ||
class="mt-6 bg-blue-500 text-white rounded-md py-2 px-4 hover:bg-blue-600 transition duration-200" | ||
> | ||
Mark all as read | ||
</button> | ||
</div> | ||
</div> | ||
|
||
<script> | ||
document.addEventListener("DOMContentLoaded", function () { | ||
const allNotifications = document.getElementById("all-notifications"); | ||
const markAllReadButton = document.getElementById("mark-all-read"); | ||
|
||
function fetchAllNotifications() { | ||
fetch("/notifications/api/notifications/all/") | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
allNotifications.innerHTML = ""; | ||
|
||
data.forEach((notification) => { | ||
const item = document.createElement("div"); | ||
item.className = "p-4 bg-white shadow rounded-lg"; | ||
item.innerHTML = ` | ||
<a | ||
data-notification-link | ||
href="${ | ||
notification.url | ||
}" class="block font-semibold mb-1 hover:underline ${ | ||
notification.read ? "text-gray-500" : "" | ||
}">${notification.title}</a> | ||
<p class="text-sm text-gray-600">${ | ||
notification.message | ||
}</p> | ||
<p class="text-xs text-gray-400 mt-2">${new Date( | ||
notification.created_at | ||
).toLocaleString()}</p> | ||
${ | ||
!notification.read | ||
? `<button class="mt-2 text-sm text-blue-500 hover:underline mark-read" data-id="${notification.id}">Mark as read</button>` | ||
: "" | ||
} | ||
`; | ||
allNotifications.appendChild(item); | ||
}); | ||
}); | ||
} | ||
|
||
allNotifications.addEventListener("click", (e) => { | ||
if (e.target.attributes.getNamedItem("data-notification-link")) { | ||
e.preventDefault(); | ||
const link = e.target; | ||
const href = link.getAttribute("href"); | ||
const notificationId = link.parentElement.querySelector("button").dataset.id; | ||
|
||
fetch( | ||
`/notifications/api/notifications/${notificationId}/mark_as_read/`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-CSRFToken": getCookie("csrftoken"), | ||
}, | ||
} | ||
).finally(() => (document.location.href = href)); | ||
} | ||
|
||
if (e.target.classList.contains("mark-read")) { | ||
const id = e.target.dataset.id; | ||
fetch(`/notifications/api/notifications/${id}/mark_as_read/`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-CSRFToken": getCookie("csrftoken"), | ||
}, | ||
}).then(() => fetchAllNotifications()); | ||
} | ||
}); | ||
|
||
markAllReadButton.addEventListener("click", () => { | ||
fetch("/notifications/api/notifications/mark_all_as_read/", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-CSRFToken": getCookie("csrftoken"), | ||
}, | ||
}).then(() => fetchAllNotifications()); | ||
}); | ||
|
||
// Initial fetch | ||
fetchAllNotifications(); | ||
}); | ||
</script> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
from django.urls import path, include | ||
from django.views.generic import TemplateView | ||
from rest_framework.routers import DefaultRouter | ||
from .views import NotificationViewSet | ||
|
||
router = DefaultRouter() | ||
router.register(r"notifications", NotificationViewSet, basename="notification") | ||
|
||
urlpatterns = [ | ||
path("", include(router.urls)), | ||
path( | ||
"", | ||
TemplateView.as_view(template_name="notifications/all-notifications.html"), | ||
name="all_notifications", | ||
), | ||
path("api/", include(router.urls)), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters