diff --git a/static/index.js b/static/index.js index 69c03cf..7947bcf 100644 --- a/static/index.js +++ b/static/index.js @@ -1,25 +1,13 @@ -function add_error(e) { - while (!e.is(".row")) { - e = e.parent(); - } - e.addClass("error"); -} - -function remove_error(e) { - while (!e.is(".row")) { - e = e.parent(); - } - e.removeClass("error"); -} - function check(id) { let checkbox = $("#task-" + id); - remove_error(checkbox); + checkbox.parents(".row").removeClass("error"); + let uri = "/v1/undo"; if (checkbox.prop("checked")) { uri = "/v1/do"; - // TODO hide it! + checkbox.parents("li").css("display", "none"); } + $.post(uri, {id: id}, function(data) { checkbox.prop("checked", data.task.done); }) @@ -29,7 +17,8 @@ function check(id) { } else { checkbox.prop("checked", true); } - add_error(checkbox); // TODO unhide it + checkbox.parents(".row").addClass("error"); + checkbox.parents("li").css("display", "block"); }); } @@ -41,6 +30,7 @@ function get_template(id) { function new_task() { let input = $("#new-task-input"); let template; + $.post("/v1/new", {content: input.val()}, function(data) { template = get_template("#task-template"); let input = $("input:checkbox", template); @@ -52,7 +42,7 @@ function new_task() { }) .fail(function(request) { template = get_template("#error-template"); - $(".error-text", template).text("ERROR:" + request.responseJSON.errors.join(" ")); + $(".error-text", template).text("ERROR: " + request.responseJSON.errors.join(" ")); }) .always(function() { $("#new-task").before(template); @@ -77,21 +67,19 @@ function new_api_key() { } function delete_item(e) { - e = $(e); - while (!e.is("li")) { - e = e.parent(); - } + e = e.parents("li"); checkbox = $("input:checkbox", e); if (checkbox.length) { e.css("display", "hidden"); let id = checkbox.prop("id"); - id = Number(id.slice(id.lastIndexOf("-") + 1, id.length)) + id = Number(id.slice(id.lastIndexOf("-") + 1, id.length)); + $.post("/v1/delete", {id: id}, function(data) { e.remove(); }) .fail(function(request) { - add_error($(".row", e)); + e.children(".row").addClass("error"); e.css("display", "block"); }); return; @@ -104,7 +92,7 @@ function delete_item(e) { e.remove(); }) .fail(function(request) { - add_error($(".row", e)); + e.children(".row").addClass("error"); code.css("color", "black"); // make key still readable e.css("display", "block"); });