Skip to content

Commit 99a3cdc

Browse files
committed
use sync notifications
1 parent 335c4c9 commit 99a3cdc

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"react-redux": "^5.0.5",
1313
"redux": "^3.6.0",
1414
"redux-logger": "^3.0.6",
15+
"redux-thunk": "^2.2.0",
1516
"uuid": "^3.0.1"
1617
},
1718
"scripts": {

src/index.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
33
import { applyMiddleware, combineReducers, createStore } from 'redux';
44
import { Provider, connect } from 'react-redux';
55
import { createLogger } from 'redux-logger';
6+
import thunk from 'redux-thunk';
67
import { schema, normalize } from 'normalizr';
78
import uuid from 'uuid/v4';
89
import './index.css';
@@ -98,6 +99,20 @@ function applySetFilter(state, action) {
9899
return action.filter;
99100
}
100101

102+
function notificationReducer(state = {}, action) {
103+
switch(action.type) {
104+
case TODO_ADD : {
105+
return applySetNotifyAboutAddTodo(state, action);
106+
}
107+
default : return state;
108+
}
109+
}
110+
111+
function applySetNotifyAboutAddTodo(state, action) {
112+
const { name, id } = action.todo;
113+
return { ...state, [id]: 'Todo Created: ' + name };
114+
}
115+
101116
// action creators
102117

103118
function doAddTodo(id, name) {
@@ -134,19 +149,28 @@ function getTodo(state, todoId) {
134149
return state.todoState.entities[todoId];
135150
}
136151

152+
function getNotifications(state) {
153+
return getArrayOfObject(state.notificationState);
154+
}
155+
156+
function getArrayOfObject(object) {
157+
return Object.keys(object).map(key => object[key]);
158+
}
159+
137160
// store
138161

139162
const rootReducer = combineReducers({
140163
todoState: todoReducer,
141164
filterState: filterReducer,
165+
notificationState: notificationReducer,
142166
});
143167

144168
const logger = createLogger();
145169

146170
const store = createStore(
147171
rootReducer,
148172
undefined,
149-
applyMiddleware(logger)
173+
applyMiddleware(thunk, logger)
150174
);
151175

152176
// components
@@ -157,6 +181,15 @@ function TodoApp() {
157181
<ConnectedFilter />
158182
<ConnectedTodoCreate />
159183
<ConnectedTodoList />
184+
<ConnectedNotifications />
185+
</div>
186+
);
187+
}
188+
189+
function Notifications({ notifications }) {
190+
return (
191+
<div>
192+
{notifications.map(note => <div key={note}>{note}</div>)}
160193
</div>
161194
);
162195
}
@@ -278,10 +311,17 @@ function mapDispatchToPropsFilter(dispatch) {
278311
};
279312
}
280313

314+
function mapStateToPropsNotifications(state, props) {
315+
return {
316+
notifications: getNotifications(state),
317+
};
318+
}
319+
281320
const ConnectedTodoList = connect(mapStateToPropsList)(TodoList);
282321
const ConnectedTodoItem = connect(mapStateToPropsItem, mapDispatchToPropsItem)(TodoItem);
283322
const ConnectedTodoCreate = connect(null, mapDispatchToPropsCreate)(TodoCreate);
284323
const ConnectedFilter = connect(null, mapDispatchToPropsFilter)(Filter);
324+
const ConnectedNotifications = connect(mapStateToPropsNotifications)(Notifications);
285325

286326
ReactDOM.render(
287327
<Provider store={store}>

0 commit comments

Comments
 (0)