battled and won typings

This commit is contained in:
Ken
2019-01-31 11:21:05 -08:00
parent 578ad8221d
commit fceb24f985
5 changed files with 55 additions and 31 deletions

View File

@@ -1,4 +1,4 @@
import * as actions from '../actions';
import { actions, TodoAction } from '../actions';
import { Store, FilterTypes } from '../store';
import { connect } from 'react-redux';
import { Dispatch } from 'redux';
@@ -11,7 +11,7 @@ export function mapStateToProps({ todos, filter }: Store) {
};
}
export function mapDispatchToProps(dispatch: Dispatch<actions.TodoAction>) {
export function mapDispatchToProps(dispatch: Dispatch<TodoAction>) {
return {
add: (label: string) => dispatch(actions.add(label)),
remove: (id: string) => dispatch(actions.remove(id)),

View File

@@ -14,21 +14,27 @@ export interface TodoListProps {
export class TodoList extends React.Component<TodoListProps> {
render() {
const { filter, todos } = this.props;
let filteredTodos = todos;
let filteredTodos: typeof todos = {};
switch (filter) {
case 'completed':
filteredTodos = Object.keys(todos).reduce(
(collection, id) => (todos[id].completed ? { ...collection, id: todos[id] } : collection),
{}
);
Object.keys(todos).forEach(id => {
if (todos[id].completed) {
filteredTodos[id] = todos[id];
}
});
break;
case 'active':
filteredTodos = Object.keys(todos).reduce(
(collection, id) => (!todos[id].completed ? { ...collection, id: todos[id] } : collection),
{}
);
Object.keys(todos).forEach(id => {
if (!todos[id].completed) {
filteredTodos[id] = todos[id];
}
});
break;
default:
filteredTodos = todos;
break;
}