Skip to content

Commit 18962bb

Browse files
committed
use async notifications
1 parent 99a3cdc commit 18962bb

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/index.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const todoSchema = new schema.Entity('todo');
2525
const TODO_ADD = 'TODO_ADD';
2626
const TODO_TOGGLE = 'TODO_TOGGLE';
2727
const FILTER_SET = 'FILTER_SET';
28+
const NOTIFICATION_HIDE = 'NOTIFICATION_HIDE';
2829

2930
// reducers
3031

@@ -104,6 +105,9 @@ function notificationReducer(state = {}, action) {
104105
case TODO_ADD : {
105106
return applySetNotifyAboutAddTodo(state, action);
106107
}
108+
case NOTIFICATION_HIDE : {
109+
return applyRemoveNotification(state, action);
110+
}
107111
default : return state;
108112
}
109113
}
@@ -113,8 +117,33 @@ function applySetNotifyAboutAddTodo(state, action) {
113117
return { ...state, [id]: 'Todo Created: ' + name };
114118
}
115119

120+
function applyRemoveNotification(state, action) {
121+
const {
122+
[action.id]: notificationToRemove,
123+
...restNotifications,
124+
} = state;
125+
return restNotifications;
126+
}
127+
116128
// action creators
117129

130+
function doAddTodoWithNotification(id, name) {
131+
return function (dispatch) {
132+
dispatch(doAddTodo(id, name));
133+
134+
setTimeout(function () {
135+
dispatch(doHideNotification(id));
136+
}, 5000);
137+
}
138+
}
139+
140+
function doHideNotification(id) {
141+
return {
142+
type: NOTIFICATION_HIDE,
143+
id
144+
};
145+
}
146+
118147
function doAddTodo(id, name) {
119148
return {
120149
type: TODO_ADD,
@@ -301,7 +330,7 @@ function mapDispatchToPropsItem(dispatch) {
301330

302331
function mapDispatchToPropsCreate(dispatch) {
303332
return {
304-
onAddTodo: name => dispatch(doAddTodo(uuid(), name)),
333+
onAddTodo: name => dispatch(doAddTodoWithNotification(uuid(), name)),
305334
};
306335
}
307336

0 commit comments

Comments
 (0)