mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-12-01 06:55:45 +01:00
fixed user deletion
This commit is contained in:
94
frontend/src/contexts/NotificationContext.tsx
Normal file
94
frontend/src/contexts/NotificationContext.tsx
Normal file
@@ -0,0 +1,94 @@
|
||||
// frontend/src/contexts/NotificationContext.tsx
|
||||
import React, { createContext, useContext, useState } from 'react';
|
||||
|
||||
export type NotificationType = 'info' | 'success' | 'warning' | 'error';
|
||||
|
||||
export interface Notification {
|
||||
id: string;
|
||||
type: NotificationType;
|
||||
title: string;
|
||||
message: string;
|
||||
duration?: number;
|
||||
}
|
||||
|
||||
interface NotificationContextType {
|
||||
notifications: Notification[];
|
||||
showNotification: (notification: Omit<Notification, 'id'>) => void;
|
||||
removeNotification: (id: string) => void;
|
||||
confirmDialog: (options: ConfirmDialogOptions) => Promise<boolean>;
|
||||
}
|
||||
|
||||
interface ConfirmDialogOptions {
|
||||
title: string;
|
||||
message: string;
|
||||
confirmText?: string;
|
||||
cancelText?: string;
|
||||
type?: NotificationType;
|
||||
}
|
||||
|
||||
const NotificationContext = createContext<NotificationContextType | undefined>(undefined);
|
||||
|
||||
export const NotificationProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [notifications, setNotifications] = useState<Notification[]>([]);
|
||||
|
||||
const showNotification = (notification: Omit<Notification, 'id'>) => {
|
||||
const id = Date.now().toString();
|
||||
const newNotification: Notification = {
|
||||
id,
|
||||
duration: 5000,
|
||||
...notification
|
||||
};
|
||||
|
||||
setNotifications(prev => [...prev, newNotification]);
|
||||
|
||||
// Auto-remove after duration
|
||||
if (newNotification.duration) {
|
||||
setTimeout(() => {
|
||||
removeNotification(id);
|
||||
}, newNotification.duration);
|
||||
}
|
||||
};
|
||||
|
||||
const removeNotification = (id: string) => {
|
||||
setNotifications(prev => prev.filter(notif => notif.id !== id));
|
||||
};
|
||||
|
||||
const confirmDialog = (options: ConfirmDialogOptions): Promise<boolean> => {
|
||||
return new Promise((resolve) => {
|
||||
const {
|
||||
title,
|
||||
message,
|
||||
confirmText = 'OK',
|
||||
cancelText = 'Abbrechen',
|
||||
type = 'warning'
|
||||
} = options;
|
||||
|
||||
const userConfirmed = window.confirm(
|
||||
`${title}\n\n${message}\n\n${confirmText} / ${cancelText}`
|
||||
);
|
||||
|
||||
resolve(userConfirmed);
|
||||
});
|
||||
};
|
||||
|
||||
const value = {
|
||||
notifications,
|
||||
showNotification,
|
||||
removeNotification,
|
||||
confirmDialog
|
||||
};
|
||||
|
||||
return (
|
||||
<NotificationContext.Provider value={value}>
|
||||
{children}
|
||||
</NotificationContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useNotification = () => {
|
||||
const context = useContext(NotificationContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useNotification must be used within a NotificationProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
Reference in New Issue
Block a user