Files
owl/examples/todoapp/components/TodoApp.js
T

61 lines
1.3 KiB
JavaScript
Raw Normal View History

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;
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() {
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() {
return this.props.todos.every(todo => todo.completed);
2019-03-19 14:10:05 +01:00
}
get remaining() {
return this.props.todos.filter(todo => !todo.completed).length;
2019-03-19 14:10:05 +01:00
}
get remainingText() {
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);
}
}
export default connect(mapStateToProps)(TodoApp);