mod-arch-core
Version:
Core functionality and API utilities for modular architecture micro-frontend projects
43 lines • 1.73 kB
JavaScript
import React, { createContext } from 'react';
import { NotificationActionTypes } from '../types';
export const NotificationContext = createContext({
notifications: [],
notificationCount: 0,
// eslint-disable-next-line @typescript-eslint/no-empty-function
updateNotificationCount: () => { },
// eslint-disable-next-line @typescript-eslint/no-empty-function
dispatch: () => { },
});
const notificationReducer = (notifications, action) => {
switch (action.type) {
case NotificationActionTypes.ADD_NOTIFICATION: {
return [
...notifications,
{
status: action.payload.status,
title: action.payload.title,
timestamp: action.payload.timestamp,
message: action.payload.message,
id: action.payload.id,
},
];
}
case NotificationActionTypes.DELETE_NOTIFICATION: {
return notifications.filter((t) => t.id !== action.payload.id);
}
default: {
return notifications;
}
}
};
export const NotificationContextProvider = ({ children, }) => {
const [notifications, dispatch] = React.useReducer(notificationReducer, []);
const [notificationCount, setNotificationCount] = React.useState(0);
return (React.createElement(NotificationContext.Provider, { value: React.useMemo(() => ({
notifications,
notificationCount,
updateNotificationCount: setNotificationCount,
dispatch,
}), [notifications, notificationCount, setNotificationCount, dispatch]) }, children));
};
//# sourceMappingURL=NotificationContext.js.map