2019-03-19 14:10:05 +01:00
|
|
|
import { TodoItem } from "./TodoItem.js";
|
|
|
|
|
|
2019-03-27 15:26:20 +01:00
|
|
|
const { Component } = owl.core;
|
|
|
|
|
const { connect } = owl.extras;
|
2019-03-19 14:10:05 +01:00
|
|
|
|
2019-03-20 13:36:37 +01:00
|
|
|
const ENTER_KEY = 13;
|
|
|
|
|
|
2019-03-22 09:52:48 +01:00
|
|
|
function mapStateToProps(state) {
|
|
|
|
|
return { todos: state.todos };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TodoApp extends Component {
|
2019-03-19 14:10:05 +01:00
|
|
|
template = "todoapp";
|
|
|
|
|
widgets = { TodoItem };
|
|
|
|
|
state = { filter: "all" };
|
|
|
|
|
|
2019-03-20 13:36:37 +01:00
|
|
|
get visibleTodos() {
|
2019-03-22 09:52:48 +01:00
|
|
|
let todos = this.props.todos;
|
2019-03-20 13:36:37 +01:00
|
|
|
if (this.state.filter === "active") {
|
|
|
|
|
todos = todos.filter(t => !t.completed);
|
|
|
|
|
}
|
|
|
|
|
if (this.state.filter === "completed") {
|
|
|
|
|
todos = todos.filter(t => t.completed);
|
|
|
|
|
}
|
|
|
|
|
return todos;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-19 14:10:05 +01:00
|
|
|
get allChecked() {
|
2019-03-22 09:52:48 +01:00
|
|
|
return this.props.todos.every(todo => todo.completed);
|
2019-03-19 14:10:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get remaining() {
|
2019-03-22 09:52:48 +01:00
|
|
|
return this.props.todos.filter(todo => !todo.completed).length;
|
2019-03-19 14:10:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get remainingText() {
|
2019-03-22 11:09:57 +01:00
|
|
|
const items = this.remaining < 2 ? "item" : "items";
|
|
|
|
|
return ` ${items} left`;
|
2019-03-19 14:10:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addTodo(ev) {
|
2019-03-20 13:36:37 +01:00
|
|
|
if (ev.keyCode === ENTER_KEY) {
|
|
|
|
|
const title = ev.target.value;
|
|
|
|
|
if (title.trim()) {
|
|
|
|
|
this.env.store.dispatch("addTodo", title);
|
2019-03-19 14:10:05 +01:00
|
|
|
}
|
|
|
|
|
ev.target.value = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearCompleted() {
|
|
|
|
|
this.env.store.dispatch("clearCompleted");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toggleAll() {
|
|
|
|
|
this.env.store.dispatch("toggleAll", !this.allChecked);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-22 09:52:48 +01:00
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(TodoApp);
|