generator-rrrw
Version:
react,redux,react-redux-router,webpack,babel,es6,autoprefixer,HMR,css-module
82 lines (69 loc) • 2.09 kB
JavaScript
import React, { Component } from 'react'
import TodoItem from '../TodoItem'
import Footer from '../Footer'
import { SHOW_ALL, SHOW_COMPLETED, SHOW_ACTIVE } from '../../constants/filters'
import style from './style.less'
const TODO_FILTERS = {
[SHOW_ALL]: () => true,
[SHOW_ACTIVE]: todo => !todo.completed,
[SHOW_COMPLETED]: todo => todo.completed
}
class MainSection extends Component {
constructor(props, context) {
super(props, context)
this.state = { filter: SHOW_ALL }
}
handleClearCompleted() {
const atLeastOneCompleted = this.props.todos.some(todo => todo.completed)
if (atLeastOneCompleted) {
this.props.actions.clearCompleted()
}
}
handleShow(filter) {
this.setState({ filter })
}
renderToggleAll(completedCount) {
const { todos, actions } = this.props
if (todos.length > 0) {
return <input
className={style.toggleAll}
type="checkbox"
checked={completedCount === todos.length}
onChange={actions.completeAll} />
}
}
renderFooter(completedCount) {
const { todos } = this.props
const { filter } = this.state
const activeCount = todos.length - completedCount
if (todos.length) {
return (
<Footer completedCount={completedCount}
activeCount={activeCount}
filter={filter}
onClearCompleted={::this.handleClearCompleted}
onShow={::this.handleShow} />
)
}
}
render() {
const { todos, actions } = this.props
const { filter } = this.state
const filteredTodos = todos.filter(TODO_FILTERS[filter])
const completedCount = todos.reduce((count, todo) => {
return todo.completed ? count + 1 : count
}, 0)
return (
<section className={style.main}>
{this.renderToggleAll(completedCount)}
<ul className={style.normal}>
{filteredTodos.map(todo =>
<TodoItem key={todo.id} todo={todo} {...actions} />
)}
</ul>
{this.renderFooter(completedCount)}
</section>
)
}
}
export default MainSection