Files
n3wt-school/Front-End/src/context/NotificationContext.js
2025-05-22 18:15:05 +02:00

44 lines
1.1 KiB
JavaScript

import React, { createContext, useState, useContext } from 'react';
import FlashNotification from '@/components/FlashNotification';
const NotificationContext = createContext();
export const NotificationProvider = ({ children }) => {
const [notification, setNotification] = useState({
message: '',
type: '',
title: '',
errorCode: '',
});
const showNotification = (
message,
type = 'info',
title = '',
errorCode = ''
) => {
setNotification({ message, type, title, errorCode });
};
const clearNotification = () => {
setNotification({ message: '', type: '', title: '', errorCode: '' });
};
return (
<NotificationContext.Provider value={{ showNotification }}>
{notification.message && (
<FlashNotification
title={notification.title}
message={notification.message}
type={notification.type}
errorCode={notification.errorCode}
onClose={clearNotification}
/>
)}
{children}
</NotificationContext.Provider>
);
};
export const useNotification = () => useContext(NotificationContext);