UNPKG

780 BJavaScriptView Raw
1export const init = ({
2 store
3}) => {
4 const api = {
5 addNotification: notification => {
6 // Get rid of it if already exists
7 api.clearNotification(notification.id);
8 const {
9 notifications
10 } = store.getState();
11 store.setState({
12 notifications: [...notifications, notification]
13 });
14 },
15 clearNotification: id => {
16 const {
17 notifications
18 } = store.getState();
19 store.setState({
20 notifications: notifications.filter(n => n.id !== id)
21 });
22 const notification = notifications.find(n => n.id === id);
23
24 if (notification && notification.onClear) {
25 notification.onClear();
26 }
27 }
28 };
29 const state = {
30 notifications: []
31 };
32 return {
33 api,
34 state
35 };
36};
\No newline at end of file