@@ -7,6 +7,14 @@ import { schema, normalize } from 'normalizr';
7
7
import uuid from 'uuid/v4' ;
8
8
import './index.css' ;
9
9
10
+ // filters
11
+
12
+ const VISIBILITY_FILTERS = {
13
+ SHOW_COMPLETED : item => item . completed ,
14
+ SHOW_INCOMPLETED : item => ! item . completed ,
15
+ SHOW_ALL : item => true ,
16
+ } ;
17
+
10
18
// schemas
11
19
12
20
const todoSchema = new schema . Entity ( 'todo' ) ;
@@ -97,7 +105,10 @@ function doSetFilter(filter) {
97
105
// selectors
98
106
99
107
function getTodosAsIds ( state ) {
100
- return state . todoState . ids ;
108
+ return state . todoState . ids
109
+ . map ( id => state . todoState . entities [ id ] )
110
+ . filter ( VISIBILITY_FILTERS [ state . filterState ] )
111
+ . map ( todo => todo . id ) ;
101
112
}
102
113
103
114
function getTodo ( state , todoId ) {
@@ -124,12 +135,33 @@ const store = createStore(
124
135
function TodoApp ( ) {
125
136
return (
126
137
< div >
138
+ < ConnectedFilter />
127
139
< ConnectedTodoCreate />
128
140
< ConnectedTodoList />
129
141
</ div >
130
142
) ;
131
143
}
132
144
145
+ function Filter ( { onSetFilter } ) {
146
+ return (
147
+ < div >
148
+ Show
149
+ < button
150
+ type = "text"
151
+ onClick = { ( ) => onSetFilter ( 'SHOW_ALL' ) } >
152
+ All</ button >
153
+ < button
154
+ type = "text"
155
+ onClick = { ( ) => onSetFilter ( 'SHOW_COMPLETED' ) } >
156
+ Completed</ button >
157
+ < button
158
+ type = "text"
159
+ onClick = { ( ) => onSetFilter ( 'SHOW_INCOMPLETED' ) } >
160
+ Incompleted</ button >
161
+ </ div >
162
+ ) ;
163
+ }
164
+
133
165
class TodoCreate extends React . Component {
134
166
constructor ( props ) {
135
167
super ( props ) ;
@@ -221,9 +253,16 @@ function mapDispatchToPropsCreate(dispatch) {
221
253
} ;
222
254
}
223
255
256
+ function mapDispatchToPropsFilter ( dispatch ) {
257
+ return {
258
+ onSetFilter : filterType => dispatch ( doSetFilter ( filterType ) ) ,
259
+ } ;
260
+ }
261
+
224
262
const ConnectedTodoList = connect ( mapStateToPropsList ) ( TodoList ) ;
225
263
const ConnectedTodoItem = connect ( mapStateToPropsItem , mapDispatchToPropsItem ) ( TodoItem ) ;
226
264
const ConnectedTodoCreate = connect ( null , mapDispatchToPropsCreate ) ( TodoCreate ) ;
265
+ const ConnectedFilter = connect ( null , mapDispatchToPropsFilter ) ( Filter ) ;
227
266
228
267
ReactDOM . render (
229
268
< Provider store = { store } >
0 commit comments