2018-12-22 17:20:04 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import TodoItem from './TodoItem';
|
|
|
|
import Footer from './Footer';
|
|
|
|
import { SHOW_ALL, SHOW_MARKED, SHOW_UNMARKED } from '../constants/TodoFilters';
|
|
|
|
|
|
|
|
const TODO_FILTERS = {
|
|
|
|
[SHOW_ALL]: () => true,
|
2020-08-08 23:26:39 +03:00
|
|
|
[SHOW_UNMARKED]: (todo) => !todo.marked,
|
|
|
|
[SHOW_MARKED]: (todo) => todo.marked,
|
2018-12-22 17:20:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class MainSection extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
todos: PropTypes.array.isRequired,
|
2020-08-08 23:26:39 +03:00
|
|
|
actions: PropTypes.object.isRequired,
|
2018-12-22 17:20:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
this.handleClearMarked = this.handleClearMarked.bind(this);
|
|
|
|
this.handleShow = this.handleShow.bind(this);
|
|
|
|
this.state = { filter: SHOW_ALL };
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClearMarked() {
|
2020-08-08 23:26:39 +03:00
|
|
|
const atLeastOneMarked = this.props.todos.some((todo) => todo.marked);
|
2018-12-22 17:20:04 +03:00
|
|
|
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]);
|
2019-01-10 21:51:14 +03:00
|
|
|
const markedCount = todos.reduce(
|
|
|
|
(count, todo) => (todo.marked ? count + 1 : count),
|
|
|
|
0
|
|
|
|
);
|
2018-12-22 17:20:04 +03:00
|
|
|
|
|
|
|
return (
|
2019-01-10 20:23:33 +03:00
|
|
|
<section className="main">
|
2018-12-22 17:20:04 +03:00
|
|
|
{this.renderToggleAll(markedCount)}
|
2019-01-10 20:23:33 +03:00
|
|
|
<ul className="todo-list">
|
2020-08-08 23:26:39 +03:00
|
|
|
{filteredTodos.map((todo) => (
|
2018-12-22 17:20:04 +03:00
|
|
|
<TodoItem key={todo.id} todo={todo} {...actions} />
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
{this.renderFooter(markedCount)}
|
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderToggleAll(markedCount) {
|
|
|
|
const { todos, actions } = this.props;
|
|
|
|
if (todos.length > 0) {
|
|
|
|
return (
|
|
|
|
<input
|
2019-01-10 20:23:33 +03:00
|
|
|
className="toggle-all"
|
|
|
|
type="checkbox"
|
2018-12-22 17:20:04 +03:00
|
|
|
checked={markedCount === todos.length}
|
|
|
|
onChange={actions.markAll}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderFooter(markedCount) {
|
|
|
|
const { todos } = this.props;
|
|
|
|
const { filter } = this.state;
|
|
|
|
const unmarkedCount = todos.length - markedCount;
|
|
|
|
|
|
|
|
if (todos.length) {
|
|
|
|
return (
|
|
|
|
<Footer
|
|
|
|
markedCount={markedCount}
|
|
|
|
unmarkedCount={unmarkedCount}
|
|
|
|
filter={filter}
|
|
|
|
onClearMarked={this.handleClearMarked}
|
|
|
|
onShow={this.handleShow}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|