forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toast.js
47 lines (42 loc) · 1.05 KB
/
toast.js
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
43
44
45
46
47
import { combineActions, createAction, handleActions } from 'redux-actions';
import uuid from 'uuid';
const prefix = 'TOAST';
const showSuccessToast = createAction(`${prefix}/SHOW_SUCCESS_TOAST`, message => ({ type: 'success', message }));
const showErrorToast = createAction(`${prefix}/SHOW_ERROR_TOAST`, message => ({ type: 'error', message }));
const hideToast = createAction(`${prefix}/HIDE_TOAST`, id => ({ id }));
export const actions = {
showSuccessToast,
showErrorToast,
hideToast,
};
const defaultState = {
toasts: [],
};
export default handleActions({
[combineActions(
showSuccessToast,
showErrorToast,
)]: (state, { payload }) => {
const id = uuid.v4();
const toast = {
id,
...payload,
};
const toasts = [
...state.toasts,
toast,
];
return {
...state,
toasts,
};
},
[hideToast]: (state, { payload }) => {
const { id } = payload;
const toasts = state.toasts.filter(toast => toast.id !== id);
return {
...state,
toasts,
};
},
}, defaultState);