-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
37 lines (31 loc) · 1.02 KB
/
index.ts
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
import { Type } from '../types';
export type SHOW_TOAST = 'SHOW_TOAST';
export const SHOW_TOAST: SHOW_TOAST = 'SHOW_TOAST';
export type HIDE_TOAST = 'HIDE_TOAST';
export const HIDE_TOAST: HIDE_TOAST = 'HIDE_TOAST';
export type ToastAction = {
type: SHOW_TOAST | HIDE_TOAST,
toast: Type.Toast
}
const showToast = (text: string, className: string) => ({
type: SHOW_TOAST,
toast: {
timestamp: Date.now(),
text,
className
}
});
export const hideToast = (toast: Type.Toast): ToastAction => ({
type: HIDE_TOAST,
toast
});
export const showSuccessToast = (text?: string) => (dispatch: any, getState: any): void => {
const toastAction = showToast(text, 'success');
dispatch(showToast(text, 'success'));
setTimeout(() => dispatch(hideToast(toastAction.toast)), 3500);
};
export const showErrorToast = (text?: string) => (dispatch: any, getState: any): void => {
const toastAction = showToast(text, 'error');
dispatch(showToast(text, 'error'));
setTimeout(() => dispatch(hideToast(toastAction.toast)), 3500);
};