This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
TodoHandler.js
91 lines (83 loc) · 3.08 KB
/
TodoHandler.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
import React from 'react';
import Flux from 'flummox/component';
import TodoList from '../components/TodoList';
import TodoInput from '../components/TodoInput';
import ItemsCounter from '../components/ItemsCounter';
import ToggleAll from '../components/ToggleAll';
class TodoHandler extends React.Component {
static async routerWillRun({flux, state}) {
let action = state.routes[state.routes.length - 1].name;
let todoActions = flux.getActions('todo');
switch (action) {
case 'active':
await todoActions.getActiveTasks();
break;
case 'completed':
await todoActions.getCompletedTasks();
break;
case 'all':
default:
await todoActions.getTasks();
break;
}
}
async handleNewTask(text) {
let actions = this.props.flux.getActions('todo');
await actions.createTask({text});
}
async handleToggleStatus(id, status) {
let actions = this.props.flux.getActions('todo');
await actions.toggleTask(id, status);
}
async handleToggleAll(status) {
let actions = this.props.flux.getActions('todo');
await actions.toggleAll(status);
}
async handleDeleteTask(id) {
let actions = this.props.flux.getActions('todo');
await actions.deleteTask(id);
}
async handleDeleteCompletedTasks(id) {
let actions = this.props.flux.getActions('todo');
await actions.deleteCompletedTasks();
}
render() {
return (
<div>
<header className="header">
<h1>todos</h1>
<TodoInput handleNewTask={this.handleNewTask.bind(this)} />
</header>
<section className="main">
<Flux connectToStores={['todo']}>
<ToggleAll onToggleStatus={this.handleToggleAll.bind(this)} />
</Flux>
<Flux connectToStores={['todo']}>
<TodoList onToggleStatus={this.handleToggleStatus.bind(this)}
onDeleteTask={this.handleDeleteTask.bind(this)} />
</Flux>
</section>
<footer className="footer">
<Flux connectToStores={['todo']}>
<ItemsCounter count={0} />
</Flux>
<ul className="filters">
<li>
<a href="/">All</a>
</li>
<li>
<a href="/active">Active</a>
</li>
<li>
<a href="/completed">Completed</a>
</li>
</ul>
<button className="clear-completed" onClick={this.handleDeleteCompletedTasks.bind(this)}>
Clear completed
</button>
</footer>
</div>
);
}
}
export default TodoHandler;