Add frontend support for notifications
This commit is contained in:
@@ -1,5 +1,24 @@
|
||||
export const API_URL = 'http://127.0.0.1:8000';
|
||||
|
||||
/**
|
||||
* Type definitions for Subscriptions and Notifications
|
||||
*/
|
||||
export interface Subscription {
|
||||
id: number;
|
||||
topic: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface Notification {
|
||||
id: number;
|
||||
subscription_id: number;
|
||||
title: string;
|
||||
message: string;
|
||||
priority: number;
|
||||
created_at: string;
|
||||
viewed: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Type definitions for Settings
|
||||
*/
|
||||
@@ -8,6 +27,7 @@ export interface Settings {
|
||||
requirements: string;
|
||||
environment: string;
|
||||
user: string;
|
||||
ntfy_url?: string;
|
||||
}
|
||||
|
||||
export async function checkHealth(): Promise<'healthy' | 'unhealthy'> {
|
||||
@@ -96,7 +116,7 @@ export async function updateSetting(
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(updatedSetting)
|
||||
body: JSON.stringify({ ...updatedSetting, ntfy_url: updatedSetting.ntfy_url })
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to update setting');
|
||||
@@ -104,6 +124,115 @@ export async function updateSetting(
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Fetch all subscriptions
|
||||
export async function fetchSubscriptions(): Promise<Subscription[]> {
|
||||
const response = await fetch(`${API_URL}/subscriptions`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch subscriptions');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Fetch subscriptions by topic
|
||||
export async function getSubscription(topic_id: string): Promise<Subscription> {
|
||||
const response = await fetch(`${API_URL}/subscriptions/${topic_id}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch subscriptions');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Add a new notification to a subscription
|
||||
export async function addNotification(
|
||||
subscriptionId: number,
|
||||
title: string,
|
||||
message: string,
|
||||
priority: number
|
||||
): Promise<Notification> {
|
||||
const response = await fetch(`${API_URL}/notifications`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ subscription_id: subscriptionId, title, message, priority })
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to add notification');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Mark a notification as viewed
|
||||
export async function setViewed(notificationId: number): Promise<Notification> {
|
||||
const response = await fetch(`${API_URL}/notifications/${notificationId}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ viewed: true })
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to set notification as viewed');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Add a new subscription
|
||||
export async function addSubscription(topic: string): Promise<Subscription> {
|
||||
const response = await fetch(`${API_URL}/subscriptions`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ topic })
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to add subscription');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Delete a subscription
|
||||
export async function deleteSubscription(subscriptionId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/subscriptions/${subscriptionId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete subscription');
|
||||
}
|
||||
}
|
||||
|
||||
// Get all subscription notifications
|
||||
export async function fetchSubscriptionNotifications(
|
||||
subscriptionId: string
|
||||
): Promise<Notification[]> {
|
||||
const response = await fetch(`${API_URL}/subscriptions/${subscriptionId}/notifications`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch subscription notifications');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Fetch all notifications or filter by topic
|
||||
export async function fetchAllNotifications(): Promise<Notification[]> {
|
||||
const url = `${API_URL}/notifications`;
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch notifications');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Delete a notification
|
||||
export async function deleteNotification(notificationId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/notifications/${notificationId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete notification');
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch a single script by ID
|
||||
export async function fetchScriptById(id: number): Promise<Script> {
|
||||
const response = await fetch(`${API_URL}/script/${id}`);
|
||||
|
||||
Reference in New Issue
Block a user