Compare commits

...

3 Commits

Author SHA1 Message Date
Sami Abuzakuk
dff07ef340 Frontend support for mark all viewed and delete all
All checks were successful
Build Container / build (push) Successful in 3m48s
2025-11-01 17:05:24 +01:00
Sami Abuzakuk
625b231de5 Add backend for set all viewed and delete all 2025-11-01 17:05:10 +01:00
Sami Abuzakuk
013ddb26c7 Fix no reload on register 2025-11-01 16:41:52 +01:00
4 changed files with 67 additions and 8 deletions

View File

@@ -242,6 +242,41 @@ def list_subscription_notifications(
]
@app.post("/subscriptions/{subscription_id}/notifications")
def set_all_notifications_viewed(
subscription_id: int,
current_user: User = Depends(get_current_user),
):
db = SessionLocal()
notifications = (
db.query(Notification)
.filter(Notification.subscription_id == subscription_id)
.all()
)
for notification in notifications:
notification.viewed = True
db.commit()
db.close()
return {"message": "Notifications marked as viewed"}
@app.delete("/subscriptions/{subscription_id}/notifications")
def remove_subscription_notifications(
subscription_id: int, current_user: User = Depends(get_current_user)
):
db = SessionLocal()
notifications = (
db.query(Notification)
.filter(Notification.subscription_id == subscription_id)
.all()
)
for notification in notifications:
db.delete(notification)
db.commit()
db.close()
return {"message": "Notifications removed"}
@app.get("/notifications")
def list_notifications(current_user: User = Depends(get_current_user)):
db = SessionLocal()

View File

@@ -245,6 +245,28 @@ export async function addSubscription(topic: string): Promise<Subscription> {
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
export async function deleteSubscription(subscriptionId: number): Promise<void> {
const response = await fetch(`${API_URL}/subscriptions/${subscriptionId}`, {

View File

@@ -1,5 +1,11 @@
<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';
let { data } = $props();
@@ -15,7 +21,7 @@
);
if (!confirmed) return;
try {
await Promise.all(notifications.map((notification) => deleteNotification(notification.id)));
deleteSubscriptionNotifications(subscription!.id);
notifications = [];
window.showNotification('success', 'All notifications deleted successfully.');
} catch (error) {
@@ -58,11 +64,7 @@
}
async function markAllViewed() {
try {
await Promise.all(
notifications
.filter((notification) => !notification.viewed)
.map((notification) => setViewed(notification.id))
);
markAllNotificationsAsViewed(subscription!.id);
notifications = notifications.map((notification) =>
notification.viewed ? notification : { ...notification, viewed: true }
);

View File

@@ -27,7 +27,7 @@
}
const data = await response.json();
localStorage.setItem('token', data.access_token);
goto('/');
goto('/').then(() => location.reload());
} catch (err) {
error = 'Network error - ' + err;
} finally {