File size: 817 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// @flow
import type { Dispatch } from 'redux';
type Toasts = 'success' | 'error' | 'neutral' | 'notification';

const addToast = (
  id: number,
  kind: Toasts,
  message: string,
  timeout?: number
) => {
  return {
    type: 'ADD_TOAST',
    payload: {
      id,
      kind,
      message,
      timeout,
    },
  };
};

const removeToast = (id: number) => {
  return { type: 'REMOVE_TOAST', id };
};

let nextToastId = 0;
export const addToastWithTimeout = (kind: Toasts, message: string) => (
  dispatch: Dispatch<Object>
) => {
  let timeout = 6000;
  if (kind === 'success') timeout = 3000;
  if (kind === 'notification') timeout = 5000;

  let id = nextToastId++;
  dispatch(addToast(id, kind, message, timeout));

  setTimeout(() => {
    dispatch(removeToast(id));
    id = nextToastId--;
  }, timeout);
};