Skip to content

Commit a2b6f28

Browse files
committed
filters
1 parent dc3f4f5 commit a2b6f28

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/index.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import { schema, normalize } from 'normalizr';
77
import uuid from 'uuid/v4';
88
import './index.css';
99

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+
1018
// schemas
1119

1220
const todoSchema = new schema.Entity('todo');
@@ -97,7 +105,10 @@ function doSetFilter(filter) {
97105
// selectors
98106

99107
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);
101112
}
102113

103114
function getTodo(state, todoId) {
@@ -124,12 +135,33 @@ const store = createStore(
124135
function TodoApp() {
125136
return (
126137
<div>
138+
<ConnectedFilter />
127139
<ConnectedTodoCreate />
128140
<ConnectedTodoList />
129141
</div>
130142
);
131143
}
132144

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+
133165
class TodoCreate extends React.Component {
134166
constructor(props) {
135167
super(props);
@@ -221,9 +253,16 @@ function mapDispatchToPropsCreate(dispatch) {
221253
};
222254
}
223255

256+
function mapDispatchToPropsFilter(dispatch) {
257+
return {
258+
onSetFilter: filterType => dispatch(doSetFilter(filterType)),
259+
};
260+
}
261+
224262
const ConnectedTodoList = connect(mapStateToPropsList)(TodoList);
225263
const ConnectedTodoItem = connect(mapStateToPropsItem, mapDispatchToPropsItem)(TodoItem);
226264
const ConnectedTodoCreate = connect(null, mapDispatchToPropsCreate)(TodoCreate);
265+
const ConnectedFilter = connect(null, mapDispatchToPropsFilter)(Filter);
227266

228267
ReactDOM.render(
229268
<Provider store={store}>

0 commit comments

Comments
 (0)