Compare commits
3 Commits
657a224163
...
dff07ef340
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dff07ef340 | ||
|
|
625b231de5 | ||
|
|
013ddb26c7 |
@@ -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()
|
||||
|
||||
@@ -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}`, {
|
||||
|
||||
@@ -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 }
|
||||
);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user