mod-arch-core
Version:
Core functionality and API utilities for modular architecture micro-frontend projects
74 lines • 2.91 kB
JavaScript
import React, { useContext } from 'react';
import { AlertVariant, NotificationActionTypes } from '../types';
import { NotificationContext } from '../context/NotificationContext';
var NotificationTypes;
(function (NotificationTypes) {
NotificationTypes["SUCCESS"] = "success";
NotificationTypes["ERROR"] = "error";
NotificationTypes["INFO"] = "info";
NotificationTypes["WARNING"] = "warning";
})(NotificationTypes || (NotificationTypes = {}));
export const useNotification = () => {
const { notificationCount, updateNotificationCount, dispatch } = useContext(NotificationContext);
const success = React.useCallback((title, message) => {
updateNotificationCount(notificationCount + 1);
dispatch({
type: NotificationActionTypes.ADD_NOTIFICATION,
payload: {
status: AlertVariant.success,
title,
timestamp: new Date(),
message,
id: notificationCount,
},
});
}, [dispatch, notificationCount, updateNotificationCount]);
const warning = React.useCallback((title, message) => {
updateNotificationCount(notificationCount + 1);
dispatch({
type: NotificationActionTypes.ADD_NOTIFICATION,
payload: {
status: AlertVariant.warning,
title,
timestamp: new Date(),
message,
id: notificationCount,
},
});
}, [dispatch, notificationCount, updateNotificationCount]);
const error = React.useCallback((title, message) => {
updateNotificationCount(notificationCount + 1);
dispatch({
type: NotificationActionTypes.ADD_NOTIFICATION,
payload: {
status: AlertVariant.danger,
title,
timestamp: new Date(),
message,
id: notificationCount,
},
});
}, [dispatch, notificationCount, updateNotificationCount]);
const info = React.useCallback((title, message) => {
updateNotificationCount(notificationCount + 1);
dispatch({
type: NotificationActionTypes.ADD_NOTIFICATION,
payload: {
status: AlertVariant.info,
title,
timestamp: new Date(),
message,
id: notificationCount,
},
});
}, [dispatch, notificationCount, updateNotificationCount]);
const remove = React.useCallback((id) => {
dispatch({
type: NotificationActionTypes.DELETE_NOTIFICATION,
payload: { id },
});
}, [dispatch]);
const notification = React.useMemo(() => ({ success, error, info, warning, remove }), [success, error, info, warning, remove]);
return notification;
};
//# sourceMappingURL=useNotification.js.map