-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainSection.js
104 lines (88 loc) · 2.57 KB
/
MainSection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React, { Component, PropTypes } from 'react';
import TodoItem from './TodoItem';
import Footer from './Footer';
import { SHOW_ALL, SHOW_MARKED, SHOW_UNMARKED } from '../constants/TodoFilters';
import ComputeTodos from '../stores/todos.js'
import * as TodoActions from '../actions/TodoActions';
const TODO_FILTERS = {
[SHOW_ALL]: () => true,
[SHOW_UNMARKED]: todo => !todo.marked,
[SHOW_MARKED]: todo => todo.marked
};
export default class MainSection extends Component {
static propTypes = {
loggit: PropTypes.object.isRequired
};
constructor(props, context) {
super(props, context);
this.state = { filter: SHOW_ALL };
}
handleClearMarked() {
const {todos} = this.data();
const atLeastOneMarked = todos.some(todo => todo.marked);
if (!atLeastOneMarked) return;
this.props.loggit.recordFact(TodoActions.clearMarked());
}
handleShow(filter) {
this.setState({ filter });
}
handleInputChanged() {
const {todos} = this.data();
const fact = (todos.every(todo => todo.marked))
? TodoActions.uncheckAll()
: TodoActions.checkAll();
this.props.loggit.recordFact(fact);
}
computations() {
return {
todos: ComputeTodos
}
}
data() {
return this.props.loggit.computeFor(this);
}
render() {
const { todos } = this.data();
const { filter } = this.state;
const filteredTodos = todos.filter(TODO_FILTERS[filter]);
const markedCount = todos.reduce((count, todo) =>
todo.marked ? count + 1 : count,
0
);
return (
<section className='main'>
{this.renderToggleAll(todos, markedCount)}
<ul className='todo-list'>
{filteredTodos.map(todo =>
<TodoItem key={todo.id} todo={todo} loggit={this.props.loggit} />
)}
</ul>
{this.renderFooter(markedCount)}
</section>
);
}
renderToggleAll(todos, markedCount) {
if (todos.length > 0) {
return (
<input className='toggle-all'
type='checkbox'
checked={markedCount === todos.length}
onChange={this.handleInputChanged.bind(this)} />
);
}
}
renderFooter(markedCount) {
const { todos } = this.data();
const { filter } = this.state;
const unmarkedCount = todos.length - markedCount;
if (todos.length) {
return (
<Footer markedCount={markedCount}
unmarkedCount={unmarkedCount}
filter={filter}
onClearMarked={this.handleClearMarked.bind(this)}
onShow={this.handleShow.bind(this)} />
);
}
}
}