add features
This commit is contained in:
31
frontend/src/services/notifications.ts
Normal file
31
frontend/src/services/notifications.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { api } from './api';
|
||||
import type { Notification } from '../types/notification.types';
|
||||
|
||||
async function unwrap<T>(
|
||||
promise: ReturnType<typeof api.get<{ success: boolean; data: T }>>
|
||||
): Promise<T> {
|
||||
const response = await promise;
|
||||
if (response.data?.data === undefined || response.data?.data === null) {
|
||||
throw new Error('Invalid API response');
|
||||
}
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
export const notificationsApi = {
|
||||
async getNotifications(): Promise<Notification[]> {
|
||||
return unwrap(api.get<{ success: boolean; data: Notification[] }>('/api/notifications'));
|
||||
},
|
||||
|
||||
async getUnreadCount(): Promise<number> {
|
||||
const data = await unwrap(api.get<{ success: boolean; data: { count: number } }>('/api/notifications/count'));
|
||||
return data.count;
|
||||
},
|
||||
|
||||
async markRead(id: string): Promise<void> {
|
||||
await api.patch(`/api/notifications/${id}/read`);
|
||||
},
|
||||
|
||||
async markAllRead(): Promise<void> {
|
||||
await api.post('/api/notifications/mark-all-read');
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user