import React, { Component, PropTypes } from 'react'; import TodoItem from './TodoItem'; import Footer from './Footer'; import { SHOW_ALL, SHOW_MARKED, SHOW_UNMARKED } from '../constants/TodoFilters'; const TODO_FILTERS = { [SHOW_ALL]: () => true, [SHOW_UNMARKED]: todo => !todo.marked, [SHOW_MARKED]: todo => todo.marked }; export default class MainSection extends Component { static propTypes = { todos: PropTypes.array.isRequired, actions: PropTypes.object.isRequired }; constructor(props, context) { super(props, context); this.state = { filter: SHOW_ALL }; } handleClearMarked() { const atLeastOneMarked = this.props.todos.some(todo => todo.marked); if (atLeastOneMarked) { this.props.actions.clearMarked(); } } handleShow(filter) { this.setState({ filter }); } render() { const { todos, actions } = this.props; const { filter } = this.state; const filteredTodos = todos.filter(TODO_FILTERS[filter]); const markedCount = todos.reduce((count, todo) => todo.marked ? count + 1 : count, 0 ); return (
{this.renderToggleAll(markedCount)} {this.renderFooter(markedCount)}
); } renderToggleAll(markedCount) { const { todos, actions } = this.props; if (todos.length > 0) { return ( ); } } renderFooter(markedCount) { const { todos } = this.props; const { filter } = this.state; const unmarkedCount = todos.length - markedCount; if (todos.length) { return (