Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.
Prev Previous commit
Next Next commit
fix Clear completed
  • Loading branch information
tabcat committed Dec 8, 2020
commit b16351935376e04d58e7c0c62b86925c90fc92a4
22 changes: 12 additions & 10 deletions src/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,18 @@ var app = app || {};
var main;
var todos = this.props.model.todos;

var shownTodos = todos.filter(function (todo) {
switch (this.state.nowShowing) {
case app.ACTIVE_TODOS:
return !todo.completed;
case app.COMPLETED_TODOS:
return todo.completed;
default:
return true;
}
}, this);
var shownTodos = todos
.filter(todo => !todo.cleared)
.filter(function (todo) {
switch (this.state.nowShowing) {
case app.ACTIVE_TODOS:
return !todo.completed;
case app.COMPLETED_TODOS:
return todo.completed;
default:
return true;
}
}, this);

var todoItems = shownTodos.map(function (todo) {
return (
Expand Down
14 changes: 9 additions & 5 deletions src/todoModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ var app = app || {};
const newTodo = {
id: Utils.uuid(),
title: title,
completed: false
completed: false,
cleared: false,
}
await Utils.store(this.db, this.key, newTodo);
this.inform();
Expand Down Expand Up @@ -83,10 +84,13 @@ var app = app || {};
this.inform();
};

app.TodoModel.prototype.clearCompleted = function () {
this.todos = this.todos.filter(function (todo) {
return !todo.completed;
});
app.TodoModel.prototype.clearCompleted = async function () {
for (let todo of this.todos) {
if (todo.completed) {
const updatedTodo = Utils.extend({}, todo, {cleared: true});
await Utils.store(this.db, this.key, updatedTodo);
}
}
this.inform();
};

Expand Down