Frontend support for mark all viewed and delete all
All checks were successful
Build Container / build (push) Successful in 3m48s

This commit is contained in:
Sami Abuzakuk
2025-11-01 17:05:24 +01:00
parent 625b231de5
commit dff07ef340
2 changed files with 31 additions and 7 deletions

View File

@@ -245,6 +245,28 @@ export async function addSubscription(topic: string): Promise<Subscription> {
return response.json(); return response.json();
} }
// Add a new notification to a specific subscription
export async function markAllNotificationsAsViewed(subscriptionId: number): Promise<void> {
const response = await fetch(`${API_URL}/subscriptions/${subscriptionId}/notifications`, {
method: 'POST',
headers: authHeaders()
});
if (!response.ok) {
throw new Error('Failed to mark all notifications as viewed for subscription');
}
}
// Delete all notifications for a specific subscription
export async function deleteSubscriptionNotifications(subscriptionId: number): Promise<void> {
const response = await fetch(`${API_URL}/subscriptions/${subscriptionId}/notifications`, {
method: 'DELETE',
headers: authHeaders()
});
if (!response.ok) {
throw new Error('Failed to delete notifications for subscription');
}
}
// Delete a subscription // Delete a subscription
export async function deleteSubscription(subscriptionId: number): Promise<void> { export async function deleteSubscription(subscriptionId: number): Promise<void> {
const response = await fetch(`${API_URL}/subscriptions/${subscriptionId}`, { const response = await fetch(`${API_URL}/subscriptions/${subscriptionId}`, {

View File

@@ -1,5 +1,11 @@
<script lang="ts"> <script lang="ts">
import { deleteNotification, setViewed, fetchSubscriptionNotifications } from '$lib/api'; import {
deleteNotification,
setViewed,
fetchSubscriptionNotifications,
deleteSubscriptionNotifications,
markAllNotificationsAsViewed
} from '$lib/api';
import type { Notification, Subscription } from '$lib/api'; import type { Notification, Subscription } from '$lib/api';
let { data } = $props(); let { data } = $props();
@@ -15,7 +21,7 @@
); );
if (!confirmed) return; if (!confirmed) return;
try { try {
await Promise.all(notifications.map((notification) => deleteNotification(notification.id))); deleteSubscriptionNotifications(subscription!.id);
notifications = []; notifications = [];
window.showNotification('success', 'All notifications deleted successfully.'); window.showNotification('success', 'All notifications deleted successfully.');
} catch (error) { } catch (error) {
@@ -58,11 +64,7 @@
} }
async function markAllViewed() { async function markAllViewed() {
try { try {
await Promise.all( markAllNotificationsAsViewed(subscription!.id);
notifications
.filter((notification) => !notification.viewed)
.map((notification) => setViewed(notification.id))
);
notifications = notifications.map((notification) => notifications = notifications.map((notification) =>
notification.viewed ? notification : { ...notification, viewed: true } notification.viewed ? notification : { ...notification, viewed: true }
); );