Files
bakery-ia/frontend/src/api/hooks/useNotification.ts
2025-08-03 17:48:34 +02:00

151 lines
4.2 KiB
TypeScript

// frontend/src/api/hooks/useNotification.ts
/**
* Notification Operations Hooks
*/
import { useState, useCallback } from 'react';
import { notificationService } from '../services';
import type {
NotificationCreate,
NotificationResponse,
NotificationTemplate,
NotificationStats,
BulkNotificationRequest,
} from '../types';
export const useNotification = () => {
const [notifications, setNotifications] = useState<NotificationResponse[]>([]);
const [templates, setTemplates] = useState<NotificationTemplate[]>([]);
const [stats, setStats] = useState<NotificationStats | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const sendNotification = useCallback(async (
tenantId: string,
notification: NotificationCreate
): Promise<NotificationResponse> => {
try {
setIsLoading(true);
setError(null);
const sentNotification = await notificationService.sendNotification(tenantId, notification);
setNotifications(prev => [sentNotification, ...prev]);
return sentNotification;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to send notification';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
const sendBulkNotifications = useCallback(async (
tenantId: string,
request: BulkNotificationRequest
): Promise<void> => {
try {
setIsLoading(true);
setError(null);
await notificationService.sendBulkNotifications(tenantId, request);
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to send bulk notifications';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
const getNotifications = useCallback(async (tenantId: string): Promise<NotificationResponse[]> => {
try {
setIsLoading(true);
setError(null);
const response = await notificationService.getNotifications(tenantId);
setNotifications(response.data);
return response.data;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get notifications';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
const getTemplates = useCallback(async (tenantId: string): Promise<NotificationTemplate[]> => {
try {
setIsLoading(true);
setError(null);
const response = await notificationService.getTemplates(tenantId);
setTemplates(response.data);
return response.data;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get templates';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
const createTemplate = useCallback(async (
tenantId: string,
template: Omit<NotificationTemplate, 'id' | 'tenant_id' | 'created_at' | 'updated_at'>
): Promise<NotificationTemplate> => {
try {
setIsLoading(true);
setError(null);
const newTemplate = await notificationService.createTemplate(tenantId, template);
setTemplates(prev => [newTemplate, ...prev]);
return newTemplate;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to create template';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
const getNotificationStats = useCallback(async (tenantId: string): Promise<NotificationStats> => {
try {
setIsLoading(true);
setError(null);
const notificationStats = await notificationService.getNotificationStats(tenantId);
setStats(notificationStats);
return notificationStats;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get notification stats';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
return {
notifications,
templates,
stats,
isLoading,
error,
sendNotification,
sendBulkNotifications,
getNotifications,
getTemplates,
createTemplate,
getNotificationStats,
clearError: () => setError(null),
};
};