UNPKG

1.07 kBJavaScriptView Raw
1/* @flow */
2import { icons, notificationActions, NOTIFICATION_TIMEOUT } from '../const';
3
4export type NotificationParam = {
5 callToAction?: string,
6 dismissable?: boolean,
7 icon: $Keys<typeof icons>,
8 onCallToAction?: () => void,
9 text: string,
10 top?: boolean,
11 timeout: number,
12};
13
14export const removeNotification = (id: number): ThunkAction<> => dispatch => {
15 dispatch({
16 type: notificationActions.TRANSITION_NOTIFICATION,
17 payload: { id },
18 });
19 setTimeout(() => {
20 dispatch({
21 type: notificationActions.REMOVE_NOTIFICATION,
22 payload: { id },
23 });
24 }, NOTIFICATION_TIMEOUT);
25};
26
27export const showNotification = (
28 payload: NotificationParam
29): ThunkAction<Promise<number>> => dispatch => {
30 const id = performance.now();
31 dispatch({
32 type: notificationActions.SHOW_NOTIFICATION,
33 payload: { ...payload, id },
34 });
35 if (payload.timeout) {
36 setTimeout(() => dispatch(removeNotification(id)), payload.timeout);
37 }
38 return Promise.resolve(id);
39};